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)
57 func concatstring2(buf *tmpBuf, a [2]string) string {
58 return concatstrings(buf, a[:])
61 func concatstring3(buf *tmpBuf, a [3]string) string {
62 return concatstrings(buf, a[:])
65 func concatstring4(buf *tmpBuf, a [4]string) string {
66 return concatstrings(buf, a[:])
69 func concatstring5(buf *tmpBuf, a [5]string) string {
70 return concatstrings(buf, a[:])
73 // Buf is a fixed-size buffer for the result,
74 // it is not nil if the result does not escape.
75 func slicebytetostring(buf *tmpBuf, b []byte) string {
78 // Turns out to be a relatively common case.
79 // Consider that you want to parse out data between parens in "foo()bar",
80 // you find the indices and convert the subslice to string.
83 if raceenabled && l > 0 {
84 racereadrangepc(unsafe.Pointer(&b[0]),
86 getcallerpc(unsafe.Pointer(&buf)),
87 funcPC(slicebytetostring))
89 if msanenabled && l > 0 {
90 msanread(unsafe.Pointer(&b[0]), uintptr(l))
92 s, c := rawstringtmp(buf, l)
97 // stringDataOnStack reports whether the string's data is
98 // stored on the current goroutine's stack.
99 func stringDataOnStack(s string) bool {
100 ptr := uintptr(stringStructOf(&s).str)
102 return stk.lo <= ptr && ptr < stk.hi
105 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
106 if buf != nil && l <= len(buf) {
108 s = slicebytetostringtmp(b)
115 func slicebytetostringtmp(b []byte) string {
116 // Return a "string" referring to the actual []byte bytes.
117 // This is only for use by internal compiler optimizations
118 // that know that the string form will be discarded before
119 // the calling goroutine could possibly modify the original
120 // slice or synchronize with another goroutine.
121 // First such case is a m[string(k)] lookup where
122 // m is a string-keyed map and k is a []byte.
123 // Second such case is "<"+string(b)+">" concatenation where b is []byte.
124 // Third such case is string(b)=="foo" comparison where b is []byte.
126 if raceenabled && len(b) > 0 {
127 racereadrangepc(unsafe.Pointer(&b[0]),
129 getcallerpc(unsafe.Pointer(&b)),
130 funcPC(slicebytetostringtmp))
132 if msanenabled && len(b) > 0 {
133 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
135 return *(*string)(unsafe.Pointer(&b))
138 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
140 if buf != nil && len(s) <= len(buf) {
144 b = rawbyteslice(len(s))
150 func stringtoslicebytetmp(s string) []byte {
151 // Return a slice referring to the actual string bytes.
152 // This is only for use by internal compiler optimizations
153 // that know that the slice won't be mutated.
154 // The only such case today is:
155 // for i, c := range []byte(str)
157 str := stringStructOf(&s)
158 ret := slice{array: str.str, len: str.len, cap: str.len}
159 return *(*[]byte)(unsafe.Pointer(&ret))
162 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
164 // unlike slicerunetostring, no race because strings are immutable.
168 _, k := charntorune(s)
173 if buf != nil && n <= len(buf) {
174 *buf = [tmpStringBufSize]rune{}
181 r, k := charntorune(t)
189 func slicerunetostring(buf *tmpBuf, a []rune) string {
190 if raceenabled && len(a) > 0 {
191 racereadrangepc(unsafe.Pointer(&a[0]),
192 uintptr(len(a))*unsafe.Sizeof(a[0]),
193 getcallerpc(unsafe.Pointer(&buf)),
194 funcPC(slicerunetostring))
196 if msanenabled && len(a) > 0 {
197 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
201 for _, r := range a {
202 size1 += runetochar(dum[:], r)
204 s, b := rawstringtmp(buf, size1+3)
206 for _, r := range a {
211 size2 += runetochar(b[size2:], r)
216 type stringStruct struct {
221 // Variant with *byte pointer type for DWARF debugging.
222 type stringStructDWARF struct {
227 func stringStructOf(sp *string) *stringStruct {
228 return (*stringStruct)(unsafe.Pointer(sp))
231 func intstring(buf *[4]byte, v int64) string {
236 s = slicebytetostringtmp(b)
240 if int64(rune(v)) != v {
243 n := runetochar(b, rune(v))
247 // stringiter returns the index of the next
248 // rune after the rune that starts at s[k].
249 func stringiter(s string, k int) int {
251 // 0 is end of iteration
261 _, n := charntorune(s[k:])
265 // stringiter2 returns the rune that starts at s[k]
266 // and the index where the next rune starts.
267 func stringiter2(s string, k int) (int, rune) {
269 // 0 is end of iteration
275 return k + 1, rune(c)
279 r, n := charntorune(s[k:])
283 // rawstring allocates storage for a new string. The returned
284 // string and byte slice both refer to the same storage.
285 // The storage is not zeroed. Callers should use
286 // b to set the string contents and then drop b.
287 func rawstring(size int) (s string, b []byte) {
288 p := mallocgc(uintptr(size), nil, false)
290 stringStructOf(&s).str = p
291 stringStructOf(&s).len = size
293 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
297 if uintptr(size) <= ms || atomic.Casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), ms, uintptr(size)) {
303 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
304 func rawbyteslice(size int) (b []byte) {
305 cap := roundupsize(uintptr(size))
306 p := mallocgc(cap, nil, false)
307 if cap != uintptr(size) {
308 memclr(add(p, uintptr(size)), cap-uintptr(size))
311 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
315 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
316 func rawruneslice(size int) (b []rune) {
317 if uintptr(size) > _MaxMem/4 {
318 throw("out of memory")
320 mem := roundupsize(uintptr(size) * 4)
321 p := mallocgc(mem, nil, false)
322 if mem != uintptr(size)*4 {
323 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
326 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
331 func gobytes(p *byte, n int) []byte {
333 return make([]byte, 0)
336 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
340 func gostring(p *byte) string {
346 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
350 func gostringn(p *byte, l int) string {
355 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
359 func index(s, t string) int {
363 for i := 0; i < len(s); i++ {
364 if s[i] == t[0] && hasprefix(s[i:], t) {
371 func contains(s, t string) bool {
372 return index(s, t) >= 0
375 func hasprefix(s, t string) bool {
376 return len(s) >= len(t) && s[:len(t)] == t
379 func atoi(s string) int {
381 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
382 n = n*10 + int(s[0]) - '0'
389 func findnull(s *byte) int {
393 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
401 func findnullw(s *uint16) int {
405 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
413 var maxstring uintptr = 256 // a hint for print
416 func gostringnocopy(str *byte) string {
417 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
418 s := *(*string)(unsafe.Pointer(&ss))
421 if uintptr(len(s)) <= ms || atomic.Casuintptr(&maxstring, ms, uintptr(len(s))) {
428 func gostringw(strw *uint16) string {
430 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
432 for i := 0; str[i] != 0; i++ {
433 n1 += runetochar(buf[:], rune(str[i]))
435 s, b := rawstring(n1 + 4)
437 for i := 0; str[i] != 0; i++ {
442 n2 += runetochar(b[n2:], rune(str[i]))
444 b[n2] = 0 // for luck