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
d3013d8a
Commit
d3013d8a
authored
Sep 13, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add and AddByte
R=rsc DELTA=83 (83 added, 0 deleted, 0 changed) OCL=34584 CL=34584
parent
61dd8363
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
0 deletions
+83
-0
src/pkg/bytes/bytes.go
src/pkg/bytes/bytes.go
+42
-0
src/pkg/bytes/bytes_test.go
src/pkg/bytes/bytes_test.go
+41
-0
No files found.
src/pkg/bytes/bytes.go
View file @
d3013d8a
...
...
@@ -260,3 +260,45 @@ func TrimSpace(s []byte) []byte {
}
return
s
[
start
:
end
];
}
// How big to make a byte array when growing.
// Heuristic: Scale by 50% to give n log n time.
func
resize
(
n
int
)
int
{
if
n
<
16
{
n
=
16
}
return
n
+
n
/
2
;
}
// Add appends the contents of t to the end of s and returns the result.
// If s has enough capacity, it is extended in place; otherwise a
// new array is allocated and returned.
func
Add
(
s
,
t
[]
byte
)
[]
byte
{
lens
:=
len
(
s
);
lent
:=
len
(
t
);
if
lens
+
lent
<=
cap
(
s
)
{
s
=
s
[
0
:
lens
+
lent
];
}
else
{
news
:=
make
([]
byte
,
lens
+
lent
,
resize
(
lens
+
lent
));
Copy
(
news
,
s
);
s
=
news
;
}
Copy
(
s
[
lens
:
lens
+
lent
],
t
);
return
s
;
}
// AddByte appends byte b to the end of s and returns the result.
// If s has enough capacity, it is extended in place; otherwise a
// new array is allocated and returned.
func
AddByte
(
s
[]
byte
,
t
byte
)
[]
byte
{
lens
:=
len
(
s
);
if
lens
+
1
<=
cap
(
s
)
{
s
=
s
[
0
:
lens
+
1
];
}
else
{
news
:=
make
([]
byte
,
lens
+
1
,
resize
(
lens
+
1
));
Copy
(
news
,
s
);
s
=
news
;
}
s
[
lens
]
=
t
;
return
s
;
}
src/pkg/bytes/bytes_test.go
View file @
d3013d8a
...
...
@@ -121,6 +121,7 @@ var splittests = []SplitTest {
SplitTest
{
"123"
,
""
,
2
,
[]
string
{
"1"
,
"23"
}
},
SplitTest
{
"123"
,
""
,
17
,
[]
string
{
"1"
,
"2"
,
"3"
}
},
}
func
TestSplit
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
splittests
{
a
:=
Split
(
strings
.
Bytes
(
tt
.
s
),
strings
.
Bytes
(
tt
.
sep
),
tt
.
n
);
...
...
@@ -261,3 +262,43 @@ func TestToLower(t *testing.T) {
func
TestTrimSpace
(
t
*
testing
.
T
)
{
runStringTests
(
t
,
TrimSpace
,
"TrimSpace"
,
trimSpaceTests
);
}
type
AddTest
struct
{
s
,
t
string
;
cap
int
;
}
var
addtests
=
[]
AddTest
{
AddTest
{
""
,
""
,
0
},
AddTest
{
"a"
,
""
,
1
},
AddTest
{
"a"
,
"b"
,
1
},
AddTest
{
"abc"
,
"def"
,
100
},
}
func
TestAdd
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
addtests
{
b
:=
make
([]
byte
,
len
(
test
.
s
),
test
.
cap
);
for
i
:=
0
;
i
<
len
(
test
.
s
);
i
++
{
b
[
i
]
=
test
.
s
[
i
]
}
b
=
Add
(
b
,
strings
.
Bytes
(
test
.
t
));
if
string
(
b
)
!=
test
.
s
+
test
.
t
{
t
.
Errorf
(
"Add(%q,%q) = %q"
,
test
.
s
,
test
.
t
,
string
(
b
));
}
}
}
func
TestAddByte
(
t
*
testing
.
T
)
{
const
N
=
2e5
;
b
:=
make
([]
byte
,
0
);
for
i
:=
0
;
i
<
N
;
i
++
{
b
=
AddByte
(
b
,
byte
(
i
))
}
if
len
(
b
)
!=
N
{
t
.
Errorf
(
"AddByte: too small; expected %d got %d"
,
N
,
len
(
b
));
}
for
i
,
c
:=
range
b
{
if
c
!=
byte
(
i
)
{
t
.
Fatalf
(
"AddByte: b[%d] should be %d is %d"
,
i
,
c
,
byte
(
i
));
}
}
}
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