improve package gstr

This commit is contained in:
John Guo
2021-08-30 14:29:04 +08:00
parent 549adf6487
commit 0048e2c8c4
6 changed files with 271 additions and 236 deletions

View File

@ -28,95 +28,22 @@ const (
NotFoundIndex = -1
)
// Replace returns a copy of the string <origin>
// in which string <search> replaced by <replace> case-sensitively.
func Replace(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
return strings.Replace(origin, search, replace, n)
}
const (
defaultSuffixForStrLimit = "..."
)
// Replace returns a copy of the string <origin>
// in which string <search> replaced by <replace> case-insensitively.
func ReplaceI(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
if n == 0 {
return origin
}
var (
length = len(search)
searchLower = strings.ToLower(search)
)
for {
originLower := strings.ToLower(origin)
if pos := strings.Index(originLower, searchLower); pos != -1 {
origin = origin[:pos] + replace + origin[pos+length:]
if n--; n == 0 {
break
}
} else {
break
}
}
return origin
}
// Count counts the number of <substr> appears in <s>.
// It returns 0 if no <substr> found in <s>.
// Count counts the number of `substr` appears in `s`.
// It returns 0 if no `substr` found in `s`.
func Count(s, substr string) int {
return strings.Count(s, substr)
}
// CountI counts the number of <substr> appears in <s>, case-insensitively.
// It returns 0 if no <substr> found in <s>.
// CountI counts the number of `substr` appears in `s`, case-insensitively.
// It returns 0 if no `substr` found in `s`.
func CountI(s, substr string) int {
return strings.Count(ToLower(s), ToLower(substr))
}
// ReplaceByArray returns a copy of <origin>,
// which is replaced by a slice in order, case-sensitively.
func ReplaceByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = Replace(origin, array[i], array[i+1])
}
return origin
}
// ReplaceIByArray returns a copy of <origin>,
// which is replaced by a slice in order, case-insensitively.
func ReplaceIByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = ReplaceI(origin, array[i], array[i+1])
}
return origin
}
// ReplaceByMap returns a copy of <origin>,
// which is replaced by a map in unordered way, case-sensitively.
func ReplaceByMap(origin string, replaces map[string]string) string {
return utils.ReplaceByMap(origin, replaces)
}
// ReplaceIByMap returns a copy of <origin>,
// which is replaced by a map in unordered way, case-insensitively.
func ReplaceIByMap(origin string, replaces map[string]string) string {
for k, v := range replaces {
origin = ReplaceI(origin, k, v)
}
return origin
}
// ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
func ToLower(s string) string {
return strings.ToLower(s)
@ -163,86 +90,7 @@ func IsNumeric(s string) bool {
return utils.IsNumeric(s)
}
// SubStr returns a portion of string <str> specified by the <start> and <length> parameters.
func SubStr(str string, start int, length ...int) (substr string) {
lth := len(str)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= lth {
start = lth
}
end := lth
if len(length) > 0 {
end = start + length[0]
if end < start {
end = lth
}
}
if end > lth {
end = lth
}
return str[start:end]
}
// SubStrRune returns a portion of string <str> specified by the <start> and <length> parameters.
// SubStrRune considers parameter <str> as unicode string.
func SubStrRune(str string, start int, length ...int) (substr string) {
// Converting to []rune to support unicode.
rs := []rune(str)
lth := len(rs)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= lth {
start = lth
}
end := lth
if len(length) > 0 {
end = start + length[0]
if end < start {
end = lth
}
}
if end > lth {
end = lth
}
return string(rs[start:end])
}
// StrLimit returns a portion of string <str> specified by <length> parameters, if the length
// of <str> is greater than <length>, then the <suffix> will be appended to the result string.
func StrLimit(str string, length int, suffix ...string) string {
if len(str) < length {
return str
}
addStr := "..."
if len(suffix) > 0 {
addStr = suffix[0]
}
return str[0:length] + addStr
}
// StrLimitRune returns a portion of string <str> specified by <length> parameters, if the length
// of <str> is greater than <length>, then the <suffix> will be appended to the result string.
// StrLimitRune considers parameter <str> as unicode string.
func StrLimitRune(str string, length int, suffix ...string) string {
rs := []rune(str)
if len(rs) < length {
return str
}
addStr := "..."
if len(suffix) > 0 {
addStr = suffix[0]
}
return string(rs[0:length]) + addStr
}
// Reverse returns a string which is the reverse of <str>.
// Reverse returns a string which is the reverse of `str`.
func Reverse(str string) string {
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
@ -252,9 +100,9 @@ func Reverse(str string) string {
}
// NumberFormat formats a number with grouped thousands.
// <decimals>: Sets the number of decimal points.
// <decPoint>: Sets the separator for the decimal point.
// <thousandsSep>: Sets the thousands separator.
// `decimals`: Sets the number of decimal points.
// `decPoint`: Sets the separator for the decimal point.
// `thousandsSep`: Sets the thousands' separator.
// See http://php.net/manual/en/function.number-format.php.
func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string {
neg := false
@ -301,7 +149,7 @@ func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) s
// Can be used to split a string into smaller chunks which is useful for
// e.g. converting BASE64 string output to match RFC 2045 semantics.
// It inserts end every chunkLen characters.
// It considers parameter <body> and <end> as unicode string.
// It considers parameter `body` and `end` as unicode string.
func ChunkSplit(body string, chunkLen int, end string) string {
if end == "" {
end = "\r\n"
@ -329,7 +177,7 @@ func Compare(a, b string) int {
return strings.Compare(a, b)
}
// Equal reports whether <a> and <b>, interpreted as UTF-8 strings,
// Equal reports whether `a` and `b`, interpreted as UTF-8 strings,
// are equal under Unicode case-folding, case-insensitively.
func Equal(a, b string) bool {
return strings.EqualFold(a, b)
@ -351,7 +199,7 @@ func HasSuffix(s, suffix string) bool {
}
// CountWords returns information about words' count used in a string.
// It considers parameter <str> as unicode string.
// It considers parameter `str` as unicode string.
func CountWords(str string) map[string]int {
m := make(map[string]int)
buffer := bytes.NewBuffer(nil)
@ -372,7 +220,7 @@ func CountWords(str string) map[string]int {
}
// CountChars returns information about chars' count used in a string.
// It considers parameter <str> as unicode string.
// It considers parameter `str` as unicode string.
func CountChars(str string, noSpace ...bool) map[string]int {
m := make(map[string]int)
countSpace := true
@ -465,51 +313,8 @@ func Repeat(input string, multiplier int) string {
return strings.Repeat(input, multiplier)
}
// Str returns part of <haystack> string starting from and including
// the first occurrence of <needle> to the end of <haystack>.
// See http://php.net/manual/en/function.strstr.php.
func Str(haystack string, needle string) string {
if needle == "" {
return ""
}
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex {
return ""
}
return haystack[pos+len([]byte(needle))-1:]
}
// StrEx returns part of <haystack> string starting from and excluding
// the first occurrence of <needle> to the end of <haystack>.
func StrEx(haystack string, needle string) string {
if s := Str(haystack, needle); s != "" {
return s[1:]
}
return ""
}
// StrTill returns part of <haystack> string ending to and including
// the first occurrence of <needle> from the start of <haystack>.
func StrTill(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex || pos == 0 {
return ""
}
return haystack[:pos+1]
}
// StrTillEx returns part of <haystack> string ending to and excluding
// the first occurrence of <needle> from the start of <haystack>.
func StrTillEx(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex || pos == 0 {
return ""
}
return haystack[:pos]
}
// Shuffle randomly shuffles a string.
// It considers parameter <str> as unicode string.
// It considers parameter `str` as unicode string.
func Shuffle(str string) string {
runes := []rune(str)
s := make([]rune, len(runes))
@ -519,12 +324,12 @@ func Shuffle(str string) string {
return string(s)
}
// Split splits string <str> by a string <delimiter>, to an array.
// Split splits string `str` by a string `delimiter`, to an array.
func Split(str, delimiter string) []string {
return strings.Split(str, delimiter)
}
// SplitAndTrim splits string <str> by a string <delimiter> to an array,
// SplitAndTrim splits string `str` by a string `delimiter` to an array,
// and calls Trim to every element of this array. It ignores the elements
// which are empty after Trim.
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
@ -538,7 +343,7 @@ func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
return array
}
// SplitAndTrimSpace splits string <str> by a string <delimiter> to an array,
// SplitAndTrimSpace splits string `str` by a string `delimiter` to an array,
// and calls TrimSpace to every element of this array.
// Deprecated, use SplitAndTrim instead.
func SplitAndTrimSpace(str, delimiter string) []string {
@ -552,27 +357,27 @@ func SplitAndTrimSpace(str, delimiter string) []string {
return array
}
// Join concatenates the elements of <array> to create a single string. The separator string
// <sep> is placed between elements in the resulting string.
// Join concatenates the elements of `array` to create a single string. The separator string
// `sep` is placed between elements in the resulting string.
func Join(array []string, sep string) string {
return strings.Join(array, sep)
}
// JoinAny concatenates the elements of <array> to create a single string. The separator string
// <sep> is placed between elements in the resulting string.
// JoinAny concatenates the elements of `array` to create a single string. The separator string
// `sep` is placed between elements in the resulting string.
//
// The parameter <array> can be any type of slice, which be converted to string array.
// The parameter `array` can be any type of slice, which be converted to string array.
func JoinAny(array interface{}, sep string) string {
return strings.Join(gconv.Strings(array), sep)
}
// Explode splits string <str> by a string <delimiter>, to an array.
// Explode splits string `str` by a string `delimiter`, to an array.
// See http://php.net/manual/en/function.explode.php.
func Explode(delimiter, str string) []string {
return Split(str, delimiter)
}
// Implode joins array elements <pieces> with a string <glue>.
// Implode joins array elements `pieces` with a string `glue`.
// http://php.net/manual/en/function.implode.php
func Implode(glue string, pieces []string) string {
return strings.Join(pieces, glue)
@ -588,8 +393,8 @@ func Ord(char string) int {
return int(char[0])
}
// HideStr replaces part of the the string <str> to <hide> by <percentage> from the <middle>.
// It considers parameter <str> as unicode string.
// HideStr replaces part of the string `str` to `hide` by `percentage` from the `middle`.
// It considers parameter `str` as unicode string.
func HideStr(str string, percent int, hide string) string {
array := strings.Split(str, "@")
if len(array) > 1 {
@ -619,7 +424,7 @@ func HideStr(str string, percent int, hide string) string {
// Nl2Br inserts HTML line breaks(<br>|<br />) before all newlines in a string:
// \n\r, \r\n, \r, \n.
// It considers parameter <str> as unicode string.
// It considers parameter `str` as unicode string.
func Nl2Br(str string, isXhtml ...bool) string {
r, n, runes := '\r', '\n', []rune(str)
var br []byte
@ -705,9 +510,9 @@ func QuoteMeta(str string, chars ...string) string {
return buf.String()
}
// SearchArray searches string <s> in string slice <a> case-sensitively,
// returns its index in <a>.
// If <s> is not found in <a>, it returns -1.
// SearchArray searches string `s` in string slice `a` case-sensitively,
// returns its index in `a`.
// If `s` is not found in `a`, it returns -1.
func SearchArray(a []string, s string) int {
for i, v := range a {
if s == v {
@ -717,7 +522,7 @@ func SearchArray(a []string, s string) int {
return NotFoundIndex
}
// InArray checks whether string <s> in slice <a>.
// InArray checks whether string `s` in slice `a`.
func InArray(a []string, s string) bool {
return SearchArray(a, s) != NotFoundIndex
}

View File

@ -8,8 +8,8 @@ package gstr
import "strings"
// Pos returns the position of the first occurrence of <needle>
// in <haystack> from <startOffset>, case-sensitively.
// Pos returns the position of the first occurrence of `needle`
// in `haystack` from <startOffset>, case-sensitively.
// It returns -1, if not found.
func Pos(haystack, needle string, startOffset ...int) int {
length := len(haystack)

89
text/gstr/gstr_replace.go Normal file
View File

@ -0,0 +1,89 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr
import (
"github.com/gogf/gf/internal/utils"
"strings"
)
// Replace returns a copy of the string `origin`
// in which string `search` replaced by `replace` case-sensitively.
func Replace(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
return strings.Replace(origin, search, replace, n)
}
// ReplaceI returns a copy of the string `origin`
// in which string `search` replaced by `replace` case-insensitively.
func ReplaceI(origin, search, replace string, count ...int) string {
n := -1
if len(count) > 0 {
n = count[0]
}
if n == 0 {
return origin
}
var (
length = len(search)
searchLower = strings.ToLower(search)
)
for {
originLower := strings.ToLower(origin)
if pos := strings.Index(originLower, searchLower); pos != -1 {
origin = origin[:pos] + replace + origin[pos+length:]
if n--; n == 0 {
break
}
} else {
break
}
}
return origin
}
// ReplaceByArray returns a copy of `origin`,
// which is replaced by a slice in order, case-sensitively.
func ReplaceByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = Replace(origin, array[i], array[i+1])
}
return origin
}
// ReplaceIByArray returns a copy of `origin`,
// which is replaced by a slice in order, case-insensitively.
func ReplaceIByArray(origin string, array []string) string {
for i := 0; i < len(array); i += 2 {
if i+1 >= len(array) {
break
}
origin = ReplaceI(origin, array[i], array[i+1])
}
return origin
}
// ReplaceByMap returns a copy of `origin`,
// which is replaced by a map in unordered way, case-sensitively.
func ReplaceByMap(origin string, replaces map[string]string) string {
return utils.ReplaceByMap(origin, replaces)
}
// ReplaceIByMap returns a copy of `origin`,
// which is replaced by a map in unordered way, case-insensitively.
func ReplaceIByMap(origin string, replaces map[string]string) string {
for k, v := range replaces {
origin = ReplaceI(origin, k, v)
}
return origin
}

52
text/gstr/gstr_str.go Normal file
View File

@ -0,0 +1,52 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr
import "strings"
// Str returns part of `haystack` string starting from and including
// the first occurrence of `needle` to the end of `haystack`.
// See http://php.net/manual/en/function.strstr.php.
func Str(haystack string, needle string) string {
if needle == "" {
return ""
}
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex {
return ""
}
return haystack[pos+len([]byte(needle))-1:]
}
// StrEx returns part of `haystack` string starting from and excluding
// the first occurrence of `needle` to the end of `haystack`.
func StrEx(haystack string, needle string) string {
if s := Str(haystack, needle); s != "" {
return s[1:]
}
return ""
}
// StrTill returns part of `haystack` string ending to and including
// the first occurrence of `needle` from the start of `haystack`.
func StrTill(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex || pos == 0 {
return ""
}
return haystack[:pos+1]
}
// StrTillEx returns part of `haystack` string ending to and excluding
// the first occurrence of `needle` from the start of `haystack`.
func StrTillEx(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == NotFoundIndex || pos == 0 {
return ""
}
return haystack[:pos]
}

89
text/gstr/gstr_substr.go Normal file
View File

@ -0,0 +1,89 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr
// SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
// The parameter `length` is optional, it uses the length of `str` in default.
func SubStr(str string, start int, length ...int) (substr string) {
strLength := len(str)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= strLength {
start = strLength
}
end := strLength
if len(length) > 0 {
end = start + length[0]
if end < start {
end = strLength
}
}
if end > strLength {
end = strLength
}
return str[start:end]
}
// SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
// SubStrRune considers parameter `str` as unicode string.
// The parameter `length` is optional, it uses the length of `str` in default.
func SubStrRune(str string, start int, length ...int) (substr string) {
// Converting to []rune to support unicode.
var (
runes = []rune(str)
runesLength = len(runes)
)
// Simple border checks.
if start < 0 {
start = 0
}
if start >= runesLength {
start = runesLength
}
end := runesLength
if len(length) > 0 {
end = start + length[0]
if end < start {
end = runesLength
}
}
if end > runesLength {
end = runesLength
}
return string(runes[start:end])
}
// StrLimit returns a portion of string `str` specified by `length` parameters, if the length
// of `str` is greater than `length`, then the `suffix` will be appended to the result string.
func StrLimit(str string, length int, suffix ...string) string {
if len(str) < length {
return str
}
suffixStr := defaultSuffixForStrLimit
if len(suffix) > 0 {
suffixStr = suffix[0]
}
return str[0:length] + suffixStr
}
// StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
// of `str` is greater than `length`, then the `suffix` will be appended to the result string.
// StrLimitRune considers parameter `str` as unicode string.
func StrLimitRune(str string, length int, suffix ...string) string {
runes := []rune(str)
if len(runes) < length {
return str
}
suffixStr := defaultSuffixForStrLimit
if len(suffix) > 0 {
suffixStr = suffix[0]
}
return string(runes[0:length]) + suffixStr
}

View File

@ -17,8 +17,8 @@ func Trim(str string, characterMask ...string) string {
return utils.Trim(str, characterMask...)
}
// TrimStr strips all of the given <cut> string from the beginning and end of a string.
// Note that it does not strips the whitespaces of its beginning or end.
// TrimStr strips all the given <cut> string from the beginning and end of a string.
// Note that it does not strip the whitespaces of its beginning or end.
func TrimStr(str string, cut string, count ...int) string {
return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
}
@ -32,8 +32,8 @@ func TrimLeft(str string, characterMask ...string) string {
return strings.TrimLeft(str, trimChars)
}
// TrimLeftStr strips all of the given <cut> string from the beginning of a string.
// Note that it does not strips the whitespaces of its beginning.
// TrimLeftStr strips all the given <cut> string from the beginning of a string.
// Note that it does not strip the whitespaces of its beginning.
func TrimLeftStr(str string, cut string, count ...int) string {
var (
lenCut = len(cut)
@ -58,8 +58,8 @@ func TrimRight(str string, characterMask ...string) string {
return strings.TrimRight(str, trimChars)
}
// TrimRightStr strips all of the given <cut> string from the end of a string.
// Note that it does not strips the whitespaces of its end.
// TrimRightStr strips all the given <cut> string from the end of a string.
// Note that it does not strip the whitespaces of its end.
func TrimRightStr(str string, cut string, count ...int) string {
var (
lenStr = len(str)