github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/string.go (about)

     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  	"unsafe"
     9  )
    10  
    11  func concatstrings(a []string) string {
    12  	idx := 0
    13  	l := 0
    14  	count := 0
    15  	for i, x := range a {
    16  		n := len(x)
    17  		if n == 0 {
    18  			continue
    19  		}
    20  		if l+n < l {
    21  			gothrow("string concatenation too long")
    22  		}
    23  		l += n
    24  		count++
    25  		idx = i
    26  	}
    27  	if count == 0 {
    28  		return ""
    29  	}
    30  	if count == 1 {
    31  		return a[idx]
    32  	}
    33  	s, b := rawstring(l)
    34  	l = 0
    35  	for _, x := range a {
    36  		copy(b[l:], x)
    37  		l += len(x)
    38  	}
    39  	return s
    40  }
    41  
    42  func concatstring2(a [2]string) string {
    43  	return concatstrings(a[:])
    44  }
    45  
    46  func concatstring3(a [3]string) string {
    47  	return concatstrings(a[:])
    48  }
    49  
    50  func concatstring4(a [4]string) string {
    51  	return concatstrings(a[:])
    52  }
    53  
    54  func concatstring5(a [5]string) string {
    55  	return concatstrings(a[:])
    56  }
    57  
    58  func slicebytetostring(b []byte) string {
    59  	if raceenabled && len(b) > 0 {
    60  		racereadrangepc(unsafe.Pointer(&b[0]),
    61  			uintptr(len(b)),
    62  			getcallerpc(unsafe.Pointer(&b)),
    63  			funcPC(slicebytetostring))
    64  	}
    65  	s, c := rawstring(len(b))
    66  	copy(c, b)
    67  	return s
    68  }
    69  
    70  func slicebytetostringtmp(b []byte) string {
    71  	// Return a "string" referring to the actual []byte bytes.
    72  	// This is only for use by internal compiler optimizations
    73  	// that know that the string form will be discarded before
    74  	// the calling goroutine could possibly modify the original
    75  	// slice or synchronize with another goroutine.
    76  	// Today, the only such case is a m[string(k)] lookup where
    77  	// m is a string-keyed map and k is a []byte.
    78  
    79  	if raceenabled && len(b) > 0 {
    80  		racereadrangepc(unsafe.Pointer(&b[0]),
    81  			uintptr(len(b)),
    82  			getcallerpc(unsafe.Pointer(&b)),
    83  			funcPC(slicebytetostringtmp))
    84  	}
    85  	return *(*string)(unsafe.Pointer(&b))
    86  }
    87  
    88  func stringtoslicebyte(s string) []byte {
    89  	b := rawbyteslice(len(s))
    90  	copy(b, s)
    91  	return b
    92  }
    93  
    94  func stringtoslicerune(s string) []rune {
    95  	// two passes.
    96  	// unlike slicerunetostring, no race because strings are immutable.
    97  	n := 0
    98  	t := s
    99  	for len(s) > 0 {
   100  		_, k := charntorune(s)
   101  		s = s[k:]
   102  		n++
   103  	}
   104  	a := rawruneslice(n)
   105  	n = 0
   106  	for len(t) > 0 {
   107  		r, k := charntorune(t)
   108  		t = t[k:]
   109  		a[n] = r
   110  		n++
   111  	}
   112  	return a
   113  }
   114  
   115  func slicerunetostring(a []rune) string {
   116  	if raceenabled && len(a) > 0 {
   117  		racereadrangepc(unsafe.Pointer(&a[0]),
   118  			uintptr(len(a))*unsafe.Sizeof(a[0]),
   119  			getcallerpc(unsafe.Pointer(&a)),
   120  			funcPC(slicerunetostring))
   121  	}
   122  	var dum [4]byte
   123  	size1 := 0
   124  	for _, r := range a {
   125  		size1 += runetochar(dum[:], r)
   126  	}
   127  	s, b := rawstring(size1 + 3)
   128  	size2 := 0
   129  	for _, r := range a {
   130  		// check for race
   131  		if size2 >= size1 {
   132  			break
   133  		}
   134  		size2 += runetochar(b[size2:], r)
   135  	}
   136  	return s[:size2]
   137  }
   138  
   139  type stringStruct struct {
   140  	str unsafe.Pointer
   141  	len int
   142  }
   143  
   144  func intstring(v int64) string {
   145  	s, b := rawstring(4)
   146  	n := runetochar(b, rune(v))
   147  	return s[:n]
   148  }
   149  
   150  // stringiter returns the index of the next
   151  // rune after the rune that starts at s[k].
   152  func stringiter(s string, k int) int {
   153  	if k >= len(s) {
   154  		// 0 is end of iteration
   155  		return 0
   156  	}
   157  
   158  	c := s[k]
   159  	if c < runeself {
   160  		return k + 1
   161  	}
   162  
   163  	// multi-char rune
   164  	_, n := charntorune(s[k:])
   165  	return k + n
   166  }
   167  
   168  // stringiter2 returns the rune that starts at s[k]
   169  // and the index where the next rune starts.
   170  func stringiter2(s string, k int) (int, rune) {
   171  	if k >= len(s) {
   172  		// 0 is end of iteration
   173  		return 0, 0
   174  	}
   175  
   176  	c := s[k]
   177  	if c < runeself {
   178  		return k + 1, rune(c)
   179  	}
   180  
   181  	// multi-char rune
   182  	r, n := charntorune(s[k:])
   183  	return k + n, r
   184  }
   185  
   186  // rawstring allocates storage for a new string. The returned
   187  // string and byte slice both refer to the same storage.
   188  // The storage is not zeroed. Callers should use
   189  // b to set the string contents and then drop b.
   190  func rawstring(size int) (s string, b []byte) {
   191  	p := mallocgc(uintptr(size), nil, flagNoScan|flagNoZero)
   192  
   193  	(*stringStruct)(unsafe.Pointer(&s)).str = p
   194  	(*stringStruct)(unsafe.Pointer(&s)).len = size
   195  
   196  	(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
   197  	(*slice)(unsafe.Pointer(&b)).len = uint(size)
   198  	(*slice)(unsafe.Pointer(&b)).cap = uint(size)
   199  
   200  	for {
   201  		ms := maxstring
   202  		if uintptr(size) <= uintptr(ms) || casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), uintptr(ms), uintptr(size)) {
   203  			return
   204  		}
   205  	}
   206  }
   207  
   208  // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
   209  func rawbyteslice(size int) (b []byte) {
   210  	cap := goroundupsize(uintptr(size))
   211  	p := mallocgc(cap, nil, flagNoScan|flagNoZero)
   212  	if cap != uintptr(size) {
   213  		memclr(add(p, uintptr(size)), cap-uintptr(size))
   214  	}
   215  
   216  	(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
   217  	(*slice)(unsafe.Pointer(&b)).len = uint(size)
   218  	(*slice)(unsafe.Pointer(&b)).cap = uint(cap)
   219  	return
   220  }
   221  
   222  // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
   223  func rawruneslice(size int) (b []rune) {
   224  	if uintptr(size) > _MaxMem/4 {
   225  		gothrow("out of memory")
   226  	}
   227  	mem := goroundupsize(uintptr(size) * 4)
   228  	p := mallocgc(mem, nil, flagNoScan|flagNoZero)
   229  	if mem != uintptr(size)*4 {
   230  		memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
   231  	}
   232  
   233  	(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
   234  	(*slice)(unsafe.Pointer(&b)).len = uint(size)
   235  	(*slice)(unsafe.Pointer(&b)).cap = uint(mem / 4)
   236  	return
   237  }
   238  
   239  // used by cmd/cgo
   240  func gobytes(p *byte, n int) []byte {
   241  	if n == 0 {
   242  		return make([]byte, 0)
   243  	}
   244  	x := make([]byte, n)
   245  	memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
   246  	return x
   247  }
   248  
   249  func gostringsize(n int) string {
   250  	s, _ := rawstring(n)
   251  	return s
   252  }
   253  
   254  func gostring(p *byte) string {
   255  	l := findnull(p)
   256  	if l == 0 {
   257  		return ""
   258  	}
   259  	s, b := rawstring(l)
   260  	memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
   261  	return s
   262  }
   263  
   264  func gostringn(p *byte, l int) string {
   265  	if l == 0 {
   266  		return ""
   267  	}
   268  	s, b := rawstring(l)
   269  	memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
   270  	return s
   271  }
   272  
   273  func index(s, t string) int {
   274  	if len(t) == 0 {
   275  		return 0
   276  	}
   277  	for i := 0; i < len(s); i++ {
   278  		if s[i] == t[0] && hasprefix(s[i:], t) {
   279  			return i
   280  		}
   281  	}
   282  	return -1
   283  }
   284  
   285  func contains(s, t string) bool {
   286  	return index(s, t) >= 0
   287  }
   288  
   289  func hasprefix(s, t string) bool {
   290  	return len(s) >= len(t) && s[:len(t)] == t
   291  }
   292  
   293  func goatoi(s string) int {
   294  	n := 0
   295  	for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
   296  		n = n*10 + int(s[0]) - '0'
   297  		s = s[1:]
   298  	}
   299  	return n
   300  }