bitbucket.org/ai69/amoy@v0.2.3/string.go (about)

     1  package amoy
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  
     7  	"github.com/1set/gut/ystring"
     8  )
     9  
    10  // EmptyStr is the missing empty string for Go :)
    11  var EmptyStr string
    12  
    13  // TruncateStr renders a truncated string of the given length, or original one if it's shorter.
    14  func TruncateStr(s string, limit int) string {
    15  	runes := []rune(s)
    16  	if l := len(runes); l > limit {
    17  		if limit <= 0 {
    18  			return EmptyStr
    19  		}
    20  		return string(runes[0:limit])
    21  	}
    22  	return s
    23  }
    24  
    25  // ReverseStr returns a reversed string of the given string.
    26  func ReverseStr(s string) string {
    27  	runes := []rune(s)
    28  	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
    29  		runes[i], runes[j] = runes[j], runes[i]
    30  	}
    31  	return string(runes)
    32  }
    33  
    34  // SubstrAfterFirst returns a substring which starts after the first occurrence of target and continues to the end, or empty string if the target is not found or empty.
    35  func SubstrAfterFirst(s, sub string) string {
    36  	if s == EmptyStr || sub == EmptyStr {
    37  		return EmptyStr
    38  	}
    39  	idx := strings.Index(s, sub)
    40  	if idx < 0 {
    41  		return EmptyStr
    42  	}
    43  	return s[idx+len(sub):]
    44  }
    45  
    46  // SubstrAfterLast returns a substring which starts after the last occurrence of target and continues to the end, or empty string if the target is not found or empty.
    47  func SubstrAfterLast(s, sub string) string {
    48  	if s == EmptyStr || sub == EmptyStr {
    49  		return EmptyStr
    50  	}
    51  	idx := strings.LastIndex(s, sub)
    52  	if idx < 0 {
    53  		return EmptyStr
    54  	}
    55  	return s[idx+len(sub):]
    56  }
    57  
    58  // SubstrBeforeFirst returns a substring which starts starts at the begin of a string and continues to the first occurrence of target, or empty string if the target is not found or empty.
    59  func SubstrBeforeFirst(s, sub string) string {
    60  	if s == EmptyStr || sub == EmptyStr {
    61  		return EmptyStr
    62  	}
    63  	idx := strings.Index(s, sub)
    64  	if idx < 0 {
    65  		return EmptyStr
    66  	}
    67  	return s[0:idx]
    68  }
    69  
    70  // SubstrBeforeLast returns a substring which starts starts at the begin of a string and continues to the last occurrence of target, or empty string if the target is not found or empty.
    71  func SubstrBeforeLast(s, sub string) string {
    72  	if s == EmptyStr || sub == EmptyStr {
    73  		return EmptyStr
    74  	}
    75  	idx := strings.LastIndex(s, sub)
    76  	if idx < 0 {
    77  		return EmptyStr
    78  	}
    79  	return s[0:idx]
    80  }
    81  
    82  // SubstrOrOrigin returns the original string if the target is missed in substr function.
    83  func SubstrOrOrigin(subFunc func(string, string) string, s, sub string) string {
    84  	if result := subFunc(s, sub); ystring.IsNotEmpty(result) {
    85  		return result
    86  	}
    87  	return s
    88  }
    89  
    90  // ContainsChinese returns true if the string contains Chinese characters (Hanzhi).
    91  func ContainsChinese(s string) bool {
    92  	for _, r := range s {
    93  		if unicode.Is(unicode.Han, r) {
    94  			return true
    95  		}
    96  	}
    97  	return false
    98  }
    99  
   100  // ContainsKorean returns true if the string contains Korean characters (Hangul).
   101  func ContainsKorean(s string) bool {
   102  	for _, r := range s {
   103  		if unicode.Is(unicode.Hangul, r) {
   104  			return true
   105  		}
   106  	}
   107  	return false
   108  }
   109  
   110  // ContainsJapanese returns true if the string contains Japanese characters (Kanji, Hiragana, Katakana).
   111  func ContainsJapanese(s string) bool {
   112  	for _, r := range s {
   113  		if unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r) {
   114  			return true
   115  		}
   116  	}
   117  	return false
   118  }
   119  
   120  // TrimLeftSpace returns a string with all leading spaces removed.
   121  func TrimLeftSpace(s string) string {
   122  	return strings.TrimLeftFunc(s, unicode.IsSpace)
   123  }
   124  
   125  // TrimRightSpace returns a string with all trailing spaces removed.
   126  func TrimRightSpace(s string) string {
   127  	return strings.TrimRightFunc(s, unicode.IsSpace)
   128  }
   129  
   130  // TrimInnerSpace returns a string with all inner, leading, trailing spaces removed.
   131  func TrimInnerSpace(s string) string {
   132  	return ystring.Shrink(s, EmptyStr)
   133  }