summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Glondu <steph@glondu.net>2019-08-12 06:29:46 +0200
committerStephane Glondu <steph@glondu.net>2019-08-12 06:29:46 +0200
commitd62098fb78d27b6fe62a58958c8b2918ea7eb52b (patch)
tree2ee69a3b7db3c50314e7a0bd547bf5e406d74f6c
parentfd0136a84bb97bbdc214e36c53c77292eb2d3f0a (diff)
New upstream version 1.1.1
-rw-r--r--VERSION2
-rw-r--r--bin/jbuild3
-rw-r--r--src/bi_inbuf.ml10
-rw-r--r--src/bi_inbuf.mli8
-rw-r--r--src/bi_io.ml40
-rw-r--r--src/bi_outbuf.ml15
-rw-r--r--src/bi_outbuf.mli11
-rw-r--r--src/bi_vint.ml4
-rw-r--r--src/jbuild3
-rw-r--r--test/jbuild3
10 files changed, 64 insertions, 35 deletions
diff --git a/VERSION b/VERSION
index 1cc5f65..8cfbc90 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.1.0 \ No newline at end of file
+1.1.1 \ No newline at end of file
diff --git a/bin/jbuild b/bin/jbuild
index f8aa517..98962f1 100644
--- a/bin/jbuild
+++ b/bin/jbuild
@@ -4,4 +4,5 @@
((name bdump)
(public_name bdump)
(package biniou)
- (libraries (biniou)))) \ No newline at end of file
+ (flags (-safe-string))
+ (libraries (biniou))))
diff --git a/src/bi_inbuf.ml b/src/bi_inbuf.ml
index f67c481..3c34d0b 100644
--- a/src/bi_inbuf.ml
+++ b/src/bi_inbuf.ml
@@ -59,16 +59,18 @@ let peek ib =
else
raise End_of_input
-let from_string ?(pos = 0) ?(shrlen = 16) s = {
- i_s = Bytes.of_string s;
+let from_bytes ?(pos = 0) ?(shrlen = 16) s = {
+ i_s = s;
i_pos = pos;
- i_len = String.length s;
+ i_len = Bytes.length s;
i_offs = -pos;
- i_max_len = String.length s;
+ i_max_len = Bytes.length s;
i_refill = (fun ib n -> ());
i_shared = Bi_share.Rd.create shrlen;
}
+let from_string ?pos ?shrlen s = from_bytes ?pos ?shrlen (Bytes.of_string s)
+
(*
Like Pervasives.really_input but returns the number of bytes
read instead of raising End_of_file when the end of file is reached.
diff --git a/src/bi_inbuf.mli b/src/bi_inbuf.mli
index 7a9e302..4a54c25 100644
--- a/src/bi_inbuf.mli
+++ b/src/bi_inbuf.mli
@@ -99,6 +99,14 @@ val from_string : ?pos:int -> ?shrlen:int -> string -> t
@param shrlen initial length of the table used to store shared values.
*)
+val from_bytes : ?pos:int -> ?shrlen:int -> bytes -> t
+ (**
+ Create an input buffer from bytes.
+ @param pos position to start from. Default: 0.
+ @param shrlen initial length of the table used to store shared values.
+ @since 1.2.0
+ *)
+
val from_channel : ?len:int -> ?shrlen:int -> in_channel -> t
(**
Create an input buffer from an in_channel.
diff --git a/src/bi_io.ml b/src/bi_io.ml
index 1325336..f2d1efe 100644
--- a/src/bi_io.ml
+++ b/src/bi_io.ml
@@ -109,13 +109,13 @@ let string_of_hashtag h has_arg =
let read_hashtag ib cont =
let i = Bi_inbuf.read ib 4 in
- let s = Bytes.to_string ib.i_s in
- let x0 = Char.code s.[i] in
+ let s = ib.i_s in
+ let x0 = Char.code (Bytes.get s i) in
let has_arg = x0 >= 0x80 in
let x1 = (x0 land 0x7f) lsl 24 in
- let x2 = (Char.code s.[i+1]) lsl 16 in
- let x3 = (Char.code s.[i+2]) lsl 8 in
- let x4 = Char.code s.[i+3] in
+ let x2 = (Char.code (Bytes.get s (i+1))) lsl 16 in
+ let x3 = (Char.code (Bytes.get s (i+2))) lsl 8 in
+ let x4 = Char.code (Bytes.get s (i+3)) in
let h = make_signed (x1 lor x2 lor x3 lor x4) in
cont ib h has_arg
@@ -123,14 +123,14 @@ let read_hashtag ib cont =
let read_field_hashtag ib =
let i = Bi_inbuf.read ib 4 in
- let s = Bytes.to_string ib.i_s in
- let x0 = Char.code (String.unsafe_get s i) in
+ let s = ib.i_s in
+ let x0 = Char.code (Bytes.unsafe_get s i) in
if x0 < 0x80 then
Bi_util.error "Corrupted data (invalid field hashtag)";
let x1 = (x0 land 0x7f) lsl 24 in
- let x2 = (Char.code (String.unsafe_get s (i+1))) lsl 16 in
- let x3 = (Char.code (String.unsafe_get s (i+2))) lsl 8 in
- let x4 = Char.code (String.unsafe_get s (i+3)) in
+ let x2 = (Char.code (Bytes.unsafe_get s (i+1))) lsl 16 in
+ let x3 = (Char.code (Bytes.unsafe_get s (i+2))) lsl 8 in
+ let x4 = Char.code (Bytes.unsafe_get s (i+3)) in
make_signed (x1 lor x2 lor x3 lor x4)
@@ -147,8 +147,7 @@ let write_numtag ob i has_arg =
let read_numtag ib cont =
let i = Bi_inbuf.read ib 1 in
- let s = Bytes.to_string ib.i_s in
- let x = Char.code s.[i] in
+ let x = Char.code (Bytes.get ib.i_s i) in
let has_arg = x >= 0x80 in
cont ib (x land 0x7f) has_arg
@@ -215,16 +214,16 @@ let float_endianness = lazy (
let read_untagged_float64 ib =
let i = Bi_inbuf.read ib 8 in
- let s = Bytes.to_string ib.i_s in
+ let s = ib.i_s in
let x = Obj.new_block Obj.double_tag 8 in
(match Lazy.force float_endianness with
`Little ->
for j = 0 to 7 do
- Bytes.unsafe_set (Obj.obj x) (7-j) (String.unsafe_get s (i+j))
+ Bytes.unsafe_set (Obj.obj x) (7-j) (Bytes.unsafe_get s (i+j))
done
| `Big ->
for j = 0 to 7 do
- Bytes.unsafe_set (Obj.obj x) j (String.unsafe_get s (i+j))
+ Bytes.unsafe_set (Obj.obj x) j (Bytes.unsafe_get s (i+j))
done
);
(Obj.obj x : float)
@@ -526,17 +525,18 @@ let read_untagged_int8 ib =
let read_untagged_int16 ib =
let i = Bi_inbuf.read ib 2 in
- let s = Bytes.to_string ib.i_s in
- ((Char.code s.[i]) lsl 8) lor (Char.code s.[i+1])
+ let s = ib.i_s in
+ ((Char.code (Bytes.get s i)) lsl 8) lor (Char.code (Bytes.get s (i+1)))
let read_untagged_int32 ib =
let i = Bi_inbuf.read ib 4 in
- let s = Bytes.to_string ib.i_s in
+ let s = ib.i_s in
+ let get_code s i = Char.code (Bytes.get s i) in
let x1 =
- Int32.of_int (((Char.code s.[i ]) lsl 8) lor (Char.code s.[i+1])) in
+ Int32.of_int (((get_code s (i )) lsl 8) lor (get_code s (i+1))) in
let x2 =
- Int32.of_int (((Char.code s.[i+2]) lsl 8) lor (Char.code s.[i+3])) in
+ Int32.of_int (((get_code s (i+2)) lsl 8) lor (get_code s (i+3))) in
Int32.logor (Int32.shift_left x1 16) x2
let read_untagged_float32 ib =
diff --git a/src/bi_outbuf.ml b/src/bi_outbuf.ml
index a10b949..dda23f1 100644
--- a/src/bi_outbuf.ml
+++ b/src/bi_outbuf.ml
@@ -33,8 +33,7 @@ let flush_to_output abstract_output b n =
if n > b.o_max_len then
really_extend b n
-let flush_to_channel oc =
- flush_to_output (fun s start len -> output_string oc (String.sub s start len))
+let flush_to_channel oc = flush_to_output (output_substring oc)
let create ?(make_room = really_extend) ?(shrlen = 16) n = {
@@ -73,14 +72,20 @@ let alloc b n =
b.o_len <- pos + n;
pos
-let add_substring b s pos len =
+let add_sub blit b s pos len =
extend b len;
- String.blit s pos b.o_s b.o_len len;
+ blit s pos b.o_s b.o_len len;
b.o_len <- b.o_len + len
+let add_substring = add_sub String.blit
+let add_subbytes = add_sub Bytes.blit
+
let add_string b s =
add_substring b s 0 (String.length s)
+let add_bytes b s =
+ add_subbytes b s 0 (Bytes.length s)
+
let add_char b c =
let pos = alloc b 1 in
@@ -119,4 +124,4 @@ let reset b =
b.o_len <- 0;
b.o_shared <- Bi_share.Wr.create b.o_shared_init_len
-let contents b = Bytes.to_string (Bytes.sub b.o_s 0 b.o_len)
+let contents b = Bytes.sub_string b.o_s 0 b.o_len
diff --git a/src/bi_outbuf.mli b/src/bi_outbuf.mli
index 6a93a21..2192429 100644
--- a/src/bi_outbuf.mli
+++ b/src/bi_outbuf.mli
@@ -90,6 +90,17 @@ val alloc : t -> int -> int
by accessing [buf.s] directly.
*)
+val add_bytes : t -> bytes -> unit
+ (** Add bytes to the buffer.
+
+ @since 1.2.0 *)
+
+val add_subbytes : t -> bytes -> int -> int -> unit
+ (** [add_subbytes dst src srcpos len] copies [len] bytes from
+ bytes [src] to buffer [dst] starting from position [srcpos].
+
+ @since 1.2.0 *)
+
val add_string : t -> string -> unit
(** Add a string to the buffer. *)
diff --git a/src/bi_vint.ml b/src/bi_vint.ml
index 273532f..9ef012a 100644
--- a/src/bi_vint.ml
+++ b/src/bi_vint.ml
@@ -75,12 +75,12 @@ let svint_of_int ?buf i =
let read_uvint ib =
let avail = Bi_inbuf.try_preread ib max_vint_bytes in
- let s = Bytes.to_string ib.i_s in
+ let s = ib.i_s in
let pos = ib.i_pos in
let x = ref 0 in
(try
for i = 0 to avail - 1 do
- let b = Char.code s.[pos+i] in
+ let b = Char.code (Bytes.get s (pos+i)) in
x := ((b land 0x7f) lsl (7*i)) lor !x;
if b < 0x80 then (
ib.i_pos <- pos + i + 1;
diff --git a/src/jbuild b/src/jbuild
index b990f1c..153a346 100644
--- a/src/jbuild
+++ b/src/jbuild
@@ -5,4 +5,5 @@
(public_name biniou)
(synopsis "Extensible binary serialization format")
(wrapped false)
- (libraries (easy-format bytes))))
+ (flags (-safe-string))
+ (libraries (easy-format))))
diff --git a/test/jbuild b/test/jbuild
index bb7db79..3ca83c9 100644
--- a/test/jbuild
+++ b/test/jbuild
@@ -2,9 +2,10 @@
(executable
((name test_biniou)
+ (flags (-safe-string))
(libraries (biniou unix))))
(alias
((name runtest)
(deps (test_biniou.exe))
- (action (run ${<})))) \ No newline at end of file
+ (action (run ${<}))))