Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
aedfb397
Commit
aedfb397
authored
Jan 16, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify misc
R=r DELTA=247 (20 added, 50 deleted, 177 changed) OCL=22951 CL=22955
parent
116a6e9c
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
176 additions
and
206 deletions
+176
-206
src/lib/http/request.go
src/lib/http/request.go
+1
-1
src/lib/http/url.go
src/lib/http/url.go
+1
-1
src/lib/io/io.go
src/lib/io/io.go
+6
-6
src/lib/json/generic.go
src/lib/json/generic.go
+1
-1
src/lib/malloc.go
src/lib/malloc.go
+1
-1
src/lib/net/dnsclient.go
src/lib/net/dnsclient.go
+1
-1
src/lib/once.go
src/lib/once.go
+11
-13
src/lib/once_test.go
src/lib/once_test.go
+7
-7
src/lib/rand.go
src/lib/rand.go
+52
-81
src/lib/sort_test.go
src/lib/sort_test.go
+3
-3
src/lib/strings.go
src/lib/strings.go
+7
-7
src/lib/strings_test.go
src/lib/strings_test.go
+6
-6
src/lib/utf8.go
src/lib/utf8.go
+67
-67
src/lib/utf8_test.go
src/lib/utf8_test.go
+12
-11
No files found.
src/lib/http/request.go
View file @
aedfb397
...
...
@@ -190,7 +190,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
}
var
f
[]
string
;
if
f
=
strings
.
s
plit
(
s
,
" "
);
len
(
f
)
!=
3
{
if
f
=
strings
.
S
plit
(
s
,
" "
);
len
(
f
)
!=
3
{
return
nil
,
BadRequest
}
req
.
method
,
req
.
rawurl
,
req
.
proto
=
f
[
0
],
f
[
1
],
f
[
2
];
...
...
src/lib/http/url.go
View file @
aedfb397
...
...
@@ -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
.
i
ndex
(
url
.
authority
,
"@"
)
<
0
{
if
strings
.
I
ndex
(
url
.
authority
,
"@"
)
<
0
{
url
.
host
=
url
.
authority
;
}
else
{
url
.
userinfo
,
url
.
host
=
split
(
url
.
authority
,
'@'
,
true
);
...
...
src/lib/io/io.go
View file @
aedfb397
...
...
@@ -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.
...
...
src/lib/json/generic.go
View file @
aedfb397
...
...
@@ -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
.
i
ndex
(
path
,
"/"
);
i
>=
0
{
if
i
:=
strings
.
I
ndex
(
path
,
"/"
);
i
>=
0
{
elem
=
path
[
0
:
i
];
path
=
path
[
i
+
1
:
len
(
path
)];
}
else
{
...
...
src/lib/malloc.go
View file @
aedfb397
...
...
@@ -8,7 +8,7 @@
package
malloc
type
Stats
struct
{
export
type
Stats
struct
{
alloc
uint64
;
sys
uint64
;
};
...
...
src/lib/net/dnsclient.go
View file @
aedfb397
...
...
@@ -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
.
c
ount
(
name
,
"."
)
>=
cfg
.
ndots
{
if
rooted
||
strings
.
C
ount
(
name
,
"."
)
>=
cfg
.
ndots
{
rname
:=
name
;
if
!
rooted
{
rname
+=
"."
;
...
...
src/lib/once.go
View file @
aedfb397
...
...
@@ -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
S
erver
()
{
func
s
erver
()
{
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
S
erver
()
go
s
erver
()
}
src/lib/once_test.go
View file @
aedfb397
...
...
@@ -10,22 +10,22 @@ import (
)
var
ncall
int
;
func
C
all
()
{
func
c
all
()
{
ncall
++
}
export
func
TestOnce
(
t
*
testing
.
T
)
{
ncall
=
0
;
once
.
Do
(
&
C
all
);
once
.
Do
(
&
c
all
);
if
ncall
!=
1
{
t
.
Fatalf
(
"once.Do(&
Call) didn't C
all(): ncall=%d"
,
ncall
);
t
.
Fatalf
(
"once.Do(&
call) didn't c
all(): ncall=%d"
,
ncall
);
}
once
.
Do
(
&
C
all
);
once
.
Do
(
&
c
all
);
if
ncall
!=
1
{
t
.
Fatalf
(
"second once.Do(&
Call) did C
all(): ncall=%d"
,
ncall
);
t
.
Fatalf
(
"second once.Do(&
call) did c
all(): ncall=%d"
,
ncall
);
}
once
.
Do
(
&
C
all
);
once
.
Do
(
&
c
all
);
if
ncall
!=
1
{
t
.
Fatalf
(
"third once.Do(&
Call) did C
all(): ncall=%d"
,
ncall
);
t
.
Fatalf
(
"third once.Do(&
call) did c
all(): ncall=%d"
,
ncall
);
}
}
src/lib/rand.go
View file @
aedfb397
...
...
@@ -10,60 +10,54 @@
package
rand
// rand, rand31,
rand
63 - return non-negative random int, int32, int64
// rand, rand31,
Int
63 - 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
:=
rand
63
();
v
:=
Int
63
();
for
v
>
max
{
v
=
rand
63
()
v
=
Int
63
()
}
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
(
frand
64
())
return
float
(
Float
64
())
}
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
;
sran
d
(
1
);
See
d
(
1
);
}
src/lib/sort_test.go
View file @
aedfb397
...
...
@@ -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
{
...
...
src/lib/strings.go
View file @
aedfb397
...
...
@@ -7,7 +7,7 @@ package strings
import
"utf8"
// Split string into array of UTF-8 sequences (still strings)
export
func
e
xplode
(
s
string
)
[]
string
{
export
func
E
xplode
(
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
c
ount
(
s
,
sep
string
)
int
{
export
func
C
ount
(
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
i
ndex
(
s
,
sep
string
)
int
{
export
func
I
ndex
(
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
s
plit
(
s
,
sep
string
)
[]
string
{
export
func
S
plit
(
s
,
sep
string
)
[]
string
{
if
sep
==
""
{
return
e
xplode
(
s
)
return
E
xplode
(
s
)
}
c
:=
sep
[
0
];
start
:=
0
;
n
:=
c
ount
(
s
,
sep
)
+
1
;
n
:=
C
ount
(
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
j
oin
(
a
[]
string
,
sep
string
)
string
{
export
func
J
oin
(
a
[]
string
,
sep
string
)
string
{
if
len
(
a
)
==
0
{
return
""
}
...
...
src/lib/strings_test.go
View file @
aedfb397
...
...
@@ -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
:=
e
xplode
(
tt
.
s
);
a
:=
E
xplode
(
tt
.
s
);
if
!
eq
(
a
,
tt
.
a
)
{
t
.
Errorf
(
"Explode(%q) = %v; want %v"
,
tt
.
s
,
a
,
tt
.
a
);
continue
;
}
s
:=
j
oin
(
a
,
""
);
s
:=
J
oin
(
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
:=
s
plit
(
tt
.
s
,
tt
.
sep
);
a
:=
S
plit
(
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
:=
j
oin
(
a
,
tt
.
sep
);
s
:=
J
oin
(
a
,
tt
.
sep
);
if
s
!=
tt
.
s
{
t
.
Errorf
(
"Join(Split(%q, %q), %q) = %q"
,
tt
.
s
,
tt
.
sep
,
tt
.
sep
,
s
);
}
...
...
src/lib/utf8.go
View file @
aedfb397
...
...
@@ -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
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
;
_
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
_
Rune1Max
=
1
<<
7
-
1
;
_
Rune2Max
=
1
<<
11
-
1
;
_
Rune3Max
=
1
<<
16
-
1
;
_
Rune4Max
=
1
<<
21
-
1
;
)
func
D
ecodeRuneInternal
(
p
[]
byte
)
(
rune
,
size
int
,
short
bool
)
{
func
d
ecodeRuneInternal
(
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
D
ecodeRuneInStringInternal
(
s
string
,
i
int
,
n
int
)
(
rune
,
size
int
,
short
bool
)
{
func
d
ecodeRuneInStringInternal
(
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
:=
D
ecodeRuneInternal
(
p
);
rune
,
size
,
short
:=
d
ecodeRuneInternal
(
p
);
return
!
short
}
export
func
FullRuneInString
(
s
string
,
i
int
)
bool
{
rune
,
size
,
short
:=
D
ecodeRuneInStringInternal
(
s
,
i
,
len
(
s
)
-
i
);
rune
,
size
,
short
:=
d
ecodeRuneInStringInternal
(
s
,
i
,
len
(
s
)
-
i
);
return
!
short
}
export
func
DecodeRune
(
p
[]
byte
)
(
rune
,
size
int
)
{
var
short
bool
;
rune
,
size
,
short
=
D
ecodeRuneInternal
(
p
);
rune
,
size
,
short
=
d
ecodeRuneInternal
(
p
);
return
;
}
export
func
DecodeRuneInString
(
s
string
,
i
int
)
(
rune
,
size
int
)
{
var
short
bool
;
rune
,
size
,
short
=
D
ecodeRuneInStringInternal
(
s
,
i
,
len
(
s
)
-
i
);
rune
,
size
,
short
=
d
ecodeRuneInStringInternal
(
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
:=
D
ecodeRuneInStringInternal
(
s
,
i
,
ei
-
i
);
rune
,
size
,
short
:=
d
ecodeRuneInStringInternal
(
s
,
i
,
ei
-
i
);
i
+=
size
;
}
}
...
...
src/lib/utf8_test.go
View file @
aedfb397
...
...
@@ -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
(
"StringTo
B
ytes failed"
);
panic
(
"StringTo
b
ytes 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
:=
B
ytes
(
m
.
str
);
b
:=
b
ytes
(
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
E
qualBytes
(
a
,
b
[]
byte
)
bool
{
func
e
qualBytes
(
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
:=
B
ytes
(
m
.
str
);
b
:=
b
ytes
(
m
.
str
);
var
buf
[
10
]
byte
;
n
:=
utf8
.
EncodeRune
(
m
.
rune
,
buf
);
b1
:=
buf
[
0
:
n
];
if
!
E
qualBytes
(
b
,
b1
)
{
if
!
e
qualBytes
(
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
:=
B
ytes
(
m
.
str
);
b
:=
b
ytes
(
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
B
ytes left behind - make sure trailing byte works
// there's an extra byte that
b
ytes 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
(
B
ytes
(
tt
.
in
));
out
!=
tt
.
out
{
if
out
:=
utf8
.
RuneCount
(
b
ytes
(
tt
.
in
));
out
!=
tt
.
out
{
t
.
Errorf
(
"RuneCount(%q) = %d, want %d"
,
tt
.
in
,
out
,
tt
.
out
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment