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.
11 // The constant is known to the compiler.
12 // There is no fundamental theory behind this number.
13 const tmpStringBufSize = 32
15 type tmpBuf [tmpStringBufSize]byte
17 // concatstrings implements a Go string concatenation x+y+z+...
18 // The operands are passed in the slice a.
19 // If buf != nil, the compiler has determined that the result does not
20 // escape the calling function, so the string data can be stored in buf
22 func concatstrings(buf *tmpBuf, a []string) string {
32 throw("string concatenation too long")
42 // If there is just one string and either it is not on the stack
43 // or our result does not escape the calling frame (buf != nil),
44 // then we can return that string directly.
45 if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
48 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(&b)),
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) {
143 b = rawbyteslice(len(s))
149 func stringtoslicebytetmp(s string) []byte {
150 // Return a slice referring to the actual string bytes.
151 // This is only for use by internal compiler optimizations
152 // that know that the slice won't be mutated.
153 // The only such case today is:
154 // for i, c := range []byte(str)
156 str := stringStructOf(&s)
157 ret := slice{array: unsafe.Pointer(str.str), len: str.len, cap: str.len}
158 return *(*[]byte)(unsafe.Pointer(&ret))
161 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
163 // unlike slicerunetostring, no race because strings are immutable.
167 _, k := charntorune(s)
172 if buf != nil && n <= len(buf) {
179 r, k := charntorune(t)
187 func slicerunetostring(buf *tmpBuf, a []rune) string {
188 if raceenabled && len(a) > 0 {
189 racereadrangepc(unsafe.Pointer(&a[0]),
190 uintptr(len(a))*unsafe.Sizeof(a[0]),
191 getcallerpc(unsafe.Pointer(&a)),
192 funcPC(slicerunetostring))
194 if msanenabled && len(a) > 0 {
195 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
199 for _, r := range a {
200 size1 += runetochar(dum[:], r)
202 s, b := rawstringtmp(buf, size1+3)
204 for _, r := range a {
209 size2 += runetochar(b[size2:], r)
214 type stringStruct struct {
219 // Variant with *byte pointer type for DWARF debugging.
220 type stringStructDWARF struct {
225 func stringStructOf(sp *string) *stringStruct {
226 return (*stringStruct)(unsafe.Pointer(sp))
229 func intstring(buf *[4]byte, v int64) string {
234 s = slicebytetostringtmp(b)
238 n := runetochar(b, rune(v))
242 // stringiter returns the index of the next
243 // rune after the rune that starts at s[k].
244 func stringiter(s string, k int) int {
246 // 0 is end of iteration
256 _, n := charntorune(s[k:])
260 // stringiter2 returns the rune that starts at s[k]
261 // and the index where the next rune starts.
262 func stringiter2(s string, k int) (int, rune) {
264 // 0 is end of iteration
270 return k + 1, rune(c)
274 r, n := charntorune(s[k:])
278 // rawstring allocates storage for a new string. The returned
279 // string and byte slice both refer to the same storage.
280 // The storage is not zeroed. Callers should use
281 // b to set the string contents and then drop b.
282 func rawstring(size int) (s string, b []byte) {
283 p := mallocgc(uintptr(size), nil, flagNoScan|flagNoZero)
285 stringStructOf(&s).str = p
286 stringStructOf(&s).len = size
288 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
292 if uintptr(size) <= uintptr(ms) || casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), uintptr(ms), uintptr(size)) {
298 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
299 func rawbyteslice(size int) (b []byte) {
300 cap := roundupsize(uintptr(size))
301 p := mallocgc(cap, nil, flagNoScan|flagNoZero)
302 if cap != uintptr(size) {
303 memclr(add(p, uintptr(size)), cap-uintptr(size))
306 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
310 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
311 func rawruneslice(size int) (b []rune) {
312 if uintptr(size) > _MaxMem/4 {
313 throw("out of memory")
315 mem := roundupsize(uintptr(size) * 4)
316 p := mallocgc(mem, nil, flagNoScan|flagNoZero)
317 if mem != uintptr(size)*4 {
318 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
321 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
326 func gobytes(p *byte, n int) []byte {
328 return make([]byte, 0)
331 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
335 func gostring(p *byte) string {
341 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
345 func gostringn(p *byte, l int) string {
350 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
354 func index(s, t string) int {
358 for i := 0; i < len(s); i++ {
359 if s[i] == t[0] && hasprefix(s[i:], t) {
366 func contains(s, t string) bool {
367 return index(s, t) >= 0
370 func hasprefix(s, t string) bool {
371 return len(s) >= len(t) && s[:len(t)] == t
374 func atoi(s string) int {
376 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
377 n = n*10 + int(s[0]) - '0'
384 func findnull(s *byte) int {
388 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
396 func findnullw(s *uint16) int {
400 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
408 var maxstring uintptr = 256 // a hint for print
411 func gostringnocopy(str *byte) string {
412 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
413 s := *(*string)(unsafe.Pointer(&ss))
416 if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) {
423 func gostringw(strw *uint16) string {
425 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
427 for i := 0; str[i] != 0; i++ {
428 n1 += runetochar(buf[:], rune(str[i]))
430 s, b := rawstring(n1 + 4)
432 for i := 0; str[i] != 0; i++ {
437 n2 += runetochar(b[n2:], rune(str[i]))
439 b[n2] = 0 // for luck