123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- // Copyright 2018 gf Author(https://github.com/gogf/gf). 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"
- // 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)
- 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)
- if pos == -1 {
- return -1
- }
- 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.
- // It returns -1, if not found.
- 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
- }
- 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.
- // It returns -1, if not found.
- 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
- }
- 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.
- // It returns -1, if not found.
- 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
- }
- 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]))
- }
|