gf/text/gstr/gstr_pos.go

141 lines
3.6 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-02-02 14:22:32 +08:00
//
// 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.
2019-02-02 14:22:32 +08:00
package gstr
import "strings"
2021-08-30 14:29:04 +08:00
// Pos returns the position of the first occurrence of `needle`
// in `haystack` from `startOffset`, case-sensitively.
2019-04-06 21:31:01 +08:00
// It returns -1, if not found.
2019-06-19 09:06:52 +08:00
func Pos(haystack, needle string, startOffset ...int) int {
length := len(haystack)
offset := 0
if len(startOffset) > 0 {
offset = startOffset[0]
}
if length == 0 || offset > length || -offset > length {
return -1
}
if offset < 0 {
offset += length
}
pos := strings.Index(haystack[offset:], needle)
2021-02-08 17:57:21 +08:00
if pos == NotFoundIndex {
return NotFoundIndex
2019-06-19 09:06:52 +08:00
}
return pos + offset
}
// PosRune acts like function Pos but considers `haystack` and `needle` as unicode string.
func PosRune(haystack, needle string, startOffset ...int) int {
pos := Pos(haystack, needle, startOffset...)
if pos < 3 {
return pos
}
return len([]rune(haystack[:pos]))
}
// PosI returns the position of the first occurrence of `needle`
// in `haystack` from `startOffset`, case-insensitively.
2019-04-06 21:31:01 +08:00
// It returns -1, if not found.
2019-06-19 09:06:52 +08:00
func PosI(haystack, needle string, startOffset ...int) int {
length := len(haystack)
offset := 0
if len(startOffset) > 0 {
offset = startOffset[0]
}
if length == 0 || offset > length || -offset > length {
return -1
}
2019-06-19 09:06:52 +08:00
if offset < 0 {
offset += length
}
pos := strings.Index(strings.ToLower(haystack[offset:]), strings.ToLower(needle))
if pos == -1 {
return -1
}
return pos + offset
}
// PosIRune acts like function PosI but considers `haystack` and `needle` as unicode string.
func PosIRune(haystack, needle string, startOffset ...int) int {
pos := PosI(haystack, needle, startOffset...)
if pos < 3 {
return pos
}
return len([]rune(haystack[:pos]))
}
// PosR returns the position of the last occurrence of `needle`
// in `haystack` from `startOffset`, case-sensitively.
2019-04-06 21:31:01 +08:00
// It returns -1, if not found.
2019-06-19 09:06:52 +08:00
func PosR(haystack, needle string, startOffset ...int) int {
offset := 0
if len(startOffset) > 0 {
offset = startOffset[0]
}
pos, length := 0, len(haystack)
if length == 0 || offset > length || -offset > length {
return -1
}
2019-06-19 09:06:52 +08:00
if offset < 0 {
haystack = haystack[:offset+length+1]
} else {
haystack = haystack[offset:]
}
pos = strings.LastIndex(haystack, needle)
if offset > 0 && pos != -1 {
pos += offset
}
return pos
}
// PosRRune acts like function PosR but considers `haystack` and `needle` as unicode string.
func PosRRune(haystack, needle string, startOffset ...int) int {
pos := PosR(haystack, needle, startOffset...)
if pos < 3 {
return pos
}
return len([]rune(haystack[:pos]))
}
// PosRI returns the position of the last occurrence of `needle`
// in `haystack` from `startOffset`, case-insensitively.
2019-04-06 21:31:01 +08:00
// It returns -1, if not found.
2019-06-19 09:06:52 +08:00
func PosRI(haystack, needle string, startOffset ...int) int {
offset := 0
if len(startOffset) > 0 {
offset = startOffset[0]
}
pos, length := 0, len(haystack)
if length == 0 || offset > length || -offset > length {
return -1
}
2019-06-19 09:06:52 +08:00
if offset < 0 {
haystack = haystack[:offset+length+1]
} else {
haystack = haystack[offset:]
}
pos = strings.LastIndex(strings.ToLower(haystack), strings.ToLower(needle))
if offset > 0 && pos != -1 {
pos += offset
}
return pos
}
// PosRIRune acts like function PosRI but considers `haystack` and `needle` as unicode string.
func PosRIRune(haystack, needle string, startOffset ...int) int {
pos := PosRI(haystack, needle, startOffset...)
if pos < 3 {
return pos
}
return len([]rune(haystack[:pos]))
}