github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/containers/compilers/rump/go/gopatches/runtime/string1.go (about) 1 // Copyright 2009 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 "unsafe" 8 9 //go:nosplit 10 func findnull(s *byte) int { 11 if s == nil { 12 return 0 13 } 14 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s)) 15 l := 0 16 for p[l] != 0 { 17 l++ 18 } 19 if l == len(p) { 20 return 0 21 } 22 return l 23 } 24 25 func findnullw(s *uint16) int { 26 if s == nil { 27 return 0 28 } 29 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s)) 30 l := 0 31 for l < len(p) && p[l] != 0 { 32 l++ 33 } 34 if l == len(p) { 35 return 0 36 } 37 return l 38 } 39 40 var maxstring uintptr = 256 // a hint for print 41 42 //go:nosplit 43 func gostringnocopy(str *byte) string { 44 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)} 45 s := *(*string)(unsafe.Pointer(&ss)) 46 for { 47 ms := maxstring 48 if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) { 49 break 50 } 51 } 52 return s 53 } 54 55 func gostringw(strw *uint16) string { 56 var buf [8]byte 57 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw)) 58 n1 := 0 59 for i := 0; str[i] != 0; i++ { 60 n1 += runetochar(buf[:], rune(str[i])) 61 } 62 s, b := rawstring(n1 + 4) 63 n2 := 0 64 for i := 0; str[i] != 0; i++ { 65 // check for race 66 if n2 >= n1 { 67 break 68 } 69 n2 += runetochar(b[n2:], rune(str[i])) 70 } 71 b[n2] = 0 // for luck 72 return s[:n2] 73 }