...
Run Format

Source file src/runtime/string.go

Documentation: runtime

     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.
     4  
     5  package runtime
     6  
     7  import (
     8  	"internal/bytealg"
     9  	"unsafe"
    10  )
    11  
    12  // The constant is known to the compiler.
    13  // There is no fundamental theory behind this number.
    14  const tmpStringBufSize = 32
    15  
    16  type tmpBuf [tmpStringBufSize]byte
    17  
    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
    22  // if small enough.
    23  func concatstrings(buf *tmpBuf, a []string) string {
    24  	idx := 0
    25  	l := 0
    26  	count := 0
    27  	for i, x := range a {
    28  		n := len(x)
    29  		if n == 0 {
    30  			continue
    31  		}
    32  		if l+n < l {
    33  			throw("string concatenation too long")
    34  		}
    35  		l += n
    36  		count++
    37  		idx = i
    38  	}
    39  	if count == 0 {
    40  		return ""
    41  	}
    42  
    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])) {
    47  		return a[idx]
    48  	}
    49  	s, b := rawstringtmp(buf, l)
    50  	for _, x := range a {
    51  		copy(b, x)
    52  		b = b[len(x):]
    53  	}
    54  	return s
    55  }
    56  
    57  func concatstring2(buf *tmpBuf, a [2]string) string {
    58  	return concatstrings(buf, a[:])
    59  }
    60  
    61  func concatstring3(buf *tmpBuf, a [3]string) string {
    62  	return concatstrings(buf, a[:])
    63  }
    64  
    65  func concatstring4(buf *tmpBuf, a [4]string) string {
    66  	return concatstrings(buf, a[:])
    67  }
    68  
    69  func concatstring5(buf *tmpBuf, a [5]string) string {
    70  	return concatstrings(buf, a[:])
    71  }
    72  
    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) {
    76  	l := len(b)
    77  	if l == 0 {
    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.
    81  		return ""
    82  	}
    83  	if raceenabled {
    84  		racereadrangepc(unsafe.Pointer(&b[0]),
    85  			uintptr(l),
    86  			getcallerpc(),
    87  			funcPC(slicebytetostring))
    88  	}
    89  	if msanenabled {
    90  		msanread(unsafe.Pointer(&b[0]), uintptr(l))
    91  	}
    92  	if l == 1 {
    93  		stringStructOf(&str).str = unsafe.Pointer(&staticbytes[b[0]])
    94  		stringStructOf(&str).len = 1
    95  		return
    96  	}
    97  
    98  	var p unsafe.Pointer
    99  	if buf != nil && len(b) <= len(buf) {
   100  		p = unsafe.Pointer(buf)
   101  	} else {
   102  		p = mallocgc(uintptr(len(b)), nil, false)
   103  	}
   104  	stringStructOf(&str).str = p
   105  	stringStructOf(&str).len = len(b)
   106  	memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
   107  	return
   108  }
   109  
   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)
   114  	stk := getg().stack
   115  	return stk.lo <= ptr && ptr < stk.hi
   116  }
   117  
   118  func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
   119  	if buf != nil && l <= len(buf) {
   120  		b = buf[:l]
   121  		s = slicebytetostringtmp(b)
   122  	} else {
   123  		s, b = rawstring(l)
   124  	}
   125  	return
   126  }
   127  
   128  // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
   129  //
   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.
   133  //
   134  // The function is only called when instrumenting
   135  // and otherwise intrinsified by the compiler.
   136  //
   137  // Some internal compiler optimizations use this function.
   138  // - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
   139  //   where k is []byte, T1 to Tn is a nesting of struct and array literals.
   140  // - Used for "<"+string(b)+">" concatenation where b is []byte.
   141  // - Used for string(b)=="foo" comparison where b is []byte.
   142  func slicebytetostringtmp(b []byte) string {
   143  	if raceenabled && len(b) > 0 {
   144  		racereadrangepc(unsafe.Pointer(&b[0]),
   145  			uintptr(len(b)),
   146  			getcallerpc(),
   147  			funcPC(slicebytetostringtmp))
   148  	}
   149  	if msanenabled && len(b) > 0 {
   150  		msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
   151  	}
   152  	return *(*string)(unsafe.Pointer(&b))
   153  }
   154  
   155  func stringtoslicebyte(buf *tmpBuf, s string) []byte {
   156  	var b []byte
   157  	if buf != nil && len(s) <= len(buf) {
   158  		*buf = tmpBuf{}
   159  		b = buf[:len(s)]
   160  	} else {
   161  		b = rawbyteslice(len(s))
   162  	}
   163  	copy(b, s)
   164  	return b
   165  }
   166  
   167  func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
   168  	// two passes.
   169  	// unlike slicerunetostring, no race because strings are immutable.
   170  	n := 0
   171  	for range s {
   172  		n++
   173  	}
   174  
   175  	var a []rune
   176  	if buf != nil && n <= len(buf) {
   177  		*buf = [tmpStringBufSize]rune{}
   178  		a = buf[:n]
   179  	} else {
   180  		a = rawruneslice(n)
   181  	}
   182  
   183  	n = 0
   184  	for _, r := range s {
   185  		a[n] = r
   186  		n++
   187  	}
   188  	return a
   189  }
   190  
   191  func slicerunetostring(buf *tmpBuf, a []rune) string {
   192  	if raceenabled && len(a) > 0 {
   193  		racereadrangepc(unsafe.Pointer(&a[0]),
   194  			uintptr(len(a))*unsafe.Sizeof(a[0]),
   195  			getcallerpc(),
   196  			funcPC(slicerunetostring))
   197  	}
   198  	if msanenabled && len(a) > 0 {
   199  		msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
   200  	}
   201  	var dum [4]byte
   202  	size1 := 0
   203  	for _, r := range a {
   204  		size1 += encoderune(dum[:], r)
   205  	}
   206  	s, b := rawstringtmp(buf, size1+3)
   207  	size2 := 0
   208  	for _, r := range a {
   209  		// check for race
   210  		if size2 >= size1 {
   211  			break
   212  		}
   213  		size2 += encoderune(b[size2:], r)
   214  	}
   215  	return s[:size2]
   216  }
   217  
   218  type stringStruct struct {
   219  	str unsafe.Pointer
   220  	len int
   221  }
   222  
   223  // Variant with *byte pointer type for DWARF debugging.
   224  type stringStructDWARF struct {
   225  	str *byte
   226  	len int
   227  }
   228  
   229  func stringStructOf(sp *string) *stringStruct {
   230  	return (*stringStruct)(unsafe.Pointer(sp))
   231  }
   232  
   233  func intstring(buf *[4]byte, v int64) (s string) {
   234  	if v >= 0 && v < runeSelf {
   235  		stringStructOf(&s).str = unsafe.Pointer(&staticbytes[v])
   236  		stringStructOf(&s).len = 1
   237  		return
   238  	}
   239  
   240  	var b []byte
   241  	if buf != nil {
   242  		b = buf[:]
   243  		s = slicebytetostringtmp(b)
   244  	} else {
   245  		s, b = rawstring(4)
   246  	}
   247  	if int64(rune(v)) != v {
   248  		v = runeError
   249  	}
   250  	n := encoderune(b, rune(v))
   251  	return s[:n]
   252  }
   253  
   254  // rawstring allocates storage for a new string. The returned
   255  // string and byte slice both refer to the same storage.
   256  // The storage is not zeroed. Callers should use
   257  // b to set the string contents and then drop b.
   258  func rawstring(size int) (s string, b []byte) {
   259  	p := mallocgc(uintptr(size), nil, false)
   260  
   261  	stringStructOf(&s).str = p
   262  	stringStructOf(&s).len = size
   263  
   264  	*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
   265  
   266  	return
   267  }
   268  
   269  // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
   270  func rawbyteslice(size int) (b []byte) {
   271  	cap := roundupsize(uintptr(size))
   272  	p := mallocgc(cap, nil, false)
   273  	if cap != uintptr(size) {
   274  		memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
   275  	}
   276  
   277  	*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
   278  	return
   279  }
   280  
   281  // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
   282  func rawruneslice(size int) (b []rune) {
   283  	if uintptr(size) > maxAlloc/4 {
   284  		throw("out of memory")
   285  	}
   286  	mem := roundupsize(uintptr(size) * 4)
   287  	p := mallocgc(mem, nil, false)
   288  	if mem != uintptr(size)*4 {
   289  		memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
   290  	}
   291  
   292  	*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
   293  	return
   294  }
   295  
   296  // used by cmd/cgo
   297  func gobytes(p *byte, n int) (b []byte) {
   298  	if n == 0 {
   299  		return make([]byte, 0)
   300  	}
   301  
   302  	if n < 0 || uintptr(n) > maxAlloc {
   303  		panic(errorString("gobytes: length out of range"))
   304  	}
   305  
   306  	bp := mallocgc(uintptr(n), nil, false)
   307  	memmove(bp, unsafe.Pointer(p), uintptr(n))
   308  
   309  	*(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
   310  	return
   311  }
   312  
   313  func gostring(p *byte) string {
   314  	l := findnull(p)
   315  	if l == 0 {
   316  		return ""
   317  	}
   318  	s, b := rawstring(l)
   319  	memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
   320  	return s
   321  }
   322  
   323  func gostringn(p *byte, l int) string {
   324  	if l == 0 {
   325  		return ""
   326  	}
   327  	s, b := rawstring(l)
   328  	memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
   329  	return s
   330  }
   331  
   332  func index(s, t string) int {
   333  	if len(t) == 0 {
   334  		return 0
   335  	}
   336  	for i := 0; i < len(s); i++ {
   337  		if s[i] == t[0] && hasPrefix(s[i:], t) {
   338  			return i
   339  		}
   340  	}
   341  	return -1
   342  }
   343  
   344  func contains(s, t string) bool {
   345  	return index(s, t) >= 0
   346  }
   347  
   348  func hasPrefix(s, prefix string) bool {
   349  	return len(s) >= len(prefix) && s[:len(prefix)] == prefix
   350  }
   351  
   352  const (
   353  	maxUint = ^uint(0)
   354  	maxInt  = int(maxUint >> 1)
   355  )
   356  
   357  // atoi parses an int from a string s.
   358  // The bool result reports whether s is a number
   359  // representable by a value of type int.
   360  func atoi(s string) (int, bool) {
   361  	if s == "" {
   362  		return 0, false
   363  	}
   364  
   365  	neg := false
   366  	if s[0] == '-' {
   367  		neg = true
   368  		s = s[1:]
   369  	}
   370  
   371  	un := uint(0)
   372  	for i := 0; i < len(s); i++ {
   373  		c := s[i]
   374  		if c < '0' || c > '9' {
   375  			return 0, false
   376  		}
   377  		if un > maxUint/10 {
   378  			// overflow
   379  			return 0, false
   380  		}
   381  		un *= 10
   382  		un1 := un + uint(c) - '0'
   383  		if un1 < un {
   384  			// overflow
   385  			return 0, false
   386  		}
   387  		un = un1
   388  	}
   389  
   390  	if !neg && un > uint(maxInt) {
   391  		return 0, false
   392  	}
   393  	if neg && un > uint(maxInt)+1 {
   394  		return 0, false
   395  	}
   396  
   397  	n := int(un)
   398  	if neg {
   399  		n = -n
   400  	}
   401  
   402  	return n, true
   403  }
   404  
   405  // atoi32 is like atoi but for integers
   406  // that fit into an int32.
   407  func atoi32(s string) (int32, bool) {
   408  	if n, ok := atoi(s); n == int(int32(n)) {
   409  		return int32(n), ok
   410  	}
   411  	return 0, false
   412  }
   413  
   414  //go:nosplit
   415  func findnull(s *byte) int {
   416  	if s == nil {
   417  		return 0
   418  	}
   419  
   420  	// Avoid IndexByteString on Plan 9 because it uses SSE instructions
   421  	// on x86 machines, and those are classified as floating point instructions,
   422  	// which are illegal in a note handler.
   423  	if GOOS == "plan9" {
   424  		p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
   425  		l := 0
   426  		for p[l] != 0 {
   427  			l++
   428  		}
   429  		return l
   430  	}
   431  
   432  	// pageSize is the unit we scan at a time looking for NULL.
   433  	// It must be the minimum page size for any architecture Go
   434  	// runs on. It's okay (just a minor performance loss) if the
   435  	// actual system page size is larger than this value.
   436  	const pageSize = 4096
   437  
   438  	offset := 0
   439  	ptr := unsafe.Pointer(s)
   440  	// IndexByteString uses wide reads, so we need to be careful
   441  	// with page boundaries. Call IndexByteString on
   442  	// [ptr, endOfPage) interval.
   443  	safeLen := int(pageSize - uintptr(ptr)%pageSize)
   444  
   445  	for {
   446  		t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
   447  		// Check one page at a time.
   448  		if i := bytealg.IndexByteString(t, 0); i != -1 {
   449  			return offset + i
   450  		}
   451  		// Move to next page
   452  		ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
   453  		offset += safeLen
   454  		safeLen = pageSize
   455  	}
   456  }
   457  
   458  func findnullw(s *uint16) int {
   459  	if s == nil {
   460  		return 0
   461  	}
   462  	p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
   463  	l := 0
   464  	for p[l] != 0 {
   465  		l++
   466  	}
   467  	return l
   468  }
   469  
   470  //go:nosplit
   471  func gostringnocopy(str *byte) string {
   472  	ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
   473  	s := *(*string)(unsafe.Pointer(&ss))
   474  	return s
   475  }
   476  
   477  func gostringw(strw *uint16) string {
   478  	var buf [8]byte
   479  	str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
   480  	n1 := 0
   481  	for i := 0; str[i] != 0; i++ {
   482  		n1 += encoderune(buf[:], rune(str[i]))
   483  	}
   484  	s, b := rawstring(n1 + 4)
   485  	n2 := 0
   486  	for i := 0; str[i] != 0; i++ {
   487  		// check for race
   488  		if n2 >= n1 {
   489  			break
   490  		}
   491  		n2 += encoderune(b[n2:], rune(str[i]))
   492  	}
   493  	b[n2] = 0 // for luck
   494  	return s[:n2]
   495  }
   496  

View as plain text