Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
ea3d4540
Commit
ea3d4540
authored
Mar 03, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- itob
- more test cases R=rsc DELTA=97 (52 added, 4 deleted, 41 changed) OCL=25585 CL=25607
parent
426335f8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
44 deletions
+92
-44
itoa.go
src/lib/strconv/itoa.go
+25
-14
itoa_test.go
src/lib/strconv/itoa_test.go
+67
-30
No files found.
src/lib/strconv/itoa.go
View file @
ea3d4540
...
...
@@ -4,34 +4,45 @@
package
strconv
func
Ito
a64
(
i
int64
)
string
{
func
Ito
b64
(
i
int64
,
base
uint
)
string
{
if
i
==
0
{
return
"0"
}
neg
:=
false
;
// negative
u
:=
uint64
(
i
);
if
i
<
0
{
neg
=
true
;
u
=
-
u
;
}
// Assemble decimal in reverse order.
var
b
[
32
]
byte
;
bp
:=
len
(
b
);
for
;
u
>
0
;
u
/=
10
{
bp
--
;
b
[
bp
]
=
byte
(
u
%
10
)
+
'0'
var
buf
[
32
]
byte
;
j
:=
len
(
buf
);
b
:=
uint64
(
base
);
for
u
>
0
{
j
--
;
buf
[
j
]
=
"0123456789abcdefghijklmnopqrstuvwxyz"
[
u
%
b
];
u
/=
b
;
}
if
neg
{
// add sign
bp
--
;
b
[
bp
]
=
'-'
if
i
<
0
{
// add sign
j
--
;
buf
[
j
]
=
'-'
}
return
string
(
b
[
bp
:
len
(
b
)])
return
string
(
b
uf
[
j
:
len
(
buf
)])
}
func
Itoa
(
i
int
)
string
{
return
Itoa64
(
int64
(
i
));
func
Itoa64
(
i
int64
)
string
{
return
Itob64
(
i
,
10
);
}
func
Itob
(
i
int
,
base
uint
)
string
{
return
Itob64
(
int64
(
i
),
base
);
}
func
Itoa
(
i
int
)
string
{
return
Itob64
(
int64
(
i
),
10
);
}
src/lib/strconv/itoa_test.go
View file @
ea3d4540
...
...
@@ -11,49 +11,86 @@ import (
"testing"
;
)
type
ito
a
64Test
struct
{
type
ito
b
64Test
struct
{
in
int64
;
base
uint
;
out
string
;
}
var
itoa64tests
=
[]
itoa64Test
(
itoa64Test
(
0
,
"0"
),
itoa64Test
(
1
,
"1"
),
itoa64Test
(
-
1
,
"-1"
),
itoa64Test
(
12345678
,
"12345678"
),
itoa64Test
(
-
987654321
,
"-987654321"
),
itoa64Test
(
1
<<
31
-
1
,
"2147483647"
),
itoa64Test
(
-
1
<<
31
+
1
,
"-2147483647"
),
itoa64Test
(
1
<<
31
,
"2147483648"
),
itoa64Test
(
-
1
<<
31
,
"-2147483648"
),
itoa64Test
(
1
<<
31
+
1
,
"2147483649"
),
itoa64Test
(
-
1
<<
31
-
1
,
"-2147483649"
),
itoa64Test
(
1
<<
32
-
1
,
"4294967295"
),
itoa64Test
(
-
1
<<
32
+
1
,
"-4294967295"
),
itoa64Test
(
1
<<
32
,
"4294967296"
),
itoa64Test
(
-
1
<<
32
,
"-4294967296"
),
itoa64Test
(
1
<<
32
+
1
,
"4294967297"
),
itoa64Test
(
-
1
<<
32
-
1
,
"-4294967297"
),
itoa64Test
(
1
<<
50
,
"1125899906842624"
),
itoa64Test
(
1
<<
63
-
1
,
"9223372036854775807"
),
itoa64Test
(
-
1
<<
63
+
1
,
"-9223372036854775807"
),
itoa64Test
(
-
1
<<
63
,
"-9223372036854775808"
),
var
itob64tests
=
[]
itob64Test
(
itob64Test
(
0
,
10
,
"0"
),
itob64Test
(
1
,
10
,
"1"
),
itob64Test
(
-
1
,
10
,
"-1"
),
itob64Test
(
12345678
,
10
,
"12345678"
),
itob64Test
(
-
987654321
,
10
,
"-987654321"
),
itob64Test
(
1
<<
31
-
1
,
10
,
"2147483647"
),
itob64Test
(
-
1
<<
31
+
1
,
10
,
"-2147483647"
),
itob64Test
(
1
<<
31
,
10
,
"2147483648"
),
itob64Test
(
-
1
<<
31
,
10
,
"-2147483648"
),
itob64Test
(
1
<<
31
+
1
,
10
,
"2147483649"
),
itob64Test
(
-
1
<<
31
-
1
,
10
,
"-2147483649"
),
itob64Test
(
1
<<
32
-
1
,
10
,
"4294967295"
),
itob64Test
(
-
1
<<
32
+
1
,
10
,
"-4294967295"
),
itob64Test
(
1
<<
32
,
10
,
"4294967296"
),
itob64Test
(
-
1
<<
32
,
10
,
"-4294967296"
),
itob64Test
(
1
<<
32
+
1
,
10
,
"4294967297"
),
itob64Test
(
-
1
<<
32
-
1
,
10
,
"-4294967297"
),
itob64Test
(
1
<<
50
,
10
,
"1125899906842624"
),
itob64Test
(
1
<<
63
-
1
,
10
,
"9223372036854775807"
),
itob64Test
(
-
1
<<
63
+
1
,
10
,
"-9223372036854775807"
),
itob64Test
(
-
1
<<
63
,
10
,
"-9223372036854775808"
),
itob64Test
(
0
,
2
,
"0"
),
itob64Test
(
10
,
2
,
"1010"
),
itob64Test
(
-
1
,
2
,
"-1"
),
itob64Test
(
1
<<
15
,
2
,
"1000000000000000"
),
itob64Test
(
-
8
,
8
,
"-10"
),
itob64Test
(
057635436545
,
8
,
"57635436545"
),
itob64Test
(
1
<<
24
,
8
,
"100000000"
),
itob64Test
(
16
,
16
,
"10"
),
itob64Test
(
-
0x123456789abcdef
,
16
,
"-123456789abcdef"
),
itob64Test
(
1
<<
63
-
1
,
16
,
"7fffffffffffffff"
),
itob64Test
(
16
,
17
,
"g"
),
itob64Test
(
25
,
25
,
"10"
),
itob64Test
(
(((((
17
*
35
+
24
)
*
35
+
21
)
*
35
+
34
)
*
35
+
12
)
*
35
+
24
)
*
35
+
32
,
35
,
"holycow"
),
itob64Test
(
(((((
17
*
36
+
24
)
*
36
+
21
)
*
36
+
34
)
*
36
+
12
)
*
36
+
24
)
*
36
+
32
,
36
,
"holycow"
),
)
func
TestItoa
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
len
(
itoa64tests
);
i
++
{
test
:=
itoa64tests
[
i
];
s
:=
strconv
.
Itoa64
(
test
.
in
);
for
i
:=
0
;
i
<
len
(
itob64tests
);
i
++
{
test
:=
itob64tests
[
i
];
s
:=
strconv
.
Itob64
(
test
.
in
,
test
.
base
);
if
s
!=
test
.
out
{
t
.
Error
(
"strconv.Itoa64(
%v) = %v want %v
\n
"
,
test
.
in
,
s
,
test
.
out
);
t
.
Error
f
(
"strconv.Itob64(%v,
%v) = %v want %v
\n
"
,
test
.
in
,
test
.
base
,
s
,
test
.
out
);
}
if
int64
(
int
(
test
.
in
))
==
test
.
in
{
s
:=
strconv
.
Ito
a
(
int
(
test
.
in
)
);
s
:=
strconv
.
Ito
b
(
int
(
test
.
in
),
test
.
base
);
if
s
!=
test
.
out
{
t
.
Error
(
"strconv.Itoa(%v) = %v want %v
\n
"
,
t
.
Errorf
(
"strconv.Itob(%v, %v) = %v want %v
\n
"
,
test
.
in
,
test
.
base
,
s
,
test
.
out
);
}
}
if
test
.
base
==
10
{
s
:=
strconv
.
Itoa64
(
test
.
in
);
if
s
!=
test
.
out
{
t
.
Errorf
(
"strconv.Itoa64(%v) = %v want %v
\n
"
,
test
.
in
,
s
,
test
.
out
);
}
if
int64
(
int
(
test
.
in
))
==
test
.
in
{
s
:=
strconv
.
Itoa
(
int
(
test
.
in
));
if
s
!=
test
.
out
{
t
.
Errorf
(
"strconv.Itoa(%v) = %v want %v
\n
"
,
test
.
in
,
s
,
test
.
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