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.
8 "runtime/internal/atomic"
12 // The constant is known to the compiler.
13 // There is no fundamental theory behind this number.
14 const tmpStringBufSize = 32
16 type tmpBuf [tmpStringBufSize]byte
18 // concatstrings implements a Go string concatenation x+y+z+...
19 // The operands are passed in the slice a.
20 // If buf != nil, the compiler has determined that the result does not
21 // escape the calling function, so the string data can be stored in buf
23 func concatstrings(buf *tmpBuf, a []string) string {
33 throw("string concatenation too long")
43 // If there is just one string and either it is not on the stack
44 // or our result does not escape the calling frame (buf != nil),
45 // then we can return that string directly.
46 if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
49 s, b := rawstringtmp(buf, l)
58 func concatstring2(buf *tmpBuf, a [2]string) string {
59 return concatstrings(buf, a[:])
62 func concatstring3(buf *tmpBuf, a [3]string) string {
63 return concatstrings(buf, a[:])
66 func concatstring4(buf *tmpBuf, a [4]string) string {
67 return concatstrings(buf, a[:])
70 func concatstring5(buf *tmpBuf, a [5]string) string {
71 return concatstrings(buf, a[:])
74 // Buf is a fixed-size buffer for the result,
75 // it is not nil if the result does not escape.
76 func slicebytetostring(buf *tmpBuf, b []byte) string {
79 // Turns out to be a relatively common case.
80 // Consider that you want to parse out data between parens in "foo()bar",
81 // you find the indices and convert the subslice to string.
84 if raceenabled && l > 0 {
85 racereadrangepc(unsafe.Pointer(&b[0]),
87 getcallerpc(unsafe.Pointer(&buf)),
88 funcPC(slicebytetostring))
90 if msanenabled && l > 0 {
91 msanread(unsafe.Pointer(&b[0]), uintptr(l))
93 s, c := rawstringtmp(buf, l)
98 // stringDataOnStack reports whether the string's data is
99 // stored on the current goroutine's stack.
100 func stringDataOnStack(s string) bool {
101 ptr := uintptr(stringStructOf(&s).str)
103 return stk.lo <= ptr && ptr < stk.hi
106 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
107 if buf != nil && l <= len(buf) {
109 s = slicebytetostringtmp(b)
116 func slicebytetostringtmp(b []byte) string {
117 // Return a "string" referring to the actual []byte bytes.
118 // This is only for use by internal compiler optimizations
119 // that know that the string form will be discarded before
120 // the calling goroutine could possibly modify the original
121 // slice or synchronize with another goroutine.
122 // First such case is a m[string(k)] lookup where
123 // m is a string-keyed map and k is a []byte.
124 // Second such case is "<"+string(b)+">" concatenation where b is []byte.
125 // Third such case is string(b)=="foo" comparison where b is []byte.
127 if raceenabled && len(b) > 0 {
128 racereadrangepc(unsafe.Pointer(&b[0]),
130 getcallerpc(unsafe.Pointer(&b)),
131 funcPC(slicebytetostringtmp))
133 if msanenabled && len(b) > 0 {
134 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
136 return *(*string)(unsafe.Pointer(&b))
139 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
141 if buf != nil && len(s) <= len(buf) {
145 b = rawbyteslice(len(s))
151 func stringtoslicebytetmp(s string) []byte {
152 // Return a slice referring to the actual string bytes.
153 // This is only for use by internal compiler optimizations
154 // that know that the slice won't be mutated.
155 // The only such case today is:
156 // for i, c := range []byte(str)
158 str := stringStructOf(&s)
159 ret := slice{array: str.str, len: str.len, cap: str.len}
160 return *(*[]byte)(unsafe.Pointer(&ret))
163 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
165 // unlike slicerunetostring, no race because strings are immutable.
169 _, k := charntorune(s)
174 if buf != nil && n <= len(buf) {
175 *buf = [tmpStringBufSize]rune{}
182 r, k := charntorune(t)
190 func slicerunetostring(buf *tmpBuf, a []rune) string {
191 if raceenabled && len(a) > 0 {
192 racereadrangepc(unsafe.Pointer(&a[0]),
193 uintptr(len(a))*unsafe.Sizeof(a[0]),
194 getcallerpc(unsafe.Pointer(&buf)),
195 funcPC(slicerunetostring))
197 if msanenabled && len(a) > 0 {
198 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
202 for _, r := range a {
203 size1 += runetochar(dum[:], r)
205 s, b := rawstringtmp(buf, size1+3)
207 for _, r := range a {
212 size2 += runetochar(b[size2:], r)
217 type stringStruct struct {
222 // Variant with *byte pointer type for DWARF debugging.
223 type stringStructDWARF struct {
228 func stringStructOf(sp *string) *stringStruct {
229 return (*stringStruct)(unsafe.Pointer(sp))
232 func intstring(buf *[4]byte, v int64) string {
237 s = slicebytetostringtmp(b)
241 if int64(rune(v)) != v {
244 n := runetochar(b, rune(v))
248 // stringiter returns the index of the next
249 // rune after the rune that starts at s[k].
250 func stringiter(s string, k int) int {
252 // 0 is end of iteration
262 _, n := charntorune(s[k:])
266 // stringiter2 returns the rune that starts at s[k]
267 // and the index where the next rune starts.
268 func stringiter2(s string, k int) (int, rune) {
270 // 0 is end of iteration
276 return k + 1, rune(c)
280 r, n := charntorune(s[k:])
284 // rawstring allocates storage for a new string. The returned
285 // string and byte slice both refer to the same storage.
286 // The storage is not zeroed. Callers should use
287 // b to set the string contents and then drop b.
288 func rawstring(size int) (s string, b []byte) {
289 p := mallocgc(uintptr(size), nil, false)
291 stringStructOf(&s).str = p
292 stringStructOf(&s).len = size
294 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
298 if uintptr(size) <= ms || atomic.Casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), ms, uintptr(size)) {
304 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
305 func rawbyteslice(size int) (b []byte) {
306 cap := roundupsize(uintptr(size))
307 p := mallocgc(cap, nil, false)
308 if cap != uintptr(size) {
309 memclr(add(p, uintptr(size)), cap-uintptr(size))
312 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
316 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
317 func rawruneslice(size int) (b []rune) {
318 if uintptr(size) > _MaxMem/4 {
319 throw("out of memory")
321 mem := roundupsize(uintptr(size) * 4)
322 p := mallocgc(mem, nil, false)
323 if mem != uintptr(size)*4 {
324 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
327 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
332 func gobytes(p *byte, n int) []byte {
334 return make([]byte, 0)
337 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
341 func gostring(p *byte) string {
347 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
351 func gostringn(p *byte, l int) string {
356 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
360 func index(s, t string) int {
364 for i := 0; i < len(s); i++ {
365 if s[i] == t[0] && hasprefix(s[i:], t) {
372 func contains(s, t string) bool {
373 return index(s, t) >= 0
376 func hasprefix(s, t string) bool {
377 return len(s) >= len(t) && s[:len(t)] == t
380 func atoi(s string) int {
382 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
383 n = n*10 + int(s[0]) - '0'
390 func findnull(s *byte) int {
394 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
402 func findnullw(s *uint16) int {
406 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
414 var maxstring uintptr = 256 // a hint for print
417 func gostringnocopy(str *byte) string {
418 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
419 s := *(*string)(unsafe.Pointer(&ss))
422 if uintptr(len(s)) <= ms || atomic.Casuintptr(&maxstring, ms, uintptr(len(s))) {
429 func gostringw(strw *uint16) string {
431 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
433 for i := 0; str[i] != 0; i++ {
434 n1 += runetochar(buf[:], rune(str[i]))
436 s, b := rawstring(n1 + 4)
438 for i := 0; str[i] != 0; i++ {
443 n2 += runetochar(b[n2:], rune(str[i]))
445 b[n2] = 0 // for luck