string.go 6.0 KB

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