blob: 35cde43be07a231cb2ac6cfd690e7d3da816789f [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 {
38 var s string
39 sp := (*stringStruct)(unsafe.Pointer(&s))
40 sp.str = unsafe.Pointer(str)
41 sp.len = findnull(str)
42 for {
43 ms := maxstring
44 if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) {
45 break
46 }
47 }
48 return s
49}
50
51func gostringw(strw *uint16) string {
52 var buf [8]byte
53 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
54 n1 := 0
55 for i := 0; str[i] != 0; i++ {
56 n1 += runetochar(buf[:], rune(str[i]))
57 }
58 s, b := rawstring(n1 + 4)
59 n2 := 0
60 for i := 0; str[i] != 0; i++ {
61 // check for race
62 if n2 >= n1 {
63 break
64 }
65 n2 += runetochar(b[n2:], rune(str[i]))
66 }
67 b[n2] = 0 // for luck
68 return s[:n2]
69}
70
71func strcmp(s1, s2 *byte) int32 {
72 p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1))
73 p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2))
74
75 for i := uintptr(0); ; i++ {
76 c1 := p1[i]
77 c2 := p2[i]
78 if c1 < c2 {
79 return -1
80 }
81 if c1 > c2 {
82 return +1
83 }
84 if c1 == 0 {
85 return 0
86 }
87 }
88}
89
90func strncmp(s1, s2 *byte, n uintptr) int32 {
91 p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1))
92 p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2))
93
94 for i := uintptr(0); i < n; i++ {
95 c1 := p1[i]
96 c2 := p2[i]
97 if c1 < c2 {
98 return -1
99 }
100 if c1 > c2 {
101 return +1
102 }
103 if c1 == 0 {
104 break
105 }
106 }
107 return 0
108}