diff options
author | Russ Cox <rsc@golang.org> | 2014-12-05 11:43:41 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-12-05 11:43:41 -0500 |
commit | 8670c52abcdb260f5366d6fa205a7c7f9b5b1067 (patch) | |
tree | 3781a05c976360f88b736c71316dadc789e02062 /src/runtime/string1.go | |
parent | 3ebebda3a7495402239db4369d59d73749c1bfa2 (diff) | |
parent | 6d3ba1914e289ed223f7bb69f34604c0e2ae5384 (diff) | |
download | go-8670c52abcdb260f5366d6fa205a7c7f9b5b1067.tar.gz |
all: merge dev.cc (81884b89bd88) into default
With this change, default now contains Go 1.5 work.
Any future bug fixes for Go 1.4 in the compilers or
the runtime will have to be made directly to the
release branch.
Diffstat (limited to 'src/runtime/string1.go')
-rw-r--r-- | src/runtime/string1.go | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/runtime/string1.go b/src/runtime/string1.go new file mode 100644 index 000000000..35cde43be --- /dev/null +++ b/src/runtime/string1.go @@ -0,0 +1,108 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package runtime + +import "unsafe" + +//go:nosplit +func findnull(s *byte) int { + if s == nil { + return 0 + } + p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s)) + l := 0 + for p[l] != 0 { + l++ + } + return l +} + +func findnullw(s *uint16) int { + if s == nil { + return 0 + } + p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s)) + l := 0 + for p[l] != 0 { + l++ + } + return l +} + +var maxstring uintptr = 256 // a hint for print + +//go:nosplit +func gostringnocopy(str *byte) string { + var s string + sp := (*stringStruct)(unsafe.Pointer(&s)) + sp.str = unsafe.Pointer(str) + sp.len = findnull(str) + for { + ms := maxstring + if uintptr(len(s)) <= ms || casuintptr(&maxstring, ms, uintptr(len(s))) { + break + } + } + return s +} + +func gostringw(strw *uint16) string { + var buf [8]byte + str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw)) + n1 := 0 + for i := 0; str[i] != 0; i++ { + n1 += runetochar(buf[:], rune(str[i])) + } + s, b := rawstring(n1 + 4) + n2 := 0 + for i := 0; str[i] != 0; i++ { + // check for race + if n2 >= n1 { + break + } + n2 += runetochar(b[n2:], rune(str[i])) + } + b[n2] = 0 // for luck + return s[:n2] +} + +func strcmp(s1, s2 *byte) int32 { + p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1)) + p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2)) + + for i := uintptr(0); ; i++ { + c1 := p1[i] + c2 := p2[i] + if c1 < c2 { + return -1 + } + if c1 > c2 { + return +1 + } + if c1 == 0 { + return 0 + } + } +} + +func strncmp(s1, s2 *byte, n uintptr) int32 { + p1 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s1)) + p2 := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s2)) + + for i := uintptr(0); i < n; i++ { + c1 := p1[i] + c2 := p2[i] + if c1 < c2 { + return -1 + } + if c1 > c2 { + return +1 + } + if c1 == 0 { + break + } + } + return 0 +} |