github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_pos.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gstr
     8  
     9  import "strings"
    10  
    11  // Pos returns the position of the first occurrence of `needle`
    12  // in `haystack` from `startOffset`, case-sensitively.
    13  // It returns -1, if not found.
    14  func Pos(haystack, needle string, startOffset ...int) int {
    15  	length := len(haystack)
    16  	offset := 0
    17  	if len(startOffset) > 0 {
    18  		offset = startOffset[0]
    19  	}
    20  	if length == 0 || offset > length || -offset > length {
    21  		return -1
    22  	}
    23  	if offset < 0 {
    24  		offset += length
    25  	}
    26  	pos := strings.Index(haystack[offset:], needle)
    27  	if pos == NotFoundIndex {
    28  		return NotFoundIndex
    29  	}
    30  	return pos + offset
    31  }
    32  
    33  // PosRune acts like function Pos but considers `haystack` and `needle` as unicode string.
    34  func PosRune(haystack, needle string, startOffset ...int) int {
    35  	pos := Pos(haystack, needle, startOffset...)
    36  	if pos < 3 {
    37  		return pos
    38  	}
    39  	return len([]rune(haystack[:pos]))
    40  }
    41  
    42  // PosI returns the position of the first occurrence of `needle`
    43  // in `haystack` from `startOffset`, case-insensitively.
    44  // It returns -1, if not found.
    45  func PosI(haystack, needle string, startOffset ...int) int {
    46  	length := len(haystack)
    47  	offset := 0
    48  	if len(startOffset) > 0 {
    49  		offset = startOffset[0]
    50  	}
    51  	if length == 0 || offset > length || -offset > length {
    52  		return -1
    53  	}
    54  
    55  	if offset < 0 {
    56  		offset += length
    57  	}
    58  	pos := strings.Index(strings.ToLower(haystack[offset:]), strings.ToLower(needle))
    59  	if pos == -1 {
    60  		return -1
    61  	}
    62  	return pos + offset
    63  }
    64  
    65  // PosIRune acts like function PosI but considers `haystack` and `needle` as unicode string.
    66  func PosIRune(haystack, needle string, startOffset ...int) int {
    67  	pos := PosI(haystack, needle, startOffset...)
    68  	if pos < 3 {
    69  		return pos
    70  	}
    71  	return len([]rune(haystack[:pos]))
    72  }
    73  
    74  // PosR returns the position of the last occurrence of `needle`
    75  // in `haystack` from `startOffset`, case-sensitively.
    76  // It returns -1, if not found.
    77  func PosR(haystack, needle string, startOffset ...int) int {
    78  	offset := 0
    79  	if len(startOffset) > 0 {
    80  		offset = startOffset[0]
    81  	}
    82  	pos, length := 0, len(haystack)
    83  	if length == 0 || offset > length || -offset > length {
    84  		return -1
    85  	}
    86  
    87  	if offset < 0 {
    88  		haystack = haystack[:offset+length+1]
    89  	} else {
    90  		haystack = haystack[offset:]
    91  	}
    92  	pos = strings.LastIndex(haystack, needle)
    93  	if offset > 0 && pos != -1 {
    94  		pos += offset
    95  	}
    96  	return pos
    97  }
    98  
    99  // PosRRune acts like function PosR but considers `haystack` and `needle` as unicode string.
   100  func PosRRune(haystack, needle string, startOffset ...int) int {
   101  	pos := PosR(haystack, needle, startOffset...)
   102  	if pos < 3 {
   103  		return pos
   104  	}
   105  	return len([]rune(haystack[:pos]))
   106  }
   107  
   108  // PosRI returns the position of the last occurrence of `needle`
   109  // in `haystack` from `startOffset`, case-insensitively.
   110  // It returns -1, if not found.
   111  func PosRI(haystack, needle string, startOffset ...int) int {
   112  	offset := 0
   113  	if len(startOffset) > 0 {
   114  		offset = startOffset[0]
   115  	}
   116  	pos, length := 0, len(haystack)
   117  	if length == 0 || offset > length || -offset > length {
   118  		return -1
   119  	}
   120  
   121  	if offset < 0 {
   122  		haystack = haystack[:offset+length+1]
   123  	} else {
   124  		haystack = haystack[offset:]
   125  	}
   126  	pos = strings.LastIndex(strings.ToLower(haystack), strings.ToLower(needle))
   127  	if offset > 0 && pos != -1 {
   128  		pos += offset
   129  	}
   130  	return pos
   131  }
   132  
   133  // PosRIRune acts like function PosRI but considers `haystack` and `needle` as unicode string.
   134  func PosRIRune(haystack, needle string, startOffset ...int) int {
   135  	pos := PosRI(haystack, needle, startOffset...)
   136  	if pos < 3 {
   137  		return pos
   138  	}
   139  	return len([]rune(haystack[:pos]))
   140  }