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.
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) (str 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.
84 racereadrangepc(unsafe.Pointer(&b[0]),
87 funcPC(slicebytetostring))
90 msanread(unsafe.Pointer(&b[0]), uintptr(l))
93 stringStructOf(&str).str = unsafe.Pointer(&staticbytes[b[0]])
94 stringStructOf(&str).len = 1
99 if buf != nil && len(b) <= len(buf) {
100 p = unsafe.Pointer(buf)
102 p = mallocgc(uintptr(len(b)), nil, false)
104 stringStructOf(&str).str = p
105 stringStructOf(&str).len = len(b)
106 memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
110 // stringDataOnStack reports whether the string's data is
111 // stored on the current goroutine's stack.
112 func stringDataOnStack(s string) bool {
113 ptr := uintptr(stringStructOf(&s).str)
115 return stk.lo <= ptr && ptr < stk.hi
118 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
119 if buf != nil && l <= len(buf) {
121 s = slicebytetostringtmp(b)
128 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
130 // Callers need to ensure that the returned string will not be used after
131 // the calling goroutine modifies the original slice or synchronizes with
132 // another goroutine.
134 // The function is only called when instrumenting
135 // and otherwise intrinsified by the compiler.
137 // Some internal compiler optimizations use this function.
138 // - Used for m[string(k)] lookup where m is a string-keyed map and k is a []byte.
139 // - Used for "<"+string(b)+">" concatenation where b is []byte.
140 // - Used for string(b)=="foo" comparison where b is []byte.
141 func slicebytetostringtmp(b []byte) string {
142 if raceenabled && len(b) > 0 {
143 racereadrangepc(unsafe.Pointer(&b[0]),
146 funcPC(slicebytetostringtmp))
148 if msanenabled && len(b) > 0 {
149 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
151 return *(*string)(unsafe.Pointer(&b))
154 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
156 if buf != nil && len(s) <= len(buf) {
160 b = rawbyteslice(len(s))
166 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
168 // unlike slicerunetostring, no race because strings are immutable.
175 if buf != nil && n <= len(buf) {
176 *buf = [tmpStringBufSize]rune{}
183 for _, r := range s {
190 func slicerunetostring(buf *tmpBuf, a []rune) string {
191 if raceenabled && len(a) > 0 {
192 racereadrangepc(unsafe.Pointer(&a[0]),
193 uintptr(len(a))*unsafe.Sizeof(a[0]),
195 funcPC(slicerunetostring))
197 if msanenabled && len(a) > 0 {
198 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
202 for _, r := range a {
203 size1 += encoderune(dum[:], r)
205 s, b := rawstringtmp(buf, size1+3)
207 for _, r := range a {
212 size2 += encoderune(b[size2:], r)
217 type stringStruct struct {
222 // Variant with *byte pointer type for DWARF debugging.
223 type stringStructDWARF struct {
228 func stringStructOf(sp *string) *stringStruct {
229 return (*stringStruct)(unsafe.Pointer(sp))
232 func intstring(buf *[4]byte, v int64) (s string) {
233 if v >= 0 && v < runeSelf {
234 stringStructOf(&s).str = unsafe.Pointer(&staticbytes[v])
235 stringStructOf(&s).len = 1
242 s = slicebytetostringtmp(b)
246 if int64(rune(v)) != v {
249 n := encoderune(b, rune(v))
253 // rawstring allocates storage for a new string. The returned
254 // string and byte slice both refer to the same storage.
255 // The storage is not zeroed. Callers should use
256 // b to set the string contents and then drop b.
257 func rawstring(size int) (s string, b []byte) {
258 p := mallocgc(uintptr(size), nil, false)
260 stringStructOf(&s).str = p
261 stringStructOf(&s).len = size
263 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
268 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
269 func rawbyteslice(size int) (b []byte) {
270 cap := roundupsize(uintptr(size))
271 p := mallocgc(cap, nil, false)
272 if cap != uintptr(size) {
273 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
276 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
280 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
281 func rawruneslice(size int) (b []rune) {
282 if uintptr(size) > maxAlloc/4 {
283 throw("out of memory")
285 mem := roundupsize(uintptr(size) * 4)
286 p := mallocgc(mem, nil, false)
287 if mem != uintptr(size)*4 {
288 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
291 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
296 func gobytes(p *byte, n int) (b []byte) {
298 return make([]byte, 0)
301 if n < 0 || uintptr(n) > maxAlloc {
302 panic(errorString("gobytes: length out of range"))
305 bp := mallocgc(uintptr(n), nil, false)
306 memmove(bp, unsafe.Pointer(p), uintptr(n))
308 *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
312 func gostring(p *byte) string {
318 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
322 func gostringn(p *byte, l int) string {
327 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
331 func index(s, t string) int {
335 for i := 0; i < len(s); i++ {
336 if s[i] == t[0] && hasPrefix(s[i:], t) {
343 func contains(s, t string) bool {
344 return index(s, t) >= 0
347 func hasPrefix(s, prefix string) bool {
348 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
353 maxInt = int(maxUint >> 1)
356 // atoi parses an int from a string s.
357 // The bool result reports whether s is a number
358 // representable by a value of type int.
359 func atoi(s string) (int, bool) {
371 for i := 0; i < len(s); i++ {
373 if c < '0' || c > '9' {
381 un1 := un + uint(c) - '0'
389 if !neg && un > uint(maxInt) {
392 if neg && un > uint(maxInt)+1 {
404 // atoi32 is like atoi but for integers
405 // that fit into an int32.
406 func atoi32(s string) (int32, bool) {
407 if n, ok := atoi(s); n == int(int32(n)) {
414 func findnull(s *byte) int {
419 // Avoid IndexByteString on Plan 9 because it uses SSE instructions
420 // on x86 machines, and those are classified as floating point instructions,
421 // which are illegal in a note handler.
423 p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
431 // pageSize is the unit we scan at a time looking for NULL.
432 // It must be the minimum page size for any architecture Go
433 // runs on. It's okay (just a minor performance loss) if the
434 // actual system page size is larger than this value.
435 const pageSize = 4096
438 ptr := unsafe.Pointer(s)
439 // IndexByteString uses wide reads, so we need to be careful
440 // with page boundaries. Call IndexByteString on
441 // [ptr, endOfPage) interval.
442 safeLen := int(pageSize - uintptr(ptr)%pageSize)
445 t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
446 // Check one page at a time.
447 if i := bytealg.IndexByteString(t, 0); i != -1 {
451 ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
457 func findnullw(s *uint16) int {
461 p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
470 func gostringnocopy(str *byte) string {
471 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
472 s := *(*string)(unsafe.Pointer(&ss))
476 func gostringw(strw *uint16) string {
478 str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
480 for i := 0; str[i] != 0; i++ {
481 n1 += encoderune(buf[:], rune(str[i]))
483 s, b := rawstring(n1 + 4)
485 for i := 0; str[i] != 0; i++ {
490 n2 += encoderune(b[n2:], rune(str[i]))
492 b[n2] = 0 // for luck