Skip to content

Commit 2b31d2e

Browse files
authored
Merge pull request #4601 from BuckleScript/bytes_to_string
micro tweak Bytes.to_string
2 parents 8b9cbfc + c315804 commit 2b31d2e

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

jscomp/runtime/caml_bytes.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ let string_of_large_bytes (bytes : bytes) i len =
110110
while s_len.contents > 0 do
111111
let next = if s_len.contents < 1024 then s_len.contents else seg in
112112
let tmp_bytes = new_uninitialized next in
113-
let () = caml_blit_bytes bytes offset.contents tmp_bytes 0 next in
114-
s .contents <- s.contents ^ Caml_string_extern.of_small_int_array (to_int_array tmp_bytes);
113+
for k = 0 to next - 1 do
114+
tmp_bytes.![k] <- bytes.![k + offset.contents]
115+
done;
116+
s.contents <- s.contents ^ Caml_string_extern.of_small_int_array (to_int_array tmp_bytes);
115117
s_len.contents <- s_len.contents - next ;
116118
offset.contents <- offset.contents + next;
117119
done;

jscomp/test/ext_bytes_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ Bytes.blit_string(a$2, 10, b, 5, 10);
178178

179179
eq("File \"ext_bytes_test.ml\", line 109, characters 7-14", b, Bytes.of_string("\0\0\0\0\0\n\x0b\f\r\x0e\x0f\x10\x11\x12\x13\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"));
180180

181+
var s = Bytes.init(50000, (function (i) {
182+
return Char.chr(i % 137);
183+
}));
184+
185+
var s1 = Bytes.to_string(s);
186+
187+
var s2 = Bytes.of_string(s1);
188+
189+
eq("File \"ext_bytes_test.ml\", line 115, characters 7-14", s, s2);
190+
181191
Mt.from_pair_suites("Ext_bytes_test", suites.contents);
182192

183193
exports.suites = suites;

jscomp/test/ext_bytes_test.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@ let () =
108108
Bytes.blit_string a 10 b 5 10;
109109
eq __LOC__ b (Bytes.of_string "\000\000\000\000\000\n\011\012\r\014\015\016\017\018\019\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")
110110

111+
let () =
112+
let s = Bytes.init 50_000 (fun i -> Char.chr (i mod 137)) in
113+
let s1 = Bytes.to_string s in
114+
let s2 = Bytes.of_string s1 in
115+
eq __LOC__ s s2
111116
let () =
112117
Mt.from_pair_suites __MODULE__ !suites

lib/es6/caml_bytes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function bytes_to_string(a) {
102102
while(s_len > 0) {
103103
var next = s_len < 1024 ? s_len : 1024;
104104
var tmp_bytes = new Array(next);
105-
caml_blit_bytes(a, offset, tmp_bytes, 0, next);
105+
for(var k = 0; k < next; ++k){
106+
tmp_bytes[k] = a[k + offset | 0];
107+
}
106108
s = s + String.fromCharCode.apply(null, tmp_bytes);
107109
s_len = s_len - next | 0;
108110
offset = offset + next | 0;

lib/js/caml_bytes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function bytes_to_string(a) {
102102
while(s_len > 0) {
103103
var next = s_len < 1024 ? s_len : 1024;
104104
var tmp_bytes = new Array(next);
105-
caml_blit_bytes(a, offset, tmp_bytes, 0, next);
105+
for(var k = 0; k < next; ++k){
106+
tmp_bytes[k] = a[k + offset | 0];
107+
}
106108
s = s + String.fromCharCode.apply(null, tmp_bytes);
107109
s_len = s_len - next | 0;
108110
offset = offset + next | 0;

0 commit comments

Comments
 (0)