github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/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 "internal/abi" 9 "internal/bytealg" 10 "internal/goarch" 11 "unsafe" 12 ) 13 14 // The constant is known to the compiler. 15 // There is no fundamental theory behind this number. 16 const tmpStringBufSize = 32 17 18 type tmpBuf [tmpStringBufSize]byte 19 20 // concatstrings implements a Go string concatenation x+y+z+... 21 // The operands are passed in the slice a. 22 // If buf != nil, the compiler has determined that the result does not 23 // escape the calling function, so the string data can be stored in buf 24 // if small enough. 25 func concatstrings(buf *tmpBuf, a []string) string { 26 idx := 0 27 l := 0 28 count := 0 29 for i, x := range a { 30 n := len(x) 31 if n == 0 { 32 continue 33 } 34 if l+n < l { 35 throw("string concatenation too long") 36 } 37 l += n 38 count++ 39 idx = i 40 } 41 if count == 0 { 42 return "" 43 } 44 45 // If there is just one string and either it is not on the stack 46 // or our result does not escape the calling frame (buf != nil), 47 // then we can return that string directly. 48 if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) { 49 return a[idx] 50 } 51 s, b := rawstringtmp(buf, l) 52 for _, x := range a { 53 copy(b, x) 54 b = b[len(x):] 55 } 56 return s 57 } 58 59 func concatstring2(buf *tmpBuf, a0, a1 string) string { 60 return concatstrings(buf, []string{a0, a1}) 61 } 62 63 func concatstring3(buf *tmpBuf, a0, a1, a2 string) string { 64 return concatstrings(buf, []string{a0, a1, a2}) 65 } 66 67 func concatstring4(buf *tmpBuf, a0, a1, a2, a3 string) string { 68 return concatstrings(buf, []string{a0, a1, a2, a3}) 69 } 70 71 func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string { 72 return concatstrings(buf, []string{a0, a1, a2, a3, a4}) 73 } 74 75 // slicebytetostring converts a byte slice to a string. 76 // It is inserted by the compiler into generated code. 77 // ptr is a pointer to the first element of the slice; 78 // n is the length of the slice. 79 // Buf is a fixed-size buffer for the result, 80 // it is not nil if the result does not escape. 81 func slicebytetostring(buf *tmpBuf, ptr *byte, n int) string { 82 if n == 0 { 83 // Turns out to be a relatively common case. 84 // Consider that you want to parse out data between parens in "foo()bar", 85 // you find the indices and convert the subslice to string. 86 return "" 87 } 88 if raceenabled { 89 racereadrangepc(unsafe.Pointer(ptr), 90 uintptr(n), 91 getcallerpc(), 92 abi.FuncPCABIInternal(slicebytetostring)) 93 } 94 if msanenabled { 95 msanread(unsafe.Pointer(ptr), uintptr(n)) 96 } 97 if asanenabled { 98 asanread(unsafe.Pointer(ptr), uintptr(n)) 99 } 100 if n == 1 { 101 p := unsafe.Pointer(&staticuint64s[*ptr]) 102 if goarch.BigEndian { 103 p = add(p, 7) 104 } 105 return unsafe.String((*byte)(p), 1) 106 } 107 108 var p unsafe.Pointer 109 if buf != nil && n <= len(buf) { 110 p = unsafe.Pointer(buf) 111 } else { 112 p = mallocgc(uintptr(n), nil, false) 113 } 114 memmove(p, unsafe.Pointer(ptr), uintptr(n)) 115 return unsafe.String((*byte)(p), n) 116 } 117 118 // stringDataOnStack reports whether the string's data is 119 // stored on the current goroutine's stack. 120 func stringDataOnStack(s string) bool { 121 ptr := uintptr(unsafe.Pointer(unsafe.StringData(s))) 122 stk := getg().stack 123 return stk.lo <= ptr && ptr < stk.hi 124 } 125 126 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) { 127 if buf != nil && l <= len(buf) { 128 b = buf[:l] 129 s = slicebytetostringtmp(&b[0], len(b)) 130 } else { 131 s, b = rawstring(l) 132 } 133 return 134 } 135 136 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes. 137 // 138 // Callers need to ensure that the returned string will not be used after 139 // the calling goroutine modifies the original slice or synchronizes with 140 // another goroutine. 141 // 142 // The function is only called when instrumenting 143 // and otherwise intrinsified by the compiler. 144 // 145 // Some internal compiler optimizations use this function. 146 // - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)] 147 // where k is []byte, T1 to Tn is a nesting of struct and array literals. 148 // - Used for "<"+string(b)+">" concatenation where b is []byte. 149 // - Used for string(b)=="foo" comparison where b is []byte. 150 func slicebytetostringtmp(ptr *byte, n int) string { 151 if raceenabled && n > 0 { 152 racereadrangepc(unsafe.Pointer(ptr), 153 uintptr(n), 154 getcallerpc(), 155 abi.FuncPCABIInternal(slicebytetostringtmp)) 156 } 157 if msanenabled && n > 0 { 158 msanread(unsafe.Pointer(ptr), uintptr(n)) 159 } 160 if asanenabled && n > 0 { 161 asanread(unsafe.Pointer(ptr), uintptr(n)) 162 } 163 return unsafe.String(ptr, n) 164 } 165 166 func stringtoslicebyte(buf *tmpBuf, s string) []byte { 167 var b []byte 168 if buf != nil && len(s) <= len(buf) { 169 *buf = tmpBuf{} 170 b = buf[:len(s)] 171 } else { 172 b = rawbyteslice(len(s)) 173 } 174 copy(b, s) 175 return b 176 } 177 178 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune { 179 // two passes. 180 // unlike slicerunetostring, no race because strings are immutable. 181 n := 0 182 for range s { 183 n++ 184 } 185 186 var a []rune 187 if buf != nil && n <= len(buf) { 188 *buf = [tmpStringBufSize]rune{} 189 a = buf[:n] 190 } else { 191 a = rawruneslice(n) 192 } 193 194 n = 0 195 for _, r := range s { 196 a[n] = r 197 n++ 198 } 199 return a 200 } 201 202 func slicerunetostring(buf *tmpBuf, a []rune) string { 203 if raceenabled && len(a) > 0 { 204 racereadrangepc(unsafe.Pointer(&a[0]), 205 uintptr(len(a))*unsafe.Sizeof(a[0]), 206 getcallerpc(), 207 abi.FuncPCABIInternal(slicerunetostring)) 208 } 209 if msanenabled && len(a) > 0 { 210 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0])) 211 } 212 if asanenabled && len(a) > 0 { 213 asanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0])) 214 } 215 var dum [4]byte 216 size1 := 0 217 for _, r := range a { 218 size1 += encoderune(dum[:], r) 219 } 220 s, b := rawstringtmp(buf, size1+3) 221 size2 := 0 222 for _, r := range a { 223 // check for race 224 if size2 >= size1 { 225 break 226 } 227 size2 += encoderune(b[size2:], r) 228 } 229 return s[:size2] 230 } 231 232 type stringStruct struct { 233 str unsafe.Pointer 234 len int 235 } 236 237 // Variant with *byte pointer type for DWARF debugging. 238 type stringStructDWARF struct { 239 str *byte 240 len int 241 } 242 243 func stringStructOf(sp *string) *stringStruct { 244 return (*stringStruct)(unsafe.Pointer(sp)) 245 } 246 247 func intstring(buf *[4]byte, v int64) (s string) { 248 var b []byte 249 if buf != nil { 250 b = buf[:] 251 s = slicebytetostringtmp(&b[0], len(b)) 252 } else { 253 s, b = rawstring(4) 254 } 255 if int64(rune(v)) != v { 256 v = runeError 257 } 258 n := encoderune(b, rune(v)) 259 return s[:n] 260 } 261 262 // rawstring allocates storage for a new string. The returned 263 // string and byte slice both refer to the same storage. 264 // The storage is not zeroed. Callers should use 265 // b to set the string contents and then drop b. 266 func rawstring(size int) (s string, b []byte) { 267 p := mallocgc(uintptr(size), nil, false) 268 return unsafe.String((*byte)(p), size), unsafe.Slice((*byte)(p), size) 269 } 270 271 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed. 272 func rawbyteslice(size int) (b []byte) { 273 cap := roundupsize(uintptr(size)) 274 p := mallocgc(cap, nil, false) 275 if cap != uintptr(size) { 276 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size)) 277 } 278 279 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)} 280 return 281 } 282 283 // rawruneslice allocates a new rune slice. The rune slice is not zeroed. 284 func rawruneslice(size int) (b []rune) { 285 if uintptr(size) > maxAlloc/4 { 286 throw("out of memory") 287 } 288 mem := roundupsize(uintptr(size) * 4) 289 p := mallocgc(mem, nil, false) 290 if mem != uintptr(size)*4 { 291 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4) 292 } 293 294 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)} 295 return 296 } 297 298 // used by cmd/cgo 299 func gobytes(p *byte, n int) (b []byte) { 300 if n == 0 { 301 return make([]byte, 0) 302 } 303 304 if n < 0 || uintptr(n) > maxAlloc { 305 panic(errorString("gobytes: length out of range")) 306 } 307 308 bp := mallocgc(uintptr(n), nil, false) 309 memmove(bp, unsafe.Pointer(p), uintptr(n)) 310 311 *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n} 312 return 313 } 314 315 // This is exported via linkname to assembly in syscall (for Plan9). 316 // 317 //go:linkname gostring 318 func gostring(p *byte) string { 319 l := findnull(p) 320 if l == 0 { 321 return "" 322 } 323 s, b := rawstring(l) 324 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l)) 325 return s 326 } 327 328 func gostringn(p *byte, l int) string { 329 if l == 0 { 330 return "" 331 } 332 s, b := rawstring(l) 333 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l)) 334 return s 335 } 336 337 func hasPrefix(s, prefix string) bool { 338 return len(s) >= len(prefix) && s[:len(prefix)] == prefix 339 } 340 341 const ( 342 maxUint64 = ^uint64(0) 343 maxInt64 = int64(maxUint64 >> 1) 344 ) 345 346 // atoi64 parses an int64 from a string s. 347 // The bool result reports whether s is a number 348 // representable by a value of type int64. 349 func atoi64(s string) (int64, bool) { 350 if s == "" { 351 return 0, false 352 } 353 354 neg := false 355 if s[0] == '-' { 356 neg = true 357 s = s[1:] 358 } 359 360 un := uint64(0) 361 for i := 0; i < len(s); i++ { 362 c := s[i] 363 if c < '0' || c > '9' { 364 return 0, false 365 } 366 if un > maxUint64/10 { 367 // overflow 368 return 0, false 369 } 370 un *= 10 371 un1 := un + uint64(c) - '0' 372 if un1 < un { 373 // overflow 374 return 0, false 375 } 376 un = un1 377 } 378 379 if !neg && un > uint64(maxInt64) { 380 return 0, false 381 } 382 if neg && un > uint64(maxInt64)+1 { 383 return 0, false 384 } 385 386 n := int64(un) 387 if neg { 388 n = -n 389 } 390 391 return n, true 392 } 393 394 // atoi is like atoi64 but for integers 395 // that fit into an int. 396 func atoi(s string) (int, bool) { 397 if n, ok := atoi64(s); n == int64(int(n)) { 398 return int(n), ok 399 } 400 return 0, false 401 } 402 403 // atoi32 is like atoi but for integers 404 // that fit into an int32. 405 func atoi32(s string) (int32, bool) { 406 if n, ok := atoi64(s); n == int64(int32(n)) { 407 return int32(n), ok 408 } 409 return 0, false 410 } 411 412 // parseByteCount parses a string that represents a count of bytes. 413 // 414 // s must match the following regular expression: 415 // 416 // ^[0-9]+(([KMGT]i)?B)?$ 417 // 418 // In other words, an integer byte count with an optional unit 419 // suffix. Acceptable suffixes include one of 420 // - KiB, MiB, GiB, TiB which represent binary IEC/ISO 80000 units, or 421 // - B, which just represents bytes. 422 // 423 // Returns an int64 because that's what its callers want and receive, 424 // but the result is always non-negative. 425 func parseByteCount(s string) (int64, bool) { 426 // The empty string is not valid. 427 if s == "" { 428 return 0, false 429 } 430 // Handle the easy non-suffix case. 431 last := s[len(s)-1] 432 if last >= '0' && last <= '9' { 433 n, ok := atoi64(s) 434 if !ok || n < 0 { 435 return 0, false 436 } 437 return n, ok 438 } 439 // Failing a trailing digit, this must always end in 'B'. 440 // Also at this point there must be at least one digit before 441 // that B. 442 if last != 'B' || len(s) < 2 { 443 return 0, false 444 } 445 // The one before that must always be a digit or 'i'. 446 if c := s[len(s)-2]; c >= '0' && c <= '9' { 447 // Trivial 'B' suffix. 448 n, ok := atoi64(s[:len(s)-1]) 449 if !ok || n < 0 { 450 return 0, false 451 } 452 return n, ok 453 } else if c != 'i' { 454 return 0, false 455 } 456 // Finally, we need at least 4 characters now, for the unit 457 // prefix and at least one digit. 458 if len(s) < 4 { 459 return 0, false 460 } 461 power := 0 462 switch s[len(s)-3] { 463 case 'K': 464 power = 1 465 case 'M': 466 power = 2 467 case 'G': 468 power = 3 469 case 'T': 470 power = 4 471 default: 472 // Invalid suffix. 473 return 0, false 474 } 475 m := uint64(1) 476 for i := 0; i < power; i++ { 477 m *= 1024 478 } 479 n, ok := atoi64(s[:len(s)-3]) 480 if !ok || n < 0 { 481 return 0, false 482 } 483 un := uint64(n) 484 if un > maxUint64/m { 485 // Overflow. 486 return 0, false 487 } 488 un *= m 489 if un > uint64(maxInt64) { 490 // Overflow. 491 return 0, false 492 } 493 return int64(un), true 494 } 495 496 //go:nosplit 497 func findnull(s *byte) int { 498 if s == nil { 499 return 0 500 } 501 502 // Avoid IndexByteString on Plan 9 because it uses SSE instructions 503 // on x86 machines, and those are classified as floating point instructions, 504 // which are illegal in a note handler. 505 if GOOS == "plan9" { 506 p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s)) 507 l := 0 508 for p[l] != 0 { 509 l++ 510 } 511 return l 512 } 513 514 // pageSize is the unit we scan at a time looking for NULL. 515 // It must be the minimum page size for any architecture Go 516 // runs on. It's okay (just a minor performance loss) if the 517 // actual system page size is larger than this value. 518 const pageSize = 4096 519 520 offset := 0 521 ptr := unsafe.Pointer(s) 522 // IndexByteString uses wide reads, so we need to be careful 523 // with page boundaries. Call IndexByteString on 524 // [ptr, endOfPage) interval. 525 safeLen := int(pageSize - uintptr(ptr)%pageSize) 526 527 for { 528 t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen})) 529 // Check one page at a time. 530 if i := bytealg.IndexByteString(t, 0); i != -1 { 531 return offset + i 532 } 533 // Move to next page 534 ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen)) 535 offset += safeLen 536 safeLen = pageSize 537 } 538 } 539 540 func findnullw(s *uint16) int { 541 if s == nil { 542 return 0 543 } 544 p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s)) 545 l := 0 546 for p[l] != 0 { 547 l++ 548 } 549 return l 550 } 551 552 //go:nosplit 553 func gostringnocopy(str *byte) string { 554 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)} 555 s := *(*string)(unsafe.Pointer(&ss)) 556 return s 557 } 558 559 func gostringw(strw *uint16) string { 560 var buf [8]byte 561 str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw)) 562 n1 := 0 563 for i := 0; str[i] != 0; i++ { 564 n1 += encoderune(buf[:], rune(str[i])) 565 } 566 s, b := rawstring(n1 + 4) 567 n2 := 0 568 for i := 0; str[i] != 0; i++ { 569 // check for race 570 if n2 >= n1 { 571 break 572 } 573 n2 += encoderune(b[n2:], rune(str[i])) 574 } 575 b[n2] = 0 // for luck 576 return s[:n2] 577 }