Bumps the minor-and-patch group with 8 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.8` | `2.2.9` | | [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) | `3.4.0` | `3.4.1` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.21` | `1.8.22` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.2` | `2.14.3` | | [github.com/onsi/ginkgo/v2](https://github.com/onsi/ginkgo) | `2.31.0` | `2.32.0` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.42.0` | `1.42.1` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.17.1` | `1.18.2` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.81.1` | `1.82.0` | Updates `github.com/gookit/config/v2` from 2.2.8 to 2.2.9 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.8...v2.2.9) Updates `github.com/jellydator/ttlcache/v3` from 3.4.0 to 3.4.1 - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.4.0...v3.4.1) Updates `github.com/kovidgoyal/imaging` from 1.8.21 to 1.8.22 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.21...v1.8.22) Updates `github.com/nats-io/nats-server/v2` from 2.14.2 to 2.14.3 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.2...v2.14.3) Updates `github.com/onsi/ginkgo/v2` from 2.31.0 to 2.32.0 - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/ginkgo/compare/v2.31.0...v2.32.0) Updates `github.com/onsi/gomega` from 1.42.0 to 1.42.1 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.42.0...v1.42.1) Updates `github.com/open-policy-agent/opa` from 1.17.1 to 1.18.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.17.1...v1.18.2) Updates `google.golang.org/grpc` from 1.81.1 to 1.82.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.81.1...v1.82.0) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.9 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/jellydator/ttlcache/v3 dependency-version: 3.4.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.22 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/ginkgo/v2 dependency-version: 2.32.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.42.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.18.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/grpc dependency-version: 1.82.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman Perekhod <2403905@gmail.com>
201 lines
4.8 KiB
Go
201 lines
4.8 KiB
Go
package strutil
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// BeforeFirst get substring before first sep.
|
|
func BeforeFirst(s, sep string) string {
|
|
if i := strings.Index(s, sep); i >= 0 {
|
|
return s[:i]
|
|
}
|
|
return s
|
|
}
|
|
|
|
// AfterFirst get substring after first sep.
|
|
func AfterFirst(s, sep string) string {
|
|
if i := strings.Index(s, sep); i >= 0 {
|
|
return s[i+len(sep):]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// BeforeLast get substring before last sep.
|
|
func BeforeLast(s, sep string) string {
|
|
if i := strings.LastIndex(s, sep); i >= 0 {
|
|
return s[:i]
|
|
}
|
|
return s
|
|
}
|
|
|
|
// AfterLast get substring after last sep.
|
|
func AfterLast(s, sep string) string {
|
|
if i := strings.LastIndex(s, sep); i >= 0 {
|
|
return s[i+len(sep):]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
/*************************************************************
|
|
* String split operation
|
|
*************************************************************/
|
|
|
|
// Cut alias of the strings.Cut
|
|
func Cut(s, sep string) (before string, after string, found bool) {
|
|
if i := strings.Index(s, sep); i >= 0 {
|
|
return s[:i], s[i+len(sep):], true
|
|
}
|
|
return s, "", false
|
|
}
|
|
|
|
// QuietCut always returns two substring.
|
|
func QuietCut(s, sep string) (before string, after string) {
|
|
before, after, _ = Cut(s, sep)
|
|
return
|
|
}
|
|
|
|
// MustCut always returns two substring.
|
|
func MustCut(s, sep string) (before string, after string) {
|
|
var ok bool
|
|
before, after, ok = Cut(s, sep)
|
|
if !ok {
|
|
panic("cannot split input string to two nodes")
|
|
}
|
|
return
|
|
}
|
|
|
|
// TrimCut always returns two substring and trim space for items.
|
|
func TrimCut(s, sep string) (string, string) {
|
|
before, after, _ := Cut(s, sep)
|
|
return strings.TrimSpace(before), strings.TrimSpace(after)
|
|
}
|
|
|
|
// SplitKV split string to key and value.
|
|
func SplitKV(s, sep string) (string, string) { return TrimCut(s, sep) }
|
|
|
|
// SplitValid string to slice. will trim each item and filter empty string node.
|
|
func SplitValid(s, sep string) (ss []string) { return Split(s, sep) }
|
|
|
|
// Split string to slice. will trim each item and filter empty string node.
|
|
func Split(s, sep string) (ss []string) {
|
|
if s = strings.TrimSpace(s); s == "" {
|
|
return
|
|
}
|
|
|
|
for _, val := range strings.Split(s, sep) {
|
|
if val = strings.TrimSpace(val); val != "" {
|
|
ss = append(ss, val)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SplitNValid string to slice. will filter empty string node.
|
|
func SplitNValid(s, sep string, n int) (ss []string) { return SplitN(s, sep, n) }
|
|
|
|
// SplitN string to slice. will filter empty string node.
|
|
func SplitN(s, sep string, n int) (ss []string) {
|
|
if s = strings.TrimSpace(s); s == "" {
|
|
return
|
|
}
|
|
|
|
rawList := strings.Split(s, sep)
|
|
for i, val := range rawList {
|
|
if val = strings.TrimSpace(val); val != "" {
|
|
if len(ss) == n-1 {
|
|
ss = append(ss, strings.TrimSpace(strings.Join(rawList[i:], sep)))
|
|
break
|
|
}
|
|
|
|
ss = append(ss, val)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// SplitTrimmed split string to slice.
|
|
// will trim space for each node, but not filter empty
|
|
func SplitTrimmed(s, sep string) (ss []string) {
|
|
if s = strings.TrimSpace(s); s == "" {
|
|
return
|
|
}
|
|
|
|
for _, val := range strings.Split(s, sep) {
|
|
ss = append(ss, strings.TrimSpace(val))
|
|
}
|
|
return
|
|
}
|
|
|
|
// SplitNTrimmed split string to slice.
|
|
// will trim space for each node, but not filter empty
|
|
func SplitNTrimmed(s, sep string, n int) (ss []string) {
|
|
if s = strings.TrimSpace(s); s == "" {
|
|
return
|
|
}
|
|
|
|
for _, val := range strings.SplitN(s, sep, n) {
|
|
ss = append(ss, strings.TrimSpace(val))
|
|
}
|
|
return
|
|
}
|
|
|
|
// 根据空白字符(空格,TAB,换行等)分隔字符串
|
|
var whitespaceRegexp = regexp.MustCompile(`\s+`)
|
|
|
|
// SplitByWhitespace Separate strings by whitespace characters (space, TAB, newline, etc.)
|
|
func SplitByWhitespace(s string) []string {
|
|
return whitespaceRegexp.Split(s, -1)
|
|
}
|
|
|
|
// Substr for a string. NOTE: strLn := len(runes)
|
|
// if length <= 0, return pos to end.
|
|
func Substr(s string, pos, length int) string {
|
|
runes := []rune(s)
|
|
strLn := len(runes)
|
|
|
|
// pos is too large
|
|
if pos >= strLn {
|
|
return ""
|
|
}
|
|
|
|
stopIdx := pos + length
|
|
if length == 0 || stopIdx > strLn {
|
|
stopIdx = strLn
|
|
} else if length < 0 {
|
|
stopIdx = strLn + length
|
|
}
|
|
|
|
return string(runes[pos:stopIdx])
|
|
}
|
|
|
|
// SplitInlineComment for an inline text string. default is strict mode.
|
|
func SplitInlineComment(val string, strict ...bool) (string, string) {
|
|
// strict check: must with a space
|
|
if len(strict) == 0 || strict[0] {
|
|
if pos := strings.Index(val, " #"); pos > -1 {
|
|
return strings.TrimRight(val[0:pos], " "), val[pos+1:]
|
|
}
|
|
if pos := strings.Index(val, " //"); pos > -1 {
|
|
return strings.TrimRight(val[0:pos], " "), val[pos+1:]
|
|
}
|
|
return val, ""
|
|
}
|
|
|
|
if pos := strings.IndexByte(val, '#'); pos > -1 {
|
|
return strings.TrimRight(val[0:pos], " "), val[pos:]
|
|
}
|
|
if pos := strings.Index(val, "//"); pos > -1 {
|
|
return strings.TrimRight(val[0:pos], " "), val[pos:]
|
|
}
|
|
return val, ""
|
|
}
|
|
|
|
// FirstLine from command output
|
|
func FirstLine(output string) string {
|
|
if i := strings.IndexByte(output, '\n'); i >= 0 {
|
|
return output[0:i]
|
|
}
|
|
return output
|
|
}
|