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.
9 // The constant is known to the compiler.
10 // There is no fundamental theory behind this number.
11 const tmpStringBufSize = 32
13 type tmpBuf [tmpStringBufSize]byte
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
20 func concatstrings(buf *tmpBuf, a []string) string {
30 throw("string concatenation too long")
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])) {
46 s, b := rawstringtmp(buf, l)
54 func concatstring2(buf *tmpBuf, a [2]string) string {
55 return concatstrings(buf, a[:])
58 func concatstring3(buf *tmpBuf, a [3]string) string {
59 return concatstrings(buf, a[:])
62 func concatstring4(buf *tmpBuf, a [4]string) string {
63 return concatstrings(buf, a[:])
66 func concatstring5(buf *tmpBuf, a [5]string) string {
67 return concatstrings(buf, a[:])
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 {
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.
80 if raceenabled && l > 0 {
81 racereadrangepc(unsafe.Pointer(&b[0]),
83 getcallerpc(unsafe.Pointer(&buf)),
84 funcPC(slicebytetostring))
86 if msanenabled && l > 0 {
87 msanread(unsafe.Pointer(&b[0]), uintptr(l))
89 s, c := rawstringtmp(buf, l)
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)
99 return stk.lo <= ptr && ptr < stk.hi
102 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
103 if buf != nil && l <= len(buf) {
105 s = slicebytetostringtmp(b)
112 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
114 // Callers need to ensure that the returned string will not be used after
115 // the calling goroutine modifies the original slice or synchronizes with
116 // another goroutine.
118 // The function is only called when instrumenting
119 // and otherwise intrinsified by the compiler.
121 // Some internal compiler optimizations use this function.
122 // - Used for m[string(k)] lookup where m is a string-keyed map and k is a []byte.
123 // - Used for "<"+string(b)+">" concatenation where b is []byte.
124 // - Used for string(b)=="foo" comparison where b is []byte.
125 func slicebytetostringtmp(b []byte) string {
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 stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
152 // unlike slicerunetostring, no race because strings are immutable.
159 if buf != nil && n <= len(buf) {
160 *buf = [tmpStringBufSize]rune{}
167 for _, r := range s {
174 func slicerunetostring(buf *tmpBuf, a []rune) string {
175 if raceenabled && len(a) > 0 {
176 racereadrangepc(unsafe.Pointer(&a[0]),
177 uintptr(len(a))*unsafe.Sizeof(a[0]),
178 getcallerpc(unsafe.Pointer(&buf)),
179 funcPC(slicerunetostring))
181 if msanenabled && len(a) > 0 {
182 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
186 for _, r := range a {
187 size1 += encoderune(dum[:], r)
189 s, b := rawstringtmp(buf, size1+3)
191 for _, r := range a {
196 size2 += encoderune(b[size2:], r)
201 type stringStruct struct {
206 // Variant with *byte pointer type for DWARF debugging.
207 type stringStructDWARF struct {
212 func stringStructOf(sp *string) *stringStruct {
213 return (*stringStruct)(unsafe.Pointer(sp))
216 func intstring(buf *[4]byte, v int64) string {
221 s = slicebytetostringtmp(b)
225 if int64(rune(v)) != v {
228 n := encoderune(b, rune(v))
232 // rawstring allocates storage for a new string. The returned
233 // string and byte slice both refer to the same storage.
234 // The storage is not zeroed. Callers should use
235 // b to set the string contents and then drop b.
236 func rawstring(size int) (s string, b []byte) {
237 p := mallocgc(uintptr(size), nil, false)
239 stringStructOf(&s).str = p
240 stringStructOf(&s).len = size
242 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
247 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
248 func rawbyteslice(size int) (b []byte) {
249 cap := roundupsize(uintptr(size))
250 p := mallocgc(cap, nil, false)
251 if cap != uintptr(size) {
252 memclr(add(p, uintptr(size)), cap-uintptr(size))
255 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
259 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
260 func rawruneslice(size int) (b []rune) {
261 if uintptr(size) > _MaxMem/4 {
262 throw("out of memory")
264 mem := roundupsize(uintptr(size) * 4)
265 p := mallocgc(mem, nil, false)
266 if mem != uintptr(size)*4 {
267 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
270 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
275 func gobytes(p *byte, n int) []byte {
277 return make([]byte, 0)
280 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
284 func gostring(p *byte) string {
290 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
294 func gostringn(p *byte, l int) string {
299 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
303 func index(s, t string) int {
307 for i := 0; i < len(s); i++ {
308 if s[i] == t[0] && hasprefix(s[i:], t) {
315 func contains(s, t string) bool {
316 return index(s, t) >= 0
319 func hasprefix(s, t string) bool {
320 return len(s) >= len(t) && s[:len(t)] == t
323 func atoi(s string) int {
325 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
326 n = n*10 + int(s[0]) - '0'
333 func findnull(s *byte) int {
337 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
345 func findnullw(s *uint16) int {
349 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
358 func gostringnocopy(str *byte) string {
359 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
360 s := *(*string)(unsafe.Pointer(&ss))
364 func gostringw(strw *uint16) string {
366 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
368 for i := 0; str[i] != 0; i++ {
369 n1 += encoderune(buf[:], rune(str[i]))
371 s, b := rawstring(n1 + 4)
373 for i := 0; str[i] != 0; i++ {
378 n2 += encoderune(b[n2:], rune(str[i]))
380 b[n2] = 0 // for luck