Skip to content

Commit 2807297

Browse files
committed
Prepare primitives required for safe string
1 parent 9342510 commit 2807297

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

boot/ocamlc

1.44 KB
Binary file not shown.

boot/ocamldep

1 KB
Binary file not shown.

boot/ocamllex

2.54 KB
Binary file not shown.

byterun/io.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,29 @@ CAMLprim value caml_ml_output(value vchannel, value buff, value start,
669669
CAMLreturn (Val_unit);
670670
}
671671

672+
/*
673+
* Same as [caml_ml_output] currently
674+
*/
675+
CAMLprim value caml_ml_output_bytes(value vchannel, value buff, value start,
676+
value length)
677+
{
678+
CAMLparam4 (vchannel, buff, start, length);
679+
struct channel * channel = Channel(vchannel);
680+
intnat pos = Long_val(start);
681+
intnat len = Long_val(length);
682+
683+
Lock(channel);
684+
/* We cannot call caml_really_putblock here because buff may move
685+
during caml_write_fd */
686+
while (len > 0) {
687+
int written = caml_putblock(channel, &Byte(buff, pos), len);
688+
pos += written;
689+
len -= written;
690+
}
691+
Unlock(channel);
692+
CAMLreturn (Val_unit);
693+
}
694+
672695
CAMLprim value caml_ml_seek_out(value vchannel, value pos)
673696
{
674697
CAMLparam2 (vchannel, pos);

byterun/str.c

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ CAMLprim value caml_ml_string_length(value s)
4444
return Val_long(temp - Byte (s, temp));
4545
}
4646

47+
CAMLprim value caml_ml_bytes_length(value s)
48+
{
49+
return caml_ml_string_length(s);
50+
}
51+
4752
CAMLexport int caml_string_is_c_safe (value s)
4853
{
4954
return strlen(String_val(s)) == caml_string_length(s);
5055
}
5156

52-
/* [len] is a value that represents a number of bytes (chars) */
57+
/**
58+
* [caml_create_string] is deprecated,
59+
* use [caml_create_bytes] instead
60+
*/
5361
CAMLprim value caml_create_string(value len)
5462
{
5563
mlsize_t size = Long_val(len);
@@ -59,13 +67,29 @@ CAMLprim value caml_create_string(value len)
5967
return caml_alloc_string(size);
6068
}
6169

70+
/* [len] is a value that represents a number of bytes (chars) */
71+
CAMLprim value caml_create_bytes(value len)
72+
{
73+
mlsize_t size = Long_val(len);
74+
if (size > Bsize_wsize (Max_wosize) - 1){
75+
caml_invalid_argument("Bytes.create");
76+
}
77+
return caml_alloc_string(size);
78+
}
79+
80+
81+
6282
CAMLprim value caml_string_get(value str, value index)
6383
{
6484
intnat idx = Long_val(index);
6585
if (idx < 0 || idx >= caml_string_length(str)) caml_array_bound_error();
6686
return Val_int(Byte_u(str, idx));
6787
}
6888

89+
CAMLprim value caml_bytes_get(value str, value index)
90+
{
91+
return caml_string_get(str, index);
92+
}
6993
CAMLprim value caml_string_set(value str, value index, value newval)
7094
{
7195
intnat idx = Long_val(index);
@@ -74,6 +98,12 @@ CAMLprim value caml_string_set(value str, value index, value newval)
7498
return Val_unit;
7599
}
76100

101+
CAMLprim value caml_bytes_set(value str, value index, value newval)
102+
{
103+
return caml_string_set(str,index,newval);
104+
}
105+
106+
77107
CAMLprim value caml_string_get16(value str, value index)
78108
{
79109
intnat res;
@@ -231,11 +261,21 @@ CAMLprim value caml_string_equal(value s1, value s2)
231261
return Val_true;
232262
}
233263

264+
CAMLprim value caml_bytes_equal(value s1, value s2)
265+
{
266+
return caml_string_equal(s1,s2);
267+
}
268+
234269
CAMLprim value caml_string_notequal(value s1, value s2)
235270
{
236271
return Val_not(caml_string_equal(s1, s2));
237272
}
238273

274+
CAMLprim value caml_bytes_notequal(value s1, value s2)
275+
{
276+
return caml_string_notequal(s1,s2);
277+
}
278+
239279
CAMLprim value caml_string_compare(value s1, value s2)
240280
{
241281
mlsize_t len1, len2;
@@ -252,39 +292,81 @@ CAMLprim value caml_string_compare(value s1, value s2)
252292
return Val_int(0);
253293
}
254294

295+
CAMLprim value caml_bytes_compare(value s1, value s2)
296+
{
297+
return caml_string_compare(s1,s2);
298+
}
299+
255300
CAMLprim value caml_string_lessthan(value s1, value s2)
256301
{
257302
return caml_string_compare(s1, s2) < Val_int(0) ? Val_true : Val_false;
258303
}
259304

305+
CAMLprim value caml_bytes_lessthan(value s1, value s2)
306+
{
307+
return caml_string_lessthan(s1,s2);
308+
}
309+
310+
260311
CAMLprim value caml_string_lessequal(value s1, value s2)
261312
{
262313
return caml_string_compare(s1, s2) <= Val_int(0) ? Val_true : Val_false;
263314
}
264315

316+
CAMLprim value caml_bytes_lessequal(value s1, value s2)
317+
{
318+
return caml_string_lessequal(s1,s2);
319+
}
320+
321+
265322
CAMLprim value caml_string_greaterthan(value s1, value s2)
266323
{
267324
return caml_string_compare(s1, s2) > Val_int(0) ? Val_true : Val_false;
268325
}
269326

327+
CAMLprim value caml_bytes_greaterthan(value s1, value s2)
328+
{
329+
return caml_string_greaterthan(s1,s2);
330+
}
331+
270332
CAMLprim value caml_string_greaterequal(value s1, value s2)
271333
{
272334
return caml_string_compare(s1, s2) >= Val_int(0) ? Val_true : Val_false;
273335
}
274336

337+
CAMLprim value caml_bytes_greaterequal(value s1, value s2)
338+
{
339+
return caml_string_greaterequal(s1,s2);
340+
}
341+
275342
CAMLprim value caml_blit_string(value s1, value ofs1, value s2, value ofs2,
276343
value n)
277344
{
278345
memmove(&Byte(s2, Long_val(ofs2)), &Byte(s1, Long_val(ofs1)), Long_val(n));
279346
return Val_unit;
280347
}
281348

349+
CAMLprim value caml_blit_bytes(value s1, value ofs1, value s2, value ofs2,
350+
value n)
351+
{
352+
memmove(&Byte(s2, Long_val(ofs2)), &Byte(s1, Long_val(ofs1)), Long_val(n));
353+
return Val_unit;
354+
}
355+
/**
356+
* [caml_fill_string] is deprecated, use [caml_fill_bytes] instead
357+
*/
282358
CAMLprim value caml_fill_string(value s, value offset, value len, value init)
283359
{
284360
memset(&Byte(s, Long_val(offset)), Int_val(init), Long_val(len));
285361
return Val_unit;
286362
}
287363

364+
CAMLprim value caml_fill_bytes(value s, value offset, value len, value init)
365+
{
366+
memset(&Byte(s, Long_val(offset)), Int_val(init), Long_val(len));
367+
return Val_unit;
368+
}
369+
288370
CAMLprim value caml_bitvect_test(value bv, value n)
289371
{
290372
intnat pos = Long_val(n);

0 commit comments

Comments
 (0)