Skip to content

Commit 2b521fb

Browse files
committed
Buffer.add_bytes, Buffer.add_subbytes don't convert bytes to string
1 parent 9aac4c8 commit 2b521fb

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

stdlib/buffer.ml

+12-10
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ let rec add_utf_16le_uchar b u =
148148

149149
let add_substring b s offset len =
150150
if offset < 0 || len < 0 || offset > String.length s - len
151-
then invalid_arg "Buffer.add_substring/add_subbytes";
151+
then invalid_arg "Buffer.add_substring";
152152
let position = b.position in
153153
let {buffer;length} = b.inner in
154154
let new_position = position + len in
@@ -159,22 +159,24 @@ let add_substring b s offset len =
159159
Bytes.unsafe_blit_string s offset buffer position len;
160160
b.position <- new_position
161161

162-
let add_subbytes b s offset len =
163-
add_substring b (Bytes.unsafe_to_string s) offset len
164-
165-
let add_string b s =
166-
let len = String.length s in
162+
let add_subbytes b bytes offset len =
163+
if offset < 0 || len < 0 || offset > Bytes.length bytes - len
164+
then invalid_arg "Buffer.add_subbytes";
167165
let position = b.position in
168-
let {buffer; length} = b.inner in
166+
let {buffer;length} = b.inner in
169167
let new_position = position + len in
170168
if new_position > length then (
171169
resize b len;
172-
Bytes.blit_string s 0 b.inner.buffer b.position len;
170+
Bytes.blit bytes offset b.inner.buffer b.position len
173171
) else
174-
Bytes.unsafe_blit_string s 0 buffer position len;
172+
Bytes.unsafe_blit bytes offset buffer position len;
175173
b.position <- new_position
176174

177-
let add_bytes b s = add_string b (Bytes.unsafe_to_string s)
175+
let add_string b s =
176+
add_substring b s 0 (String.length s)
177+
178+
let add_bytes b bytes =
179+
add_subbytes b bytes 0 (Bytes.length bytes)
178180

179181
let add_buffer b bs =
180182
add_subbytes b bs.inner.buffer 0 bs.position

0 commit comments

Comments
 (0)