blob: 4bfa3d95f99c65f4b4aa312211686cab6dbd3d10 [file] [log] [blame]
Russ Cox0c3c2c12014-11-11 17:07:06 -05001// 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
5package runtime
6
7import "unsafe"
8
9//go:nosplit
10func 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 return l
20}
21
22func findnullw(s *uint16) int {
23 if s == nil {
24 return 0
25 }
26 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
27 l := 0
28 for p[l] != 0 {
29 l++
30 }
31 return l
32}
33
34var maxstring uintptr = 256 // a hint for print
35
36//go:nosplit
37func gostringnocopy(str *byte) string {
David Chase7fbb1b32015-03-26 16:36:15 -040038 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
39 s := *(*string)(unsafe.Pointer(&ss))
Russ Cox0c3c2c12014-11-11 17:07:06 -050040 for {
41 ms := maxstring
42 if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) {
43 break
44 }
45 }
46 return s
47}
48
49func gostringw(strw *uint16) string {
50 var buf [8]byte
51 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
52 n1 := 0
53 for i := 0; str[i] != 0; i++ {
54 n1 += runetochar(buf[:], rune(str[i]))
55 }
56 s, b := rawstring(n1 + 4)
57 n2 := 0
58 for i := 0; str[i] != 0; i++ {
59 // check for race
60 if n2 >= n1 {
61 break
62 }
63 n2 += runetochar(b[n2:], rune(str[i]))
64 }
65 b[n2] = 0 // for luck
66 return s[:n2]
67}