]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/string.go
runtime: remove maxstring
[gostls13.git] / src / runtime / string.go
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime
6
7 import "unsafe"
8
9 // The constant is known to the compiler.
10 // There is no fundamental theory behind this number.
11 const tmpStringBufSize = 32
12
13 type tmpBuf [tmpStringBufSize]byte
14
15 // concatstrings implements a Go string concatenation x+y+z+...
16 // The operands are passed in the slice a.
17 // If buf != nil, the compiler has determined that the result does not
18 // escape the calling function, so the string data can be stored in buf
19 // if small enough.
20 func concatstrings(buf *tmpBuf, a []string) string {
21         idx := 0
22         l := 0
23         count := 0
24         for i, x := range a {
25                 n := len(x)
26                 if n == 0 {
27                         continue
28                 }
29                 if l+n < l {
30                         throw("string concatenation too long")
31                 }
32                 l += n
33                 count++
34                 idx = i
35         }
36         if count == 0 {
37                 return ""
38         }
39
40         // If there is just one string and either it is not on the stack
41         // or our result does not escape the calling frame (buf != nil),
42         // then we can return that string directly.
43         if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
44                 return a[idx]
45         }
46         s, b := rawstringtmp(buf, l)
47         for _, x := range a {
48                 copy(b, x)
49                 b = b[len(x):]
50         }
51         return s
52 }
53
54 func concatstring2(buf *tmpBuf, a [2]string) string {
55         return concatstrings(buf, a[:])
56 }
57
58 func concatstring3(buf *tmpBuf, a [3]string) string {
59         return concatstrings(buf, a[:])
60 }
61
62 func concatstring4(buf *tmpBuf, a [4]string) string {
63         return concatstrings(buf, a[:])
64 }
65
66 func concatstring5(buf *tmpBuf, a [5]string) string {
67         return concatstrings(buf, a[:])
68 }
69
70 // Buf is a fixed-size buffer for the result,
71 // it is not nil if the result does not escape.
72 func slicebytetostring(buf *tmpBuf, b []byte) string {
73         l := len(b)
74         if l == 0 {
75                 // Turns out to be a relatively common case.
76                 // Consider that you want to parse out data between parens in "foo()bar",
77                 // you find the indices and convert the subslice to string.
78                 return ""
79         }
80         if raceenabled && l > 0 {
81                 racereadrangepc(unsafe.Pointer(&b[0]),
82                         uintptr(l),
83                         getcallerpc(unsafe.Pointer(&buf)),
84                         funcPC(slicebytetostring))
85         }
86         if msanenabled && l > 0 {
87                 msanread(unsafe.Pointer(&b[0]), uintptr(l))
88         }
89         s, c := rawstringtmp(buf, l)
90         copy(c, b)
91         return s
92 }
93
94 // stringDataOnStack reports whether the string's data is
95 // stored on the current goroutine's stack.
96 func stringDataOnStack(s string) bool {
97         ptr := uintptr(stringStructOf(&s).str)
98         stk := getg().stack
99         return stk.lo <= ptr && ptr < stk.hi
100 }
101
102 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
103         if buf != nil && l <= len(buf) {
104                 b = buf[:l]
105                 s = slicebytetostringtmp(b)
106         } else {
107                 s, b = rawstring(l)
108         }
109         return
110 }
111
112 func slicebytetostringtmp(b []byte) string {
113         // Return a "string" referring to the actual []byte bytes.
114         // This is only for use by internal compiler optimizations
115         // that know that the string form will be discarded before
116         // the calling goroutine could possibly modify the original
117         // slice or synchronize with another goroutine.
118         // First such case is a m[string(k)] lookup where
119         // m is a string-keyed map and k is a []byte.
120         // Second such case is "<"+string(b)+">" concatenation where b is []byte.
121         // Third such case is string(b)=="foo" comparison where b is []byte.
122
123         if raceenabled && len(b) > 0 {
124                 racereadrangepc(unsafe.Pointer(&b[0]),
125                         uintptr(len(b)),
126                         getcallerpc(unsafe.Pointer(&b)),
127                         funcPC(slicebytetostringtmp))
128         }
129         if msanenabled && len(b) > 0 {
130                 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
131         }
132         return *(*string)(unsafe.Pointer(&b))
133 }
134
135 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
136         var b []byte
137         if buf != nil && len(s) <= len(buf) {
138                 *buf = tmpBuf{}
139                 b = buf[:len(s)]
140         } else {
141                 b = rawbyteslice(len(s))
142         }
143         copy(b, s)
144         return b
145 }
146
147 func stringtoslicebytetmp(s string) []byte {
148         // Return a slice referring to the actual string bytes.
149         // This is only for use by internal compiler optimizations
150         // that know that the slice won't be mutated.
151         // The only such case today is:
152         // for i, c := range []byte(str)
153
154         str := stringStructOf(&s)
155         ret := slice{array: str.str, len: str.len, cap: str.len}
156         return *(*[]byte)(unsafe.Pointer(&ret))
157 }
158
159 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
160         // two passes.
161         // unlike slicerunetostring, no race because strings are immutable.
162         n := 0
163         for range s {
164                 n++
165         }
166
167         var a []rune
168         if buf != nil && n <= len(buf) {
169                 *buf = [tmpStringBufSize]rune{}
170                 a = buf[:n]
171         } else {
172                 a = rawruneslice(n)
173         }
174
175         n = 0
176         for _, r := range s {
177                 a[n] = r
178                 n++
179         }
180         return a
181 }
182
183 func slicerunetostring(buf *tmpBuf, a []rune) string {
184         if raceenabled && len(a) > 0 {
185                 racereadrangepc(unsafe.Pointer(&a[0]),
186                         uintptr(len(a))*unsafe.Sizeof(a[0]),
187                         getcallerpc(unsafe.Pointer(&buf)),
188                         funcPC(slicerunetostring))
189         }
190         if msanenabled && len(a) > 0 {
191                 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
192         }
193         var dum [4]byte
194         size1 := 0
195         for _, r := range a {
196                 size1 += runetochar(dum[:], r)
197         }
198         s, b := rawstringtmp(buf, size1+3)
199         size2 := 0
200         for _, r := range a {
201                 // check for race
202                 if size2 >= size1 {
203                         break
204                 }
205                 size2 += runetochar(b[size2:], r)
206         }
207         return s[:size2]
208 }
209
210 type stringStruct struct {
211         str unsafe.Pointer
212         len int
213 }
214
215 // Variant with *byte pointer type for DWARF debugging.
216 type stringStructDWARF struct {
217         str *byte
218         len int
219 }
220
221 func stringStructOf(sp *string) *stringStruct {
222         return (*stringStruct)(unsafe.Pointer(sp))
223 }
224
225 func intstring(buf *[4]byte, v int64) string {
226         var s string
227         var b []byte
228         if buf != nil {
229                 b = buf[:]
230                 s = slicebytetostringtmp(b)
231         } else {
232                 s, b = rawstring(4)
233         }
234         if int64(rune(v)) != v {
235                 v = runeerror
236         }
237         n := runetochar(b, rune(v))
238         return s[:n]
239 }
240
241 // rawstring allocates storage for a new string. The returned
242 // string and byte slice both refer to the same storage.
243 // The storage is not zeroed. Callers should use
244 // b to set the string contents and then drop b.
245 func rawstring(size int) (s string, b []byte) {
246         p := mallocgc(uintptr(size), nil, false)
247
248         stringStructOf(&s).str = p
249         stringStructOf(&s).len = size
250
251         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
252
253         return
254 }
255
256 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
257 func rawbyteslice(size int) (b []byte) {
258         cap := roundupsize(uintptr(size))
259         p := mallocgc(cap, nil, false)
260         if cap != uintptr(size) {
261                 memclr(add(p, uintptr(size)), cap-uintptr(size))
262         }
263
264         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
265         return
266 }
267
268 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
269 func rawruneslice(size int) (b []rune) {
270         if uintptr(size) > _MaxMem/4 {
271                 throw("out of memory")
272         }
273         mem := roundupsize(uintptr(size) * 4)
274         p := mallocgc(mem, nil, false)
275         if mem != uintptr(size)*4 {
276                 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
277         }
278
279         *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
280         return
281 }
282
283 // used by cmd/cgo
284 func gobytes(p *byte, n int) []byte {
285         if n == 0 {
286                 return make([]byte, 0)
287         }
288         x := make([]byte, n)
289         memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
290         return x
291 }
292
293 func gostring(p *byte) string {
294         l := findnull(p)
295         if l == 0 {
296                 return ""
297         }
298         s, b := rawstring(l)
299         memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
300         return s
301 }
302
303 func gostringn(p *byte, l int) string {
304         if l == 0 {
305                 return ""
306         }
307         s, b := rawstring(l)
308         memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
309         return s
310 }
311
312 func index(s, t string) int {
313         if len(t) == 0 {
314                 return 0
315         }
316         for i := 0; i < len(s); i++ {
317                 if s[i] == t[0] && hasprefix(s[i:], t) {
318                         return i
319                 }
320         }
321         return -1
322 }
323
324 func contains(s, t string) bool {
325         return index(s, t) >= 0
326 }
327
328 func hasprefix(s, t string) bool {
329         return len(s) >= len(t) && s[:len(t)] == t
330 }
331
332 func atoi(s string) int {
333         n := 0
334         for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
335                 n = n*10 + int(s[0]) - '0'
336                 s = s[1:]
337         }
338         return n
339 }
340
341 //go:nosplit
342 func findnull(s *byte) int {
343         if s == nil {
344                 return 0
345         }
346         p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
347         l := 0
348         for p[l] != 0 {
349                 l++
350         }
351         return l
352 }
353
354 func findnullw(s *uint16) int {
355         if s == nil {
356                 return 0
357         }
358         p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
359         l := 0
360         for p[l] != 0 {
361                 l++
362         }
363         return l
364 }
365
366 //go:nosplit
367 func gostringnocopy(str *byte) string {
368         ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
369         s := *(*string)(unsafe.Pointer(&ss))
370         return s
371 }
372
373 func gostringw(strw *uint16) string {
374         var buf [8]byte
375         str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
376         n1 := 0
377         for i := 0; str[i] != 0; i++ {
378                 n1 += runetochar(buf[:], rune(str[i]))
379         }
380         s, b := rawstring(n1 + 4)
381         n2 := 0
382         for i := 0; str[i] != 0; i++ {
383                 // check for race
384                 if n2 >= n1 {
385                         break
386                 }
387                 n2 += runetochar(b[n2:], rune(str[i]))
388         }
389         b[n2] = 0 // for luck
390         return s[:n2]
391 }