代码拉取完成,页面将自动刷新
package stringx
import (
"errors"
"gitee.com/go-ultra/gou/core/lang"
)
var (
// ErrInvalidStartPosition is an error that indicates the start position is invalid.
ErrInvalidStartPosition = errors.New("start position is invalid")
// ErrInvalidStopPosition is an error that indicates the stop position is invalid.
ErrInvalidStopPosition = errors.New("stop position is invalid")
)
// Contains checks if str is in list.
func Contains(list []string, str string) bool {
for _, each := range list {
if each == str {
return true
}
}
return false
}
// Filter filters chars from s with given filter function.
func Filter(s string, filter func(r rune) bool) string {
var n int
chars := []rune(s)
for i, x := range chars {
if n < i {
chars[n] = x
}
if !filter(x) {
n++
}
}
return string(chars[:n])
}
// FirstN returns first n runes from s.
func FirstN(s string, n int, ellipsis ...string) string {
var i int
for j := range s {
if i == n {
ret := s[:j]
for _, each := range ellipsis {
ret += each
}
return ret
}
i++
}
return s
}
// HasEmpty checks if there are empty strings in args.
func HasEmpty(args ...string) bool {
for _, arg := range args {
if len(arg) == 0 {
return true
}
}
return false
}
// NotEmpty checks if all strings are not empty in args.
func NotEmpty(args ...string) bool {
return !HasEmpty(args...)
}
// Remove removes given strs from strings.
func Remove(strings []string, strs ...string) []string {
out := append([]string(nil), strings...)
for _, str := range strs {
var n int
for _, v := range out {
if v != str {
out[n] = v
n++
}
}
out = out[:n]
}
return out
}
// Reverse reverses s.
func Reverse(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}
// Substr returns runes between start and stop [start, stop) regardless of the chars are ascii or utf8.
func Substr(str string, start, stop int) (string, error) {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
return "", ErrInvalidStartPosition
}
if stop < 0 || stop > length {
return "", ErrInvalidStopPosition
}
return string(rs[start:stop]), nil
}
// TakeOne returns valid string if not empty or later one.
func TakeOne(valid, or string) string {
if len(valid) > 0 {
return valid
}
return or
}
// TakeWithPriority returns the first not empty result from fns.
func TakeWithPriority(fns ...func() string) string {
for _, fn := range fns {
val := fn()
if len(val) > 0 {
return val
}
}
return ""
}
// Union merges the strings in first and second.
func Union(first, second []string) []string {
set := make(map[string]lang.PlaceholderType)
for _, each := range first {
set[each] = lang.Placeholder
}
for _, each := range second {
set[each] = lang.Placeholder
}
merged := make([]string, 0, len(set))
for k := range set {
merged = append(merged, k)
}
return merged
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。