mirror of
https://github.com/golang/go
synced 2024-11-26 05:27:57 -07:00
casify misc
R=r DELTA=247 (20 added, 50 deleted, 177 changed) OCL=22951 CL=22955
This commit is contained in:
parent
116a6e9c9c
commit
aedfb397ae
@ -190,7 +190,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
|
||||
}
|
||||
|
||||
var f []string;
|
||||
if f = strings.split(s, " "); len(f) != 3 {
|
||||
if f = strings.Split(s, " "); len(f) != 3 {
|
||||
return nil, BadRequest
|
||||
}
|
||||
req.method, req.rawurl, req.proto = f[0], f[1], f[2];
|
||||
|
@ -156,7 +156,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
|
||||
}
|
||||
|
||||
// If there's no @, split's default is wrong. Check explicitly.
|
||||
if strings.index(url.authority, "@") < 0 {
|
||||
if strings.Index(url.authority, "@") < 0 {
|
||||
url.host = url.authority;
|
||||
} else {
|
||||
url.userinfo, url.host = split(url.authority, '@', true);
|
||||
|
@ -60,21 +60,21 @@ export func Readn(fd Read, buf []byte) (n int, err *os.Error) {
|
||||
|
||||
// Convert something that implements Read into something
|
||||
// whose Reads are always Readn
|
||||
type FullRead struct {
|
||||
type _FullRead struct {
|
||||
fd Read;
|
||||
}
|
||||
|
||||
func (fd *FullRead) Read(p []byte) (n int, err *os.Error) {
|
||||
func (fd *_FullRead) Read(p []byte) (n int, err *os.Error) {
|
||||
n, err = Readn(fd.fd, p);
|
||||
return n, err
|
||||
}
|
||||
|
||||
export func MakeFullReader(fd Read) Read {
|
||||
if fr, ok := fd.(*FullRead); ok {
|
||||
// already a FullRead
|
||||
export func Make_FullReader(fd Read) Read {
|
||||
if fr, ok := fd.(*_FullRead); ok {
|
||||
// already a _FullRead
|
||||
return fd
|
||||
}
|
||||
return &FullRead{fd}
|
||||
return &_FullRead{fd}
|
||||
}
|
||||
|
||||
// Copies n bytes (or until EOF is reached) from src to dst.
|
||||
|
@ -131,7 +131,7 @@ func (j *_Map) String() string {
|
||||
export func Walk(j Json, path string) Json {
|
||||
for len(path) > 0 {
|
||||
var elem string;
|
||||
if i := strings.index(path, "/"); i >= 0 {
|
||||
if i := strings.Index(path, "/"); i >= 0 {
|
||||
elem = path[0:i];
|
||||
path = path[i+1:len(path)];
|
||||
} else {
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
package malloc
|
||||
|
||||
type Stats struct {
|
||||
export type Stats struct {
|
||||
alloc uint64;
|
||||
sys uint64;
|
||||
};
|
||||
|
@ -184,7 +184,7 @@ export func LookupHost(name string) (name1 string, addrs []string, err *os.Error
|
||||
// If name is rooted (trailing dot) or has enough dots,
|
||||
// try it by itself first.
|
||||
rooted := len(name) > 0 && name[len(name)-1] == '.';
|
||||
if rooted || strings.count(name, ".") >= cfg.ndots {
|
||||
if rooted || strings.Count(name, ".") >= cfg.ndots {
|
||||
rname := name;
|
||||
if !rooted {
|
||||
rname += ".";
|
||||
|
@ -11,30 +11,29 @@
|
||||
|
||||
package once
|
||||
|
||||
type Job struct {
|
||||
type _Job struct {
|
||||
done bool;
|
||||
doit chan bool; // buffer of 1
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
type _Request struct {
|
||||
f *();
|
||||
reply chan *Job
|
||||
reply chan *_Job
|
||||
}
|
||||
|
||||
// TODO: Would like to use chan Request but 6g rejects it.
|
||||
var service = make(chan *Request)
|
||||
var jobmap = make(map[*()]*Job)
|
||||
var service = make(chan _Request)
|
||||
var jobmap = make(map[*()]*_Job)
|
||||
|
||||
// Moderate access to the jobmap.
|
||||
// Even if accesses were thread-safe (they should be but are not)
|
||||
// something needs to serialize creation of new jobs.
|
||||
// That's what the Server does.
|
||||
func Server() {
|
||||
func server() {
|
||||
for {
|
||||
req := <-service;
|
||||
job, present := jobmap[req.f];
|
||||
if !present {
|
||||
job = new(Job);
|
||||
job = new(_Job);
|
||||
job.doit = make(chan bool, 1);
|
||||
job.doit <- true;
|
||||
jobmap[req.f] = job
|
||||
@ -48,13 +47,12 @@ export func Do(f *()) {
|
||||
// If not there, ask map server to make one.
|
||||
// TODO: Uncomment use of jobmap[f] once
|
||||
// maps are thread-safe.
|
||||
var job *Job;
|
||||
var job *_Job;
|
||||
var present bool;
|
||||
// job, present = jobmap[f]
|
||||
if !present {
|
||||
c := make(chan *Job);
|
||||
req := Request{f, c};
|
||||
service <- &req;
|
||||
c := make(chan *_Job);
|
||||
service <- _Request{f, c};
|
||||
job = <-c
|
||||
}
|
||||
|
||||
@ -74,6 +72,6 @@ export func Do(f *()) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
go Server()
|
||||
go server()
|
||||
}
|
||||
|
||||
|
@ -10,22 +10,22 @@ import (
|
||||
)
|
||||
|
||||
var ncall int;
|
||||
func Call() {
|
||||
func call() {
|
||||
ncall++
|
||||
}
|
||||
|
||||
export func TestOnce(t *testing.T) {
|
||||
ncall = 0;
|
||||
once.Do(&Call);
|
||||
once.Do(&call);
|
||||
if ncall != 1 {
|
||||
t.Fatalf("once.Do(&Call) didn't Call(): ncall=%d", ncall);
|
||||
t.Fatalf("once.Do(&call) didn't call(): ncall=%d", ncall);
|
||||
}
|
||||
once.Do(&Call);
|
||||
once.Do(&call);
|
||||
if ncall != 1 {
|
||||
t.Fatalf("second once.Do(&Call) did Call(): ncall=%d", ncall);
|
||||
t.Fatalf("second once.Do(&call) did call(): ncall=%d", ncall);
|
||||
}
|
||||
once.Do(&Call);
|
||||
once.Do(&call);
|
||||
if ncall != 1 {
|
||||
t.Fatalf("third once.Do(&Call) did Call(): ncall=%d", ncall);
|
||||
t.Fatalf("third once.Do(&call) did call(): ncall=%d", ncall);
|
||||
}
|
||||
}
|
||||
|
133
src/lib/rand.go
133
src/lib/rand.go
@ -10,60 +10,54 @@
|
||||
|
||||
package rand
|
||||
|
||||
// rand, rand31, rand63 - return non-negative random int, int32, int64
|
||||
// rand, rand31, Int63 - return non-negative random int, int32, int64
|
||||
// urand32 - return random uint32
|
||||
// nrand, nrand31, nrand63 - return 0 <= random < n
|
||||
// nrand, nrand31, Int63n - return 0 <= random < n
|
||||
// frand, frand64, frand32 - return 0 <= random float, float64, float32 < 1
|
||||
// perm gives a random permutation []int
|
||||
|
||||
const
|
||||
(
|
||||
LEN = 607;
|
||||
TAP = 273;
|
||||
MASK = (1<<63)-1;
|
||||
A = 48271;
|
||||
M = 2147483647;
|
||||
Q = 44488;
|
||||
R = 3399;
|
||||
const (
|
||||
_LEN = 607;
|
||||
_TAP = 273;
|
||||
_MASK = (1<<63)-1;
|
||||
_A = 48271;
|
||||
_M = 2147483647;
|
||||
_Q = 44488;
|
||||
_R = 3399;
|
||||
)
|
||||
|
||||
var
|
||||
(
|
||||
rng_cooked [LEN]int64; // cooked random numbers
|
||||
rng_vec [LEN]int64; // current feedback register
|
||||
var (
|
||||
rng_cooked [_LEN]int64; // cooked random numbers
|
||||
rng_vec [_LEN]int64; // current feedback register
|
||||
rng_tap int; // index into vector
|
||||
rng_feed int; // index into vector
|
||||
)
|
||||
|
||||
func
|
||||
seedrand(x int32) int32
|
||||
{
|
||||
func seedrand(x int32) int32 {
|
||||
// seed rng x[n+1] = 48271 * x[n] mod (2**31 - 1)
|
||||
hi := x / Q;
|
||||
lo := x % Q;
|
||||
x = A*lo - R*hi;
|
||||
hi := x / _Q;
|
||||
lo := x % _Q;
|
||||
x = _A*lo - _R*hi;
|
||||
if x < 0 {
|
||||
x += M;
|
||||
x += _M;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
export func
|
||||
srand(seed int32)
|
||||
{
|
||||
export func Seed(seed int32) {
|
||||
rng_tap = 0;
|
||||
rng_feed = LEN-TAP;
|
||||
rng_feed = _LEN-_TAP;
|
||||
|
||||
seed = seed%M;
|
||||
seed = seed%_M;
|
||||
if seed < 0 {
|
||||
seed += M;
|
||||
seed += _M;
|
||||
}
|
||||
if seed == 0 {
|
||||
seed = 89482311;
|
||||
}
|
||||
|
||||
x := seed;
|
||||
for i := -20; i < LEN; i++ {
|
||||
for i := -20; i < _LEN; i++ {
|
||||
x = seedrand(x);
|
||||
if i >= 0 {
|
||||
var u int64;
|
||||
@ -73,105 +67,84 @@ srand(seed int32)
|
||||
x = seedrand(x);
|
||||
u ^= int64(x);
|
||||
u ^= rng_cooked[i];
|
||||
rng_vec[i] = u & MASK;
|
||||
rng_vec[i] = u & _MASK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export func
|
||||
rand63() int64
|
||||
{
|
||||
export func Int63() int64 {
|
||||
rng_tap--;
|
||||
if rng_tap < 0 {
|
||||
rng_tap += LEN;
|
||||
rng_tap += _LEN;
|
||||
}
|
||||
|
||||
rng_feed--;
|
||||
if rng_feed < 0 {
|
||||
rng_feed += LEN;
|
||||
rng_feed += _LEN;
|
||||
}
|
||||
|
||||
x := (rng_vec[rng_feed] + rng_vec[rng_tap]) & MASK;
|
||||
x := (rng_vec[rng_feed] + rng_vec[rng_tap]) & _MASK;
|
||||
rng_vec[rng_feed] = x;
|
||||
return x;
|
||||
}
|
||||
|
||||
export func
|
||||
urand32() uint32
|
||||
{
|
||||
return uint32(rand63() >> 31);
|
||||
export func Uint32() uint32 {
|
||||
return uint32(Int63() >> 31);
|
||||
}
|
||||
|
||||
export func
|
||||
rand31() int32
|
||||
{
|
||||
return int32(rand63() >> 32);
|
||||
export func Int31() int32 {
|
||||
return int32(Int63() >> 32);
|
||||
}
|
||||
|
||||
export func
|
||||
rand() int
|
||||
{
|
||||
u := uint(rand63());
|
||||
export func Int() int {
|
||||
u := uint(Int63());
|
||||
return int(u << 1 >> 1); // clear sign bit if int == int32
|
||||
}
|
||||
|
||||
export func
|
||||
nrand63(n int64) int64
|
||||
{
|
||||
export func Int63n(n int64) int64 {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
max := int64((1<<63)-1 - (1<<63) % uint64(n));
|
||||
v := rand63();
|
||||
v := Int63();
|
||||
for v > max {
|
||||
v = rand63()
|
||||
v = Int63()
|
||||
}
|
||||
return v % n
|
||||
}
|
||||
|
||||
export func
|
||||
nrand31(n int32) int32
|
||||
{
|
||||
return int32(nrand63(int64(n)))
|
||||
export func Int31n(n int32) int32 {
|
||||
return int32(Int63n(int64(n)))
|
||||
}
|
||||
|
||||
export func
|
||||
nrand(n int) int
|
||||
{
|
||||
return int(nrand63(int64(n)))
|
||||
export func Intn(n int) int {
|
||||
return int(Int63n(int64(n)))
|
||||
}
|
||||
|
||||
export func
|
||||
frand64() float64
|
||||
{
|
||||
x := float64(rand63()) / float64(MASK);
|
||||
export func Float64() float64 {
|
||||
x := float64(Int63()) / float64(_MASK);
|
||||
for x >= 1 {
|
||||
x = float64(rand63()) / float64(MASK);
|
||||
x = float64(Int63()) / float64(_MASK);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
export func
|
||||
frand32() float32
|
||||
{
|
||||
return float32(frand64())
|
||||
export func Float32() float32 {
|
||||
return float32(Float64())
|
||||
}
|
||||
|
||||
export func
|
||||
frand() float
|
||||
export func Float() float
|
||||
{
|
||||
return float(frand64())
|
||||
return float(Float64())
|
||||
}
|
||||
|
||||
export func
|
||||
perm(n int) []int
|
||||
{
|
||||
export func Perm(n int) []int {
|
||||
m := make([]int, n);
|
||||
for i:=0; i<n; i++ {
|
||||
m[i] = i;
|
||||
}
|
||||
for i:=0; i<n; i++ {
|
||||
j := nrand(n);
|
||||
j := Intn(n);
|
||||
t := m[i];
|
||||
m[i] = m[j];
|
||||
m[j] = t;
|
||||
@ -179,9 +152,7 @@ perm(n int) []int
|
||||
return m;
|
||||
}
|
||||
|
||||
func
|
||||
init()
|
||||
{
|
||||
func init() {
|
||||
// the state of the rng
|
||||
// after 780e10 iterations
|
||||
|
||||
@ -793,5 +764,5 @@ init()
|
||||
rng_cooked[605] = 9103922860780351547;
|
||||
rng_cooked[606] = 4152330101494654406;
|
||||
|
||||
srand(1);
|
||||
Seed(1);
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ export func TestSortStrings(t *testing.T) {
|
||||
export func TestSortLarge_Random(t *testing.T) {
|
||||
data := make([]int, 1000000);
|
||||
for i := 0; i < len(data); i++ {
|
||||
data[i] = rand.rand() % 100;
|
||||
data[i] = rand.Intn(100);
|
||||
}
|
||||
if sort.IntsAreSorted(data) {
|
||||
t.Fatalf("terrible rand.rand");
|
||||
@ -150,13 +150,13 @@ export func TestBentleyMcIlroy(t *testing.T) {
|
||||
case _Sawtooth:
|
||||
data[i] = i % m;
|
||||
case _Rand:
|
||||
data[i] = rand.rand() % m;
|
||||
data[i] = rand.Intn(m);
|
||||
case _Stagger:
|
||||
data[i] = (i*m + i) % n;
|
||||
case _Plateau:
|
||||
data[i] = min(i, m);
|
||||
case _Shuffle:
|
||||
if rand.rand() % m != 0 {
|
||||
if rand.Intn(m) != 0 {
|
||||
j += 2;
|
||||
data[i] = j;
|
||||
} else {
|
||||
|
@ -7,7 +7,7 @@ package strings
|
||||
import "utf8"
|
||||
|
||||
// Split string into array of UTF-8 sequences (still strings)
|
||||
export func explode(s string) []string {
|
||||
export func Explode(s string) []string {
|
||||
a := make([]string, utf8.RuneCountInString(s, 0, len(s)));
|
||||
j := 0;
|
||||
var size, rune int;
|
||||
@ -20,7 +20,7 @@ export func explode(s string) []string {
|
||||
}
|
||||
|
||||
// Count non-overlapping instances of sep in s.
|
||||
export func count(s, sep string) int {
|
||||
export func Count(s, sep string) int {
|
||||
if sep == "" {
|
||||
return utf8.RuneCountInString(s, 0, len(s))+1
|
||||
}
|
||||
@ -36,7 +36,7 @@ export func count(s, sep string) int {
|
||||
}
|
||||
|
||||
// Return index of first instance of sep in s.
|
||||
export func index(s, sep string) int {
|
||||
export func Index(s, sep string) int {
|
||||
if sep == "" {
|
||||
return 0
|
||||
}
|
||||
@ -50,13 +50,13 @@ export func index(s, sep string) int {
|
||||
}
|
||||
|
||||
// Split string into list of strings at separators
|
||||
export func split(s, sep string) []string {
|
||||
export func Split(s, sep string) []string {
|
||||
if sep == "" {
|
||||
return explode(s)
|
||||
return Explode(s)
|
||||
}
|
||||
c := sep[0];
|
||||
start := 0;
|
||||
n := count(s, sep)+1;
|
||||
n := Count(s, sep)+1;
|
||||
a := make([]string, n);
|
||||
na := 0;
|
||||
for i := 0; i+len(sep) <= len(s); i++ {
|
||||
@ -72,7 +72,7 @@ export func split(s, sep string) []string {
|
||||
}
|
||||
|
||||
// Join list of strings with separators between them.
|
||||
export func join(a []string, sep string) string {
|
||||
export func Join(a []string, sep string) string {
|
||||
if len(a) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ var faces = "☺☻☹";
|
||||
var commas = "1,2,3,4";
|
||||
var dots = "1....2....3....4";
|
||||
|
||||
type ExplodeTest struct {
|
||||
export type ExplodeTest struct {
|
||||
s string;
|
||||
a []string;
|
||||
}
|
||||
@ -37,19 +37,19 @@ var explodetests = []ExplodeTest {
|
||||
export func TestExplode(t *testing.T) {
|
||||
for i := 0; i < len(explodetests); i++ {
|
||||
tt := explodetests[i];
|
||||
a := explode(tt.s);
|
||||
a := Explode(tt.s);
|
||||
if !eq(a, tt.a) {
|
||||
t.Errorf("Explode(%q) = %v; want %v", tt.s, a, tt.a);
|
||||
continue;
|
||||
}
|
||||
s := join(a, "");
|
||||
s := Join(a, "");
|
||||
if s != tt.s {
|
||||
t.Errorf(`Join(Explode(%q), "") = %q`, tt.s, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type SplitTest struct {
|
||||
export type SplitTest struct {
|
||||
s string;
|
||||
sep string;
|
||||
a []string;
|
||||
@ -67,12 +67,12 @@ var splittests = []SplitTest {
|
||||
export func TestSplit(t *testing.T) {
|
||||
for i := 0; i < len(splittests); i++ {
|
||||
tt := splittests[i];
|
||||
a := split(tt.s, tt.sep);
|
||||
a := Split(tt.s, tt.sep);
|
||||
if !eq(a, tt.a) {
|
||||
t.Errorf("Split(%q, %q) = %v; want %v", tt.s, tt.sep, a, tt.a);
|
||||
continue;
|
||||
}
|
||||
s := join(a, tt.sep);
|
||||
s := Join(a, tt.sep);
|
||||
if s != tt.s {
|
||||
t.Errorf("Join(Split(%q, %q), %q) = %q", tt.s, tt.sep, tt.sep, s);
|
||||
}
|
||||
|
130
src/lib/utf8.go
130
src/lib/utf8.go
@ -14,25 +14,25 @@ export const (
|
||||
)
|
||||
|
||||
const (
|
||||
T1 = 0x00; // 0000 0000
|
||||
Tx = 0x80; // 1000 0000
|
||||
T2 = 0xC0; // 1100 0000
|
||||
T3 = 0xE0; // 1110 0000
|
||||
T4 = 0xF0; // 1111 0000
|
||||
T5 = 0xF8; // 1111 1000
|
||||
_T1 = 0x00; // 0000 0000
|
||||
_Tx = 0x80; // 1000 0000
|
||||
_T2 = 0xC0; // 1100 0000
|
||||
_T3 = 0xE0; // 1110 0000
|
||||
_T4 = 0xF0; // 1111 0000
|
||||
_T5 = 0xF8; // 1111 1000
|
||||
|
||||
Maskx = 0x3F; // 0011 1111
|
||||
Mask2 = 0x1F; // 0001 1111
|
||||
Mask3 = 0x0F; // 0000 1111
|
||||
Mask4 = 0x07; // 0000 0111
|
||||
_Maskx = 0x3F; // 0011 1111
|
||||
_Mask2 = 0x1F; // 0001 1111
|
||||
_Mask3 = 0x0F; // 0000 1111
|
||||
_Mask4 = 0x07; // 0000 0111
|
||||
|
||||
Rune1Max = 1<<7 - 1;
|
||||
Rune2Max = 1<<11 - 1;
|
||||
Rune3Max = 1<<16 - 1;
|
||||
Rune4Max = 1<<21 - 1;
|
||||
_Rune1Max = 1<<7 - 1;
|
||||
_Rune2Max = 1<<11 - 1;
|
||||
_Rune3Max = 1<<16 - 1;
|
||||
_Rune4Max = 1<<21 - 1;
|
||||
)
|
||||
|
||||
func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
func decodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
n := len(p);
|
||||
if n < 1 {
|
||||
return RuneError, 0, true;
|
||||
@ -40,12 +40,12 @@ func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
c0 := p[0];
|
||||
|
||||
// 1-byte, 7-bit sequence?
|
||||
if c0 < Tx {
|
||||
if c0 < _Tx {
|
||||
return int(c0), 1, false
|
||||
}
|
||||
|
||||
// unexpected continuation byte?
|
||||
if c0 < T2 {
|
||||
if c0 < _T2 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
@ -54,14 +54,14 @@ func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c1 := p[1];
|
||||
if c1 < Tx || T2 <= c1 {
|
||||
if c1 < _Tx || _T2 <= c1 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 2-byte, 11-bit sequence?
|
||||
if c0 < T3 {
|
||||
rune = int(c0&Mask2)<<6 | int(c1&Maskx);
|
||||
if rune <= Rune1Max {
|
||||
if c0 < _T3 {
|
||||
rune = int(c0&_Mask2)<<6 | int(c1&_Maskx);
|
||||
if rune <= _Rune1Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 2, false
|
||||
@ -72,14 +72,14 @@ func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c2 := p[2];
|
||||
if c2 < Tx || T2 <= c2 {
|
||||
if c2 < _Tx || _T2 <= c2 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 3-byte, 16-bit sequence?
|
||||
if c0 < T4 {
|
||||
rune = int(c0&Mask3)<<12 | int(c1&Maskx)<<6 | int(c2&Maskx);
|
||||
if rune <= Rune2Max {
|
||||
if c0 < _T4 {
|
||||
rune = int(c0&_Mask3)<<12 | int(c1&_Maskx)<<6 | int(c2&_Maskx);
|
||||
if rune <= _Rune2Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 3, false
|
||||
@ -90,14 +90,14 @@ func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c3 := p[3];
|
||||
if c3 < Tx || T2 <= c3 {
|
||||
if c3 < _Tx || _T2 <= c3 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 4-byte, 21-bit sequence?
|
||||
if c0 < T5 {
|
||||
rune = int(c0&Mask4)<<18 | int(c1&Maskx)<<12 | int(c2&Maskx)<<6 | int(c3&Maskx);
|
||||
if rune <= Rune3Max {
|
||||
if c0 < _T5 {
|
||||
rune = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx);
|
||||
if rune <= _Rune3Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 4, false
|
||||
@ -107,19 +107,19 @@ func DecodeRuneInternal(p []byte) (rune, size int, short bool) {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
func DecodeRuneInStringInternal(s string, i int, n int) (rune, size int, short bool) {
|
||||
func decodeRuneInStringInternal(s string, i int, n int) (rune, size int, short bool) {
|
||||
if n < 1 {
|
||||
return RuneError, 0, true;
|
||||
}
|
||||
c0 := s[i];
|
||||
|
||||
// 1-byte, 7-bit sequence?
|
||||
if c0 < Tx {
|
||||
if c0 < _Tx {
|
||||
return int(c0), 1, false
|
||||
}
|
||||
|
||||
// unexpected continuation byte?
|
||||
if c0 < T2 {
|
||||
if c0 < _T2 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
@ -128,14 +128,14 @@ func DecodeRuneInStringInternal(s string, i int, n int) (rune, size int, short b
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c1 := s[i+1];
|
||||
if c1 < Tx || T2 <= c1 {
|
||||
if c1 < _Tx || _T2 <= c1 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 2-byte, 11-bit sequence?
|
||||
if c0 < T3 {
|
||||
rune = int(c0&Mask2)<<6 | int(c1&Maskx);
|
||||
if rune <= Rune1Max {
|
||||
if c0 < _T3 {
|
||||
rune = int(c0&_Mask2)<<6 | int(c1&_Maskx);
|
||||
if rune <= _Rune1Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 2, false
|
||||
@ -146,14 +146,14 @@ func DecodeRuneInStringInternal(s string, i int, n int) (rune, size int, short b
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c2 := s[i+2];
|
||||
if c2 < Tx || T2 <= c2 {
|
||||
if c2 < _Tx || _T2 <= c2 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 3-byte, 16-bit sequence?
|
||||
if c0 < T4 {
|
||||
rune = int(c0&Mask3)<<12 | int(c1&Maskx)<<6 | int(c2&Maskx);
|
||||
if rune <= Rune2Max {
|
||||
if c0 < _T4 {
|
||||
rune = int(c0&_Mask3)<<12 | int(c1&_Maskx)<<6 | int(c2&_Maskx);
|
||||
if rune <= _Rune2Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 3, false
|
||||
@ -164,14 +164,14 @@ func DecodeRuneInStringInternal(s string, i int, n int) (rune, size int, short b
|
||||
return RuneError, 1, true
|
||||
}
|
||||
c3 := s[i+3];
|
||||
if c3 < Tx || T2 <= c3 {
|
||||
if c3 < _Tx || _T2 <= c3 {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
|
||||
// 4-byte, 21-bit sequence?
|
||||
if c0 < T5 {
|
||||
rune = int(c0&Mask4)<<18 | int(c1&Maskx)<<12 | int(c2&Maskx)<<6 | int(c3&Maskx);
|
||||
if rune <= Rune3Max {
|
||||
if c0 < _T5 {
|
||||
rune = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx);
|
||||
if rune <= _Rune3Max {
|
||||
return RuneError, 1, false
|
||||
}
|
||||
return rune, 4, false
|
||||
@ -182,50 +182,50 @@ func DecodeRuneInStringInternal(s string, i int, n int) (rune, size int, short b
|
||||
}
|
||||
|
||||
export func FullRune(p []byte) bool {
|
||||
rune, size, short := DecodeRuneInternal(p);
|
||||
rune, size, short := decodeRuneInternal(p);
|
||||
return !short
|
||||
}
|
||||
|
||||
export func FullRuneInString(s string, i int) bool {
|
||||
rune, size, short := DecodeRuneInStringInternal(s, i, len(s) - i);
|
||||
rune, size, short := decodeRuneInStringInternal(s, i, len(s) - i);
|
||||
return !short
|
||||
}
|
||||
|
||||
export func DecodeRune(p []byte) (rune, size int) {
|
||||
var short bool;
|
||||
rune, size, short = DecodeRuneInternal(p);
|
||||
rune, size, short = decodeRuneInternal(p);
|
||||
return;
|
||||
}
|
||||
|
||||
export func DecodeRuneInString(s string, i int) (rune, size int) {
|
||||
var short bool;
|
||||
rune, size, short = DecodeRuneInStringInternal(s, i, len(s) - i);
|
||||
rune, size, short = decodeRuneInStringInternal(s, i, len(s) - i);
|
||||
return;
|
||||
}
|
||||
|
||||
export func RuneLen(rune int) int {
|
||||
switch {
|
||||
case rune <= Rune1Max:
|
||||
case rune <= _Rune1Max:
|
||||
return 1;
|
||||
case rune <= Rune2Max:
|
||||
case rune <= _Rune2Max:
|
||||
return 2;
|
||||
case rune <= Rune3Max:
|
||||
case rune <= _Rune3Max:
|
||||
return 3;
|
||||
case rune <= Rune4Max:
|
||||
case rune <= _Rune4Max:
|
||||
return 4;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
export func EncodeRune(rune int, p []byte) int {
|
||||
if rune <= Rune1Max {
|
||||
if rune <= _Rune1Max {
|
||||
p[0] = byte(rune);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if rune <= Rune2Max {
|
||||
p[0] = T2 | byte(rune>>6);
|
||||
p[1] = Tx | byte(rune)&Maskx;
|
||||
if rune <= _Rune2Max {
|
||||
p[0] = _T2 | byte(rune>>6);
|
||||
p[1] = _Tx | byte(rune)&_Maskx;
|
||||
return 2;
|
||||
}
|
||||
|
||||
@ -233,17 +233,17 @@ export func EncodeRune(rune int, p []byte) int {
|
||||
rune = RuneError
|
||||
}
|
||||
|
||||
if rune <= Rune3Max {
|
||||
p[0] = T3 | byte(rune>>12);
|
||||
p[1] = Tx | byte(rune>>6)&Maskx;
|
||||
p[2] = Tx | byte(rune)&Maskx;
|
||||
if rune <= _Rune3Max {
|
||||
p[0] = _T3 | byte(rune>>12);
|
||||
p[1] = _Tx | byte(rune>>6)&_Maskx;
|
||||
p[2] = _Tx | byte(rune)&_Maskx;
|
||||
return 3;
|
||||
}
|
||||
|
||||
p[0] = T4 | byte(rune>>18);
|
||||
p[1] = Tx | byte(rune>>12)&Maskx;
|
||||
p[2] = Tx | byte(rune>>6)&Maskx;
|
||||
p[3] = Tx | byte(rune)&Maskx;
|
||||
p[0] = _T4 | byte(rune>>18);
|
||||
p[1] = _Tx | byte(rune>>12)&_Maskx;
|
||||
p[2] = _Tx | byte(rune>>6)&_Maskx;
|
||||
p[3] = _Tx | byte(rune)&_Maskx;
|
||||
return 4;
|
||||
}
|
||||
|
||||
@ -268,7 +268,7 @@ export func RuneCountInString(s string, i int, l int) int {
|
||||
if s[i] < RuneSelf {
|
||||
i++;
|
||||
} else {
|
||||
rune, size, short := DecodeRuneInStringInternal(s, i, ei - i);
|
||||
rune, size, short := decodeRuneInStringInternal(s, i, ei - i);
|
||||
i += size;
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"utf8";
|
||||
)
|
||||
|
||||
type Utf8Map struct {
|
||||
export type Utf8Map struct {
|
||||
rune int;
|
||||
str string;
|
||||
}
|
||||
@ -44,10 +44,11 @@ var utf8map = []Utf8Map {
|
||||
Utf8Map{ 0x10ffff, "\xf4\x8f\xbf\xbf" },
|
||||
}
|
||||
|
||||
func Bytes(s string) []byte {
|
||||
// like io.StringBytes but leaves one extra byte at end
|
||||
func bytes(s string) []byte {
|
||||
b := make([]byte, len(s)+1);
|
||||
if !syscall.StringToBytes(b, s) {
|
||||
panic("StringToBytes failed");
|
||||
panic("StringTobytes failed");
|
||||
}
|
||||
return b[0:len(s)];
|
||||
}
|
||||
@ -55,7 +56,7 @@ func Bytes(s string) []byte {
|
||||
export func TestFullRune(t *testing.T) {
|
||||
for i := 0; i < len(utf8map); i++ {
|
||||
m := utf8map[i];
|
||||
b := Bytes(m.str);
|
||||
b := bytes(m.str);
|
||||
if !utf8.FullRune(b) {
|
||||
t.Errorf("FullRune(%q) (rune %04x) = false, want true", b, m.rune);
|
||||
}
|
||||
@ -74,7 +75,7 @@ export func TestFullRune(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func EqualBytes(a, b []byte) bool {
|
||||
func equalBytes(a, b []byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false;
|
||||
}
|
||||
@ -89,11 +90,11 @@ func EqualBytes(a, b []byte) bool {
|
||||
export func TestEncodeRune(t *testing.T) {
|
||||
for i := 0; i < len(utf8map); i++ {
|
||||
m := utf8map[i];
|
||||
b := Bytes(m.str);
|
||||
b := bytes(m.str);
|
||||
var buf [10]byte;
|
||||
n := utf8.EncodeRune(m.rune, buf);
|
||||
b1 := buf[0:n];
|
||||
if !EqualBytes(b, b1) {
|
||||
if !equalBytes(b, b1) {
|
||||
t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b);
|
||||
}
|
||||
}
|
||||
@ -102,7 +103,7 @@ export func TestEncodeRune(t *testing.T) {
|
||||
export func TestDecodeRune(t *testing.T) {
|
||||
for i := 0; i < len(utf8map); i++ {
|
||||
m := utf8map[i];
|
||||
b := Bytes(m.str);
|
||||
b := bytes(m.str);
|
||||
rune, size := utf8.DecodeRune(b);
|
||||
if rune != m.rune || size != len(b) {
|
||||
t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b));
|
||||
@ -113,7 +114,7 @@ export func TestDecodeRune(t *testing.T) {
|
||||
t.Errorf("DecodeRune(%q, 2) = 0x%04x, %d want 0x%04x, %d", s, rune, size, m.rune, len(b));
|
||||
}
|
||||
|
||||
// there's an extra byte that Bytes left behind - make sure trailing byte works
|
||||
// there's an extra byte that bytes left behind - make sure trailing byte works
|
||||
rune, size = utf8.DecodeRune(b[0:cap(b)]);
|
||||
if rune != m.rune || size != len(b) {
|
||||
t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b));
|
||||
@ -157,7 +158,7 @@ export func TestDecodeRune(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type RuneCountTest struct {
|
||||
export type RuneCountTest struct {
|
||||
in string;
|
||||
out int;
|
||||
}
|
||||
@ -173,7 +174,7 @@ export func TestRuneCount(t *testing.T) {
|
||||
if out := utf8.RuneCountInString(tt.in, 0, len(tt.in)); out != tt.out {
|
||||
t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out);
|
||||
}
|
||||
if out := utf8.RuneCount(Bytes(tt.in)); out != tt.out {
|
||||
if out := utf8.RuneCount(bytes(tt.in)); out != tt.out {
|
||||
t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user