cuelang.org/go@v0.10.1/pkg/strings/strings.go (about) 1 // Copyright 2020 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Copyright 2018 The Go Authors. All rights reserved. 16 // Use of this source code is governed by a BSD-style 17 // license that can be found in the LICENSE file. 18 19 // Originally generated with: go run qgo.go -exclude=Rune$,Func$,^Map$,Special$,EqualFold,Byte,Title$,ToValidUTF8,All$ extract strings 20 21 package strings 22 23 import "strings" 24 25 // Compare returns an integer comparing two strings lexicographically. 26 // The result will be 0 if a==b, -1 if a < b, and +1 if a > b. 27 // 28 // Compare is included only for symmetry with package bytes. 29 // It is usually clearer and always faster to use the built-in 30 // string comparison operators ==, <, >, and so on. 31 func Compare(a, b string) int { 32 return strings.Compare(a, b) 33 } 34 35 // Count counts the number of non-overlapping instances of substr in s. 36 // If substr is an empty string, Count returns 1 + the number of Unicode code points in s. 37 func Count(s, substr string) int { 38 return strings.Count(s, substr) 39 } 40 41 // Contains reports whether substr is within s. 42 func Contains(s, substr string) bool { 43 return strings.Contains(s, substr) 44 } 45 46 // ContainsAny reports whether any Unicode code points in chars are within s. 47 func ContainsAny(s, chars string) bool { 48 return strings.ContainsAny(s, chars) 49 } 50 51 // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s. 52 func LastIndex(s, substr string) int { 53 return strings.LastIndex(s, substr) 54 } 55 56 // IndexAny returns the index of the first instance of any Unicode code point 57 // from chars in s, or -1 if no Unicode code point from chars is present in s. 58 func IndexAny(s, chars string) int { 59 return strings.IndexAny(s, chars) 60 } 61 62 // LastIndexAny returns the index of the last instance of any Unicode code 63 // point from chars in s, or -1 if no Unicode code point from chars is 64 // present in s. 65 func LastIndexAny(s, chars string) int { 66 return strings.LastIndexAny(s, chars) 67 } 68 69 // SplitN slices s into substrings separated by sep and returns a slice of 70 // the substrings between those separators. 71 // 72 // The count determines the number of substrings to return: 73 // 74 // n > 0: at most n substrings; the last substring will be the unsplit remainder. 75 // n == 0: the result is nil (zero substrings) 76 // n < 0: all substrings 77 // 78 // Edge cases for s and sep (for example, empty strings) are handled 79 // as described in the documentation for Split. 80 func SplitN(s, sep string, n int) []string { 81 return strings.SplitN(s, sep, n) 82 } 83 84 // SplitAfterN slices s into substrings after each instance of sep and 85 // returns a slice of those substrings. 86 // 87 // The count determines the number of substrings to return: 88 // 89 // n > 0: at most n substrings; the last substring will be the unsplit remainder. 90 // n == 0: the result is nil (zero substrings) 91 // n < 0: all substrings 92 // 93 // Edge cases for s and sep (for example, empty strings) are handled 94 // as described in the documentation for SplitAfter. 95 func SplitAfterN(s, sep string, n int) []string { 96 return strings.SplitAfterN(s, sep, n) 97 } 98 99 // Split slices s into all substrings separated by sep and returns a slice of 100 // the substrings between those separators. 101 // 102 // If s does not contain sep and sep is not empty, Split returns a 103 // slice of length 1 whose only element is s. 104 // 105 // If sep is empty, Split splits after each UTF-8 sequence. If both s 106 // and sep are empty, Split returns an empty slice. 107 // 108 // It is equivalent to SplitN with a count of -1. 109 func Split(s, sep string) []string { 110 return strings.Split(s, sep) 111 } 112 113 // SplitAfter slices s into all substrings after each instance of sep and 114 // returns a slice of those substrings. 115 // 116 // If s does not contain sep and sep is not empty, SplitAfter returns 117 // a slice of length 1 whose only element is s. 118 // 119 // If sep is empty, SplitAfter splits after each UTF-8 sequence. If 120 // both s and sep are empty, SplitAfter returns an empty slice. 121 // 122 // It is equivalent to SplitAfterN with a count of -1. 123 func SplitAfter(s, sep string) []string { 124 return strings.SplitAfter(s, sep) 125 } 126 127 // Fields splits the string s around each instance of one or more consecutive white space 128 // characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an 129 // empty slice if s contains only white space. 130 func Fields(s string) []string { 131 return strings.Fields(s) 132 } 133 134 // Join concatenates the elements of its first argument to create a single string. The separator 135 // string sep is placed between elements in the resulting string. 136 func Join(elems []string, sep string) string { 137 return strings.Join(elems, sep) 138 } 139 140 // HasPrefix tests whether the string s begins with prefix. 141 func HasPrefix(s, prefix string) bool { 142 return strings.HasPrefix(s, prefix) 143 } 144 145 // HasSuffix tests whether the string s ends with suffix. 146 func HasSuffix(s, suffix string) bool { 147 return strings.HasSuffix(s, suffix) 148 } 149 150 // Repeat returns a new string consisting of count copies of the string s. 151 // 152 // It panics if count is negative or if 153 // the result of (len(s) * count) overflows. 154 func Repeat(s string, count int) string { 155 return strings.Repeat(s, count) 156 } 157 158 // ToUpper returns s with all Unicode letters mapped to their upper case. 159 func ToUpper(s string) string { 160 return strings.ToUpper(s) 161 } 162 163 // ToLower returns s with all Unicode letters mapped to their lower case. 164 func ToLower(s string) string { 165 return strings.ToLower(s) 166 } 167 168 // Trim returns a slice of the string s with all leading and 169 // trailing Unicode code points contained in cutset removed. 170 func Trim(s, cutset string) string { 171 return strings.Trim(s, cutset) 172 } 173 174 // TrimLeft returns a slice of the string s with all leading 175 // Unicode code points contained in cutset removed. 176 // 177 // To remove a prefix, use TrimPrefix instead. 178 func TrimLeft(s, cutset string) string { 179 return strings.TrimLeft(s, cutset) 180 } 181 182 // TrimRight returns a slice of the string s, with all trailing 183 // Unicode code points contained in cutset removed. 184 // 185 // To remove a suffix, use TrimSuffix instead. 186 func TrimRight(s, cutset string) string { 187 return strings.TrimRight(s, cutset) 188 } 189 190 // TrimSpace returns a slice of the string s, with all leading 191 // and trailing white space removed, as defined by Unicode. 192 func TrimSpace(s string) string { 193 return strings.TrimSpace(s) 194 } 195 196 // TrimPrefix returns s without the provided leading prefix string. 197 // If s doesn't start with prefix, s is returned unchanged. 198 func TrimPrefix(s, prefix string) string { 199 return strings.TrimPrefix(s, prefix) 200 } 201 202 // TrimSuffix returns s without the provided trailing suffix string. 203 // If s doesn't end with suffix, s is returned unchanged. 204 func TrimSuffix(s, suffix string) string { 205 return strings.TrimSuffix(s, suffix) 206 } 207 208 // Replace returns a copy of the string s with the first n 209 // non-overlapping instances of old replaced by new. 210 // If old is empty, it matches at the beginning of the string 211 // and after each UTF-8 sequence, yielding up to k+1 replacements 212 // for a k-rune string. 213 // If n < 0, there is no limit on the number of replacements. 214 func Replace(s, old, new string, n int) string { 215 return strings.Replace(s, old, new, n) 216 } 217 218 // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s. 219 func Index(s, substr string) int { 220 return strings.Index(s, substr) 221 }