blob: 45c7b2072e54f4aeda9d4efcdedc4868c5ae656c [file] [log] [blame]
Marcel van Lohuizenb5dc1922018-12-11 11:49:57 +01001// Copyright 2018 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//go:generate qgo -exclude=Rune$,Func$,^Map$,Special$,EqualFold,Byte,Title$ extract strings
20
21package strings
22
23import "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.
31func 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.
37func Count(s, substr string) int {
38 return strings.Count(s, substr)
39}
40
41// Contains reports whether substr is within s.
42func 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.
47func 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.
52func 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.
58func 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.
65func 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// n > 0: at most n substrings; the last substring will be the unsplit remainder.
74// n == 0: the result is nil (zero substrings)
75// n < 0: all substrings
76//
77// Edge cases for s and sep (for example, empty strings) are handled
78// as described in the documentation for Split.
79func SplitN(s, sep string, n int) []string {
80 return strings.SplitN(s, sep, n)
81}
82
83// SplitAfterN slices s into substrings after each instance of sep and
84// returns a slice of those substrings.
85//
86// The count determines the number of substrings to return:
87// n > 0: at most n substrings; the last substring will be the unsplit remainder.
88// n == 0: the result is nil (zero substrings)
89// n < 0: all substrings
90//
91// Edge cases for s and sep (for example, empty strings) are handled
92// as described in the documentation for SplitAfter.
93func SplitAfterN(s, sep string, n int) []string {
94 return strings.SplitAfterN(s, sep, n)
95}
96
97// Split slices s into all substrings separated by sep and returns a slice of
98// the substrings between those separators.
99//
100// If s does not contain sep and sep is not empty, Split returns a
101// slice of length 1 whose only element is s.
102//
103// If sep is empty, Split splits after each UTF-8 sequence. If both s
104// and sep are empty, Split returns an empty slice.
105//
106// It is equivalent to SplitN with a count of -1.
107func Split(s, sep string) []string {
108 return strings.Split(s, sep)
109}
110
111// SplitAfter slices s into all substrings after each instance of sep and
112// returns a slice of those substrings.
113//
114// If s does not contain sep and sep is not empty, SplitAfter returns
115// a slice of length 1 whose only element is s.
116//
117// If sep is empty, SplitAfter splits after each UTF-8 sequence. If
118// both s and sep are empty, SplitAfter returns an empty slice.
119//
120// It is equivalent to SplitAfterN with a count of -1.
121func SplitAfter(s, sep string) []string {
122 return strings.SplitAfter(s, sep)
123}
124
125// Fields splits the string s around each instance of one or more consecutive white space
126// characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an
127// empty slice if s contains only white space.
128func Fields(s string) []string {
129 return strings.Fields(s)
130}
131
132// Join concatenates the elements of a to create a single string. The separator string
133// sep is placed between elements in the resulting string.
134func Join(a []string, sep string) string {
135 return strings.Join(a, sep)
136}
137
138// HasPrefix tests whether the string s begins with prefix.
139func HasPrefix(s, prefix string) bool {
140 return strings.HasPrefix(s, prefix)
141}
142
143// HasSuffix tests whether the string s ends with suffix.
144func HasSuffix(s, suffix string) bool {
145 return strings.HasSuffix(s, suffix)
146}
147
148// Repeat returns a new string consisting of count copies of the string s.
149//
150// It panics if count is negative or if
151// the result of (len(s) * count) overflows.
152func Repeat(s string, count int) string {
153 return strings.Repeat(s, count)
154}
155
156// ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
157func ToUpper(s string) string {
158 return strings.ToUpper(s)
159}
160
161// ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
162func ToLower(s string) string {
163 return strings.ToLower(s)
164}
165
166// Trim returns a slice of the string s with all leading and
167// trailing Unicode code points contained in cutset removed.
168func Trim(s string, cutset string) string {
169 return strings.Trim(s, cutset)
170}
171
172// TrimLeft returns a slice of the string s with all leading
173// Unicode code points contained in cutset removed.
174//
175// To remove a prefix, use TrimPrefix instead.
176func TrimLeft(s string, cutset string) string {
177 return strings.TrimLeft(s, cutset)
178}
179
180// TrimRight returns a slice of the string s, with all trailing
181// Unicode code points contained in cutset removed.
182//
183// To remove a suffix, use TrimSuffix instead.
184func TrimRight(s string, cutset string) string {
185 return strings.TrimRight(s, cutset)
186}
187
188// TrimSpace returns a slice of the string s, with all leading
189// and trailing white space removed, as defined by Unicode.
190func TrimSpace(s string) string {
191 return strings.TrimSpace(s)
192}
193
194// TrimPrefix returns s without the provided leading prefix string.
195// If s doesn't start with prefix, s is returned unchanged.
196func TrimPrefix(s, prefix string) string {
197 return strings.TrimPrefix(s, prefix)
198}
199
200// TrimSuffix returns s without the provided trailing suffix string.
201// If s doesn't end with suffix, s is returned unchanged.
202func TrimSuffix(s, suffix string) string {
203 return strings.TrimSuffix(s, suffix)
204}
205
206// Replace returns a copy of the string s with the first n
207// non-overlapping instances of old replaced by new.
208// If old is empty, it matches at the beginning of the string
209// and after each UTF-8 sequence, yielding up to k+1 replacements
210// for a k-rune string.
211// If n < 0, there is no limit on the number of replacements.
212func Replace(s, old, new string, n int) string {
213 return strings.Replace(s, old, new, n)
214}
215
216// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
217func Index(s, substr string) int {
218 return strings.Index(s, substr)
219}