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 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((*stringStruct)(unsafe.Pointer(&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 func slicebytetostringtmp(b []byte) string {
113 // Return a "string" referring to the actual []byte bytes.
114 // This is only for use by internal compiler optimizations
115 // that know that the string form will be discarded before
116 // the calling goroutine could possibly modify the original
117 // slice or synchronize with another goroutine.
118 // First such case is a m[string(k)] lookup where
119 // m is a string-keyed map and k is a []byte.
120 // Second such case is "<"+string(b)+">" concatenation where b is []byte.
121 // Third such case is string(b)=="foo" comparison where b is []byte.
123 if raceenabled && len(b) > 0 {
124 racereadrangepc(unsafe.Pointer(&b[0]),
126 getcallerpc(unsafe.Pointer(&b)),
127 funcPC(slicebytetostringtmp))
129 return *(*string)(unsafe.Pointer(&b))
132 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
134 if buf != nil && len(s) <= len(buf) {
137 b = rawbyteslice(len(s))
143 func stringtoslicebytetmp(s string) []byte {
144 // Return a slice referring to the actual string bytes.
145 // This is only for use by internal compiler optimizations
146 // that know that the slice won't be mutated.
147 // The only such case today is:
148 // for i, c := range []byte(str)
150 str := (*stringStruct)(unsafe.Pointer(&s))
151 ret := slice{array: unsafe.Pointer(str.str), len: str.len, cap: str.len}
152 return *(*[]byte)(unsafe.Pointer(&ret))
155 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
157 // unlike slicerunetostring, no race because strings are immutable.
161 _, k := charntorune(s)
166 if buf != nil && n <= len(buf) {
173 r, k := charntorune(t)
181 func slicerunetostring(buf *tmpBuf, a []rune) string {
182 if raceenabled && len(a) > 0 {
183 racereadrangepc(unsafe.Pointer(&a[0]),
184 uintptr(len(a))*unsafe.Sizeof(a[0]),
185 getcallerpc(unsafe.Pointer(&a)),
186 funcPC(slicerunetostring))
190 for _, r := range a {
191 size1 += runetochar(dum[:], r)
193 s, b := rawstringtmp(buf, size1+3)
195 for _, r := range a {
200 size2 += runetochar(b[size2:], r)
205 type stringStruct struct {
210 func intstring(buf *[4]byte, v int64) string {
215 s = slicebytetostringtmp(b)
219 n := runetochar(b, rune(v))
223 // stringiter returns the index of the next
224 // rune after the rune that starts at s[k].
225 func stringiter(s string, k int) int {
227 // 0 is end of iteration
237 _, n := charntorune(s[k:])
241 // stringiter2 returns the rune that starts at s[k]
242 // and the index where the next rune starts.
243 func stringiter2(s string, k int) (int, rune) {
245 // 0 is end of iteration
251 return k + 1, rune(c)
255 r, n := charntorune(s[k:])
259 // rawstring allocates storage for a new string. The returned
260 // string and byte slice both refer to the same storage.
261 // The storage is not zeroed. Callers should use
262 // b to set the string contents and then drop b.
263 func rawstring(size int) (s string, b []byte) {
264 p := mallocgc(uintptr(size), nil, flagNoScan|flagNoZero)
266 (*stringStruct)(unsafe.Pointer(&s)).str = p
267 (*stringStruct)(unsafe.Pointer(&s)).len = size
269 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
273 if uintptr(size) <= uintptr(ms) || casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), uintptr(ms), uintptr(size)) {
279 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
280 func rawbyteslice(size int) (b []byte) {
281 cap := roundupsize(uintptr(size))
282 p := mallocgc(cap, nil, flagNoScan|flagNoZero)
283 if cap != uintptr(size) {
284 memclr(add(p, uintptr(size)), cap-uintptr(size))
287 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
291 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
292 func rawruneslice(size int) (b []rune) {
293 if uintptr(size) > _MaxMem/4 {
294 throw("out of memory")
296 mem := roundupsize(uintptr(size) * 4)
297 p := mallocgc(mem, nil, flagNoScan|flagNoZero)
298 if mem != uintptr(size)*4 {
299 memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
302 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
307 func gobytes(p *byte, n int) []byte {
309 return make([]byte, 0)
312 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
316 func gostring(p *byte) string {
322 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
326 func gostringn(p *byte, l int) string {
331 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
335 func index(s, t string) int {
339 for i := 0; i < len(s); i++ {
340 if s[i] == t[0] && hasprefix(s[i:], t) {
347 func contains(s, t string) bool {
348 return index(s, t) >= 0
351 func hasprefix(s, t string) bool {
352 return len(s) >= len(t) && s[:len(t)] == t
355 func atoi(s string) int {
357 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
358 n = n*10 + int(s[0]) - '0'
365 func findnull(s *byte) int {
369 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
377 func findnullw(s *uint16) int {
381 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
389 var maxstring uintptr = 256 // a hint for print
392 func gostringnocopy(str *byte) string {
393 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
394 s := *(*string)(unsafe.Pointer(&ss))
397 if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) {
404 func gostringw(strw *uint16) string {
406 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
408 for i := 0; str[i] != 0; i++ {
409 n1 += runetochar(buf[:], rune(str[i]))
411 s, b := rawstring(n1 + 4)
413 for i := 0; str[i] != 0; i++ {
418 n2 += runetochar(b[n2:], rune(str[i]))
420 b[n2] = 0 // for luck