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.
14 // The constant is known to the compiler.
15 // There is no fundamental theory behind this number.
16 const tmpStringBufSize = 32
18 type tmpBuf [tmpStringBufSize]byte
20 // concatstrings implements a Go string concatenation x+y+z+...
21 // The operands are passed in the slice a.
22 // If buf != nil, the compiler has determined that the result does not
23 // escape the calling function, so the string data can be stored in buf
25 func concatstrings(buf *tmpBuf, a []string) string {
35 throw("string concatenation too long")
45 // If there is just one string and either it is not on the stack
46 // or our result does not escape the calling frame (buf != nil),
47 // then we can return that string directly.
48 if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
51 s, b := rawstringtmp(buf, l)
59 func concatstring2(buf *tmpBuf, a0, a1 string) string {
60 return concatstrings(buf, []string{a0, a1})
63 func concatstring3(buf *tmpBuf, a0, a1, a2 string) string {
64 return concatstrings(buf, []string{a0, a1, a2})
67 func concatstring4(buf *tmpBuf, a0, a1, a2, a3 string) string {
68 return concatstrings(buf, []string{a0, a1, a2, a3})
71 func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
72 return concatstrings(buf, []string{a0, a1, a2, a3, a4})
75 // slicebytetostring converts a byte slice to a string.
76 // It is inserted by the compiler into generated code.
77 // ptr is a pointer to the first element of the slice;
78 // n is the length of the slice.
79 // Buf is a fixed-size buffer for the result,
80 // it is not nil if the result does not escape.
81 func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
83 // Turns out to be a relatively common case.
84 // Consider that you want to parse out data between parens in "foo()bar",
85 // you find the indices and convert the subslice to string.
89 racereadrangepc(unsafe.Pointer(ptr),
92 abi.FuncPCABIInternal(slicebytetostring))
95 msanread(unsafe.Pointer(ptr), uintptr(n))
98 asanread(unsafe.Pointer(ptr), uintptr(n))
101 p := unsafe.Pointer(&staticuint64s[*ptr])
102 if goarch.BigEndian {
105 stringStructOf(&str).str = p
106 stringStructOf(&str).len = 1
111 if buf != nil && n <= len(buf) {
112 p = unsafe.Pointer(buf)
114 p = mallocgc(uintptr(n), nil, false)
116 stringStructOf(&str).str = p
117 stringStructOf(&str).len = n
118 memmove(p, unsafe.Pointer(ptr), uintptr(n))
122 // stringDataOnStack reports whether the string's data is
123 // stored on the current goroutine's stack.
124 func stringDataOnStack(s string) bool {
125 ptr := uintptr(stringStructOf(&s).str)
127 return stk.lo <= ptr && ptr < stk.hi
130 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
131 if buf != nil && l <= len(buf) {
133 s = slicebytetostringtmp(&b[0], len(b))
140 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
142 // Callers need to ensure that the returned string will not be used after
143 // the calling goroutine modifies the original slice or synchronizes with
144 // another goroutine.
146 // The function is only called when instrumenting
147 // and otherwise intrinsified by the compiler.
149 // Some internal compiler optimizations use this function.
150 // - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
151 // where k is []byte, T1 to Tn is a nesting of struct and array literals.
152 // - Used for "<"+string(b)+">" concatenation where b is []byte.
153 // - Used for string(b)=="foo" comparison where b is []byte.
154 func slicebytetostringtmp(ptr *byte, n int) (str string) {
155 if raceenabled && n > 0 {
156 racereadrangepc(unsafe.Pointer(ptr),
159 abi.FuncPCABIInternal(slicebytetostringtmp))
161 if msanenabled && n > 0 {
162 msanread(unsafe.Pointer(ptr), uintptr(n))
164 if asanenabled && n > 0 {
165 asanread(unsafe.Pointer(ptr), uintptr(n))
167 stringStructOf(&str).str = unsafe.Pointer(ptr)
168 stringStructOf(&str).len = n
172 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
174 if buf != nil && len(s) <= len(buf) {
178 b = rawbyteslice(len(s))
184 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
186 // unlike slicerunetostring, no race because strings are immutable.
193 if buf != nil && n <= len(buf) {
194 *buf = [tmpStringBufSize]rune{}
201 for _, r := range s {
208 func slicerunetostring(buf *tmpBuf, a []rune) string {
209 if raceenabled && len(a) > 0 {
210 racereadrangepc(unsafe.Pointer(&a[0]),
211 uintptr(len(a))*unsafe.Sizeof(a[0]),
213 abi.FuncPCABIInternal(slicerunetostring))
215 if msanenabled && len(a) > 0 {
216 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
218 if asanenabled && len(a) > 0 {
219 asanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
223 for _, r := range a {
224 size1 += encoderune(dum[:], r)
226 s, b := rawstringtmp(buf, size1+3)
228 for _, r := range a {
233 size2 += encoderune(b[size2:], r)
238 type stringStruct struct {
243 // Variant with *byte pointer type for DWARF debugging.
244 type stringStructDWARF struct {
249 func stringStructOf(sp *string) *stringStruct {
250 return (*stringStruct)(unsafe.Pointer(sp))
253 func intstring(buf *[4]byte, v int64) (s string) {
257 s = slicebytetostringtmp(&b[0], len(b))
261 if int64(rune(v)) != v {
264 n := encoderune(b, rune(v))
268 // rawstring allocates storage for a new string. The returned
269 // string and byte slice both refer to the same storage.
270 // The storage is not zeroed. Callers should use
271 // b to set the string contents and then drop b.
272 func rawstring(size int) (s string, b []byte) {
273 p := mallocgc(uintptr(size), nil, false)
275 stringStructOf(&s).str = p
276 stringStructOf(&s).len = size
278 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
283 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
284 func rawbyteslice(size int) (b []byte) {
285 cap := roundupsize(uintptr(size))
286 p := mallocgc(cap, nil, false)
287 if cap != uintptr(size) {
288 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
291 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
295 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
296 func rawruneslice(size int) (b []rune) {
297 if uintptr(size) > maxAlloc/4 {
298 throw("out of memory")
300 mem := roundupsize(uintptr(size) * 4)
301 p := mallocgc(mem, nil, false)
302 if mem != uintptr(size)*4 {
303 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
306 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
311 func gobytes(p *byte, n int) (b []byte) {
313 return make([]byte, 0)
316 if n < 0 || uintptr(n) > maxAlloc {
317 panic(errorString("gobytes: length out of range"))
320 bp := mallocgc(uintptr(n), nil, false)
321 memmove(bp, unsafe.Pointer(p), uintptr(n))
323 *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
327 // This is exported via linkname to assembly in syscall (for Plan9).
329 //go:linkname gostring
330 func gostring(p *byte) string {
336 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
340 func gostringn(p *byte, l int) string {
345 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
349 func hasPrefix(s, prefix string) bool {
350 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
355 maxInt = int(maxUint >> 1)
358 // atoi parses an int from a string s.
359 // The bool result reports whether s is a number
360 // representable by a value of type int.
361 func atoi(s string) (int, bool) {
373 for i := 0; i < len(s); i++ {
375 if c < '0' || c > '9' {
383 un1 := un + uint(c) - '0'
391 if !neg && un > uint(maxInt) {
394 if neg && un > uint(maxInt)+1 {
406 // atoi32 is like atoi but for integers
407 // that fit into an int32.
408 func atoi32(s string) (int32, bool) {
409 if n, ok := atoi(s); n == int(int32(n)) {
416 func findnull(s *byte) int {
421 // Avoid IndexByteString on Plan 9 because it uses SSE instructions
422 // on x86 machines, and those are classified as floating point instructions,
423 // which are illegal in a note handler.
425 p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
433 // pageSize is the unit we scan at a time looking for NULL.
434 // It must be the minimum page size for any architecture Go
435 // runs on. It's okay (just a minor performance loss) if the
436 // actual system page size is larger than this value.
437 const pageSize = 4096
440 ptr := unsafe.Pointer(s)
441 // IndexByteString uses wide reads, so we need to be careful
442 // with page boundaries. Call IndexByteString on
443 // [ptr, endOfPage) interval.
444 safeLen := int(pageSize - uintptr(ptr)%pageSize)
447 t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
448 // Check one page at a time.
449 if i := bytealg.IndexByteString(t, 0); i != -1 {
453 ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
459 func findnullw(s *uint16) int {
463 p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
472 func gostringnocopy(str *byte) string {
473 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
474 s := *(*string)(unsafe.Pointer(&ss))
478 func gostringw(strw *uint16) string {
480 str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
482 for i := 0; str[i] != 0; i++ {
483 n1 += encoderune(buf[:], rune(str[i]))
485 s, b := rawstring(n1 + 4)
487 for i := 0; str[i] != 0; i++ {
492 n2 += encoderune(b[n2:], rune(str[i]))
494 b[n2] = 0 // for luck