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.
13 // The constant is known to the compiler.
14 // There is no fundamental theory behind this number.
15 const tmpStringBufSize = 32
17 type tmpBuf [tmpStringBufSize]byte
19 // concatstrings implements a Go string concatenation x+y+z+...
20 // The operands are passed in the slice a.
21 // If buf != nil, the compiler has determined that the result does not
22 // escape the calling function, so the string data can be stored in buf
24 func concatstrings(buf *tmpBuf, a []string) string {
34 throw("string concatenation too long")
44 // If there is just one string and either it is not on the stack
45 // or our result does not escape the calling frame (buf != nil),
46 // then we can return that string directly.
47 if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
50 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 // slicebytetostring converts a byte slice to a string.
75 // It is inserted by the compiler into generated code.
76 // ptr is a pointer to the first element of the slice;
77 // n is the length of the slice.
78 // Buf is a fixed-size buffer for the result,
79 // it is not nil if the result does not escape.
80 func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
82 // Turns out to be a relatively common case.
83 // Consider that you want to parse out data between parens in "foo()bar",
84 // you find the indices and convert the subslice to string.
88 racereadrangepc(unsafe.Pointer(ptr),
91 funcPC(slicebytetostring))
94 msanread(unsafe.Pointer(ptr), uintptr(n))
97 p := unsafe.Pointer(&staticuint64s[*ptr])
101 stringStructOf(&str).str = p
102 stringStructOf(&str).len = 1
107 if buf != nil && n <= len(buf) {
108 p = unsafe.Pointer(buf)
110 p = mallocgc(uintptr(n), nil, false)
112 stringStructOf(&str).str = p
113 stringStructOf(&str).len = n
114 memmove(p, unsafe.Pointer(ptr), uintptr(n))
118 // stringDataOnStack reports whether the string's data is
119 // stored on the current goroutine's stack.
120 func stringDataOnStack(s string) bool {
121 ptr := uintptr(stringStructOf(&s).str)
123 return stk.lo <= ptr && ptr < stk.hi
126 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
127 if buf != nil && l <= len(buf) {
129 s = slicebytetostringtmp(&b[0], len(b))
136 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
138 // Callers need to ensure that the returned string will not be used after
139 // the calling goroutine modifies the original slice or synchronizes with
140 // another goroutine.
142 // The function is only called when instrumenting
143 // and otherwise intrinsified by the compiler.
145 // Some internal compiler optimizations use this function.
146 // - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
147 // where k is []byte, T1 to Tn is a nesting of struct and array literals.
148 // - Used for "<"+string(b)+">" concatenation where b is []byte.
149 // - Used for string(b)=="foo" comparison where b is []byte.
150 func slicebytetostringtmp(ptr *byte, n int) (str string) {
151 if raceenabled && n > 0 {
152 racereadrangepc(unsafe.Pointer(ptr),
155 funcPC(slicebytetostringtmp))
157 if msanenabled && n > 0 {
158 msanread(unsafe.Pointer(ptr), uintptr(n))
160 stringStructOf(&str).str = unsafe.Pointer(ptr)
161 stringStructOf(&str).len = n
165 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
167 if buf != nil && len(s) <= len(buf) {
171 b = rawbyteslice(len(s))
177 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
179 // unlike slicerunetostring, no race because strings are immutable.
186 if buf != nil && n <= len(buf) {
187 *buf = [tmpStringBufSize]rune{}
194 for _, r := range s {
201 func slicerunetostring(buf *tmpBuf, a []rune) string {
202 if raceenabled && len(a) > 0 {
203 racereadrangepc(unsafe.Pointer(&a[0]),
204 uintptr(len(a))*unsafe.Sizeof(a[0]),
206 funcPC(slicerunetostring))
208 if msanenabled && len(a) > 0 {
209 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
213 for _, r := range a {
214 size1 += encoderune(dum[:], r)
216 s, b := rawstringtmp(buf, size1+3)
218 for _, r := range a {
223 size2 += encoderune(b[size2:], r)
228 type stringStruct struct {
233 // Variant with *byte pointer type for DWARF debugging.
234 type stringStructDWARF struct {
239 func stringStructOf(sp *string) *stringStruct {
240 return (*stringStruct)(unsafe.Pointer(sp))
243 func intstring(buf *[4]byte, v int64) (s string) {
247 s = slicebytetostringtmp(&b[0], len(b))
251 if int64(rune(v)) != v {
254 n := encoderune(b, rune(v))
258 // rawstring allocates storage for a new string. The returned
259 // string and byte slice both refer to the same storage.
260 // The storage is not zeroed. Callers should use
261 // b to set the string contents and then drop b.
262 func rawstring(size int) (s string, b []byte) {
263 p := mallocgc(uintptr(size), nil, false)
265 stringStructOf(&s).str = p
266 stringStructOf(&s).len = size
268 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
273 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
274 func rawbyteslice(size int) (b []byte) {
275 cap := roundupsize(uintptr(size))
276 p := mallocgc(cap, nil, false)
277 if cap != uintptr(size) {
278 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
281 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
285 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
286 func rawruneslice(size int) (b []rune) {
287 if uintptr(size) > maxAlloc/4 {
288 throw("out of memory")
290 mem := roundupsize(uintptr(size) * 4)
291 p := mallocgc(mem, nil, false)
292 if mem != uintptr(size)*4 {
293 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
296 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
301 func gobytes(p *byte, n int) (b []byte) {
303 return make([]byte, 0)
306 if n < 0 || uintptr(n) > maxAlloc {
307 panic(errorString("gobytes: length out of range"))
310 bp := mallocgc(uintptr(n), nil, false)
311 memmove(bp, unsafe.Pointer(p), uintptr(n))
313 *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
317 // This is exported via linkname to assembly in syscall (for Plan9).
318 //go:linkname gostring
319 func gostring(p *byte) string {
325 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
329 func gostringn(p *byte, l int) string {
334 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
338 func hasPrefix(s, prefix string) bool {
339 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
344 maxInt = int(maxUint >> 1)
347 // atoi parses an int from a string s.
348 // The bool result reports whether s is a number
349 // representable by a value of type int.
350 func atoi(s string) (int, bool) {
362 for i := 0; i < len(s); i++ {
364 if c < '0' || c > '9' {
372 un1 := un + uint(c) - '0'
380 if !neg && un > uint(maxInt) {
383 if neg && un > uint(maxInt)+1 {
395 // atoi32 is like atoi but for integers
396 // that fit into an int32.
397 func atoi32(s string) (int32, bool) {
398 if n, ok := atoi(s); n == int(int32(n)) {
405 func findnull(s *byte) int {
410 // Avoid IndexByteString on Plan 9 because it uses SSE instructions
411 // on x86 machines, and those are classified as floating point instructions,
412 // which are illegal in a note handler.
414 p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
422 // pageSize is the unit we scan at a time looking for NULL.
423 // It must be the minimum page size for any architecture Go
424 // runs on. It's okay (just a minor performance loss) if the
425 // actual system page size is larger than this value.
426 const pageSize = 4096
429 ptr := unsafe.Pointer(s)
430 // IndexByteString uses wide reads, so we need to be careful
431 // with page boundaries. Call IndexByteString on
432 // [ptr, endOfPage) interval.
433 safeLen := int(pageSize - uintptr(ptr)%pageSize)
436 t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
437 // Check one page at a time.
438 if i := bytealg.IndexByteString(t, 0); i != -1 {
442 ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
448 func findnullw(s *uint16) int {
452 p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
461 func gostringnocopy(str *byte) string {
462 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
463 s := *(*string)(unsafe.Pointer(&ss))
467 func gostringw(strw *uint16) string {
469 str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
471 for i := 0; str[i] != 0; i++ {
472 n1 += encoderune(buf[:], rune(str[i]))
474 s, b := rawstring(n1 + 4)
476 for i := 0; str[i] != 0; i++ {
481 n2 += encoderune(b[n2:], rune(str[i]))
483 b[n2] = 0 // for luck