diff options
author | Stephane Glondu <steph@glondu.net> | 2019-08-12 06:30:15 +0200 |
---|---|---|
committer | Stephane Glondu <steph@glondu.net> | 2019-08-12 06:30:15 +0200 |
commit | 94609a4bb05727f1524647c9cb02a8eb3a9871e9 (patch) | |
tree | 636417a7a626b09b6c352ae1fe6521ba29aa0902 | |
parent | 554aa83c3ab0e3fd0699dd79cd5d9c05dca100f5 (diff) | |
parent | 8f1f347e8e368efd08d9cf8b59266611f983f4be (diff) |
Update upstream source from tag 'upstream/1.2.0'
Update to upstream version '1.2.0'
with Debian dir c9b988f5c9846652b4fde26560b9c0f55c065c04
-rw-r--r-- | .gitignore | 17 | ||||
-rw-r--r-- | META.in | 7 | ||||
-rw-r--r-- | Makefile | 177 | ||||
-rw-r--r-- | VERSION | 1 | ||||
-rw-r--r-- | bin/bdump.ml (renamed from bdump.ml) | 0 | ||||
-rw-r--r-- | bin/jbuild | 8 | ||||
-rw-r--r-- | biniou.opam | 24 | ||||
-rw-r--r-- | src/bi_dump.ml (renamed from bi_dump.ml) | 0 | ||||
-rw-r--r-- | src/bi_inbuf.ml (renamed from bi_inbuf.ml) | 22 | ||||
-rw-r--r-- | src/bi_inbuf.mli (renamed from bi_inbuf.mli) | 10 | ||||
-rw-r--r-- | src/bi_io.ml (renamed from bi_io.ml) | 55 | ||||
-rw-r--r-- | src/bi_io.mli (renamed from bi_io.mli) | 0 | ||||
-rw-r--r-- | src/bi_outbuf.ml (renamed from bi_outbuf.ml) | 44 | ||||
-rw-r--r-- | src/bi_outbuf.mli (renamed from bi_outbuf.mli) | 13 | ||||
-rw-r--r-- | src/bi_share.ml (renamed from bi_share.ml) | 0 | ||||
-rw-r--r-- | src/bi_share.mli (renamed from bi_share.mli) | 0 | ||||
-rw-r--r-- | src/bi_stream.ml (renamed from bi_stream.ml) | 4 | ||||
-rw-r--r-- | src/bi_stream.mli (renamed from bi_stream.mli) | 0 | ||||
-rw-r--r-- | src/bi_util.ml (renamed from bi_util.ml) | 30 | ||||
-rw-r--r-- | src/bi_util.mli (renamed from bi_util.mli) | 2 | ||||
-rw-r--r-- | src/bi_vint.ml (renamed from bi_vint.ml) | 19 | ||||
-rw-r--r-- | src/bi_vint.mli (renamed from bi_vint.mli) | 0 | ||||
-rw-r--r-- | src/jbuild | 9 | ||||
-rw-r--r-- | test/jbuild | 11 | ||||
-rw-r--r-- | test/test_biniou.ml (renamed from test_biniou.ml) | 0 |
25 files changed, 181 insertions, 272 deletions
@@ -1,14 +1,3 @@ -*~ -*.cm[ioxat] -*.[oa] -*.cmx[as] -*.cmti -*.annot -META -bdump -test-stream.dat -test2.bin -*.byte -*.native -*.exe -test_biniou +_build +.merlin +*.install diff --git a/META.in b/META.in deleted file mode 100644 index 4a5788d..0000000 --- a/META.in +++ /dev/null @@ -1,7 +0,0 @@ -name = "biniou" -version = "@@VERSION@@" -description = "Extensible binary serialization format" -requires = "easy-format" -archive(byte) = "biniou.cma" -archive(native) = "biniou.cmxa" -archive(native,plugin) = "biniou.cmxs" @@ -1,175 +1,12 @@ -VERSION = 1.0.12 +all: + @jbuilder build -FLAGS = -g -annot -bin-annot -PACKS = easy-format +test: + @jbuilder runtest -ifeq "$(shell ocamlfind ocamlc -config |grep os_type)" "os_type: Win32" -EXE=.exe -else -EXE= -endif +check: test -BEST = $(shell \ - if ocamlfind ocamlopt 2>/dev/null; then \ - echo .native; \ - else \ - echo .byte; \ - fi \ -) - -NATDYNLINK = $(shell \ - if [ -f `ocamlfind ocamlc -where`/dynlink.cmxa ]; then \ - echo YES; \ - else \ - echo NO; \ - fi \ -) - -ifeq "${NATDYNLINK}" "YES" -CMXS=biniou.cmxs -endif - -.PHONY: default all byte opt install doc test -default: all test_biniou$(EXE) - -ifeq "$(BEST)" ".native" -all: byte opt doc META -else -all: byte doc META -endif - -byte: biniou.cma bdump.byte -opt: biniou.cmxa $(CMXS) bdump.native - -test: test_biniou$(EXE) - ./$< - -ifndef PREFIX - PREFIX = $(shell dirname $$(dirname $$(which ocamlfind))) - export PREFIX -endif - -ifndef BINDIR - BINDIR = $(PREFIX)/bin - export BINDIR -endif - -META: META.in Makefile - sed -e 's:@@VERSION@@:$(VERSION):' META.in > META - -SOURCES = bi_util.mli bi_util.ml \ - bi_share.mli bi_share.ml \ - bi_outbuf.mli bi_outbuf.ml bi_inbuf.mli bi_inbuf.ml \ - bi_vint.mli bi_vint.ml bi_io.mli bi_io.ml \ - bi_dump.ml bi_stream.mli bi_stream.ml - -MLI = $(filter %.mli, $(SOURCES)) -ML = $(filter %.ml, $(SOURCES)) -CMI = $(MLI:.mli=.cmi) -CMT = $(MLI:.mli=.cmt) -ANNOT = $(MLI:.mli=.annot) -CMO = $(ML:.ml=.cmo) -CMX = $(ML:.ml=.cmx) -O = $(ML:.ml=.o) - -biniou.cma: $(SOURCES) Makefile - ocamlfind ocamlc -a $(FLAGS) -o biniou.cma \ - -package "$(PACKS)" $(SOURCES) - -biniou.cmxa: $(SOURCES) Makefile - ocamlfind ocamlopt -a $(FLAGS) \ - -o biniou.cmxa -package "$(PACKS)" $(SOURCES) - -biniou.cmxs: biniou.cmxa - ocamlfind ocamlopt -shared -linkall -I . -o $@ $^ - -bdump.byte: biniou.cma bdump.ml - ocamlfind ocamlc -o $@ $(FLAGS) \ - -package $(PACKS) -linkpkg $^ - -bdump.native: biniou.cmxa bdump.ml - ocamlfind ocamlopt -o $@ $(FLAGS) \ - -package $(PACKS) -linkpkg $^ - -test_biniou.byte: biniou.cma test_biniou.ml - ocamlfind ocamlc -o $@ $(FLAGS) \ - -package "$(PACKS) unix" -linkpkg $^ - -test_biniou.native: biniou.cmxa test_biniou.ml - ocamlfind ocamlopt -o $@ $(FLAGS) \ - -package "$(PACKS) unix" -linkpkg $^ - -%$(EXE): %$(BEST) - cp $< $@ - -doc: doc/index.html -doc/index.html: $(MLI) - mkdir -p doc - ocamlfind ocamldoc -d doc -html -package easy-format $(MLI) - -install: META byte - if [ -f bdump.native ]; then \ - cp bdump.native $(BINDIR)/bdump$(EXE); \ - else \ - cp bdump.byte $(BINDIR)/bdump$(EXE); \ - fi - ocamlfind install biniou META \ - $(MLI) $(CMI) $(CMT) $(ANNOT) $(CMO) biniou.cma \ - -optional $(CMX) $(O) biniou.cmxa biniou.a biniou.cmxs - -uninstall: - rm -f $(BINDIR)/bdump{.exe,} - ocamlfind remove biniou - -.PHONY: clean +.PHONY: clean all check test clean: - rm -f *.o *.a *.cm[ioxa] *.cmxa *~ *.annot META - rm -f {bdump,test_biniou}{.exe,.byte,.native,} - rm -rf doc - rm -f test.bin test_channels.bin - -SUBDIRS = -SVNURL = svn://svn.forge.ocamlcore.org/svnroot/biniou/trunk/biniou - -.PHONY: archive -archive: - @echo "Making archive for version $(VERSION)" - @if [ -z "$$WWW" ]; then \ - echo '*** Environment variable WWW is undefined ***' >&2; \ - exit 1; \ - fi - @if [ -n "$$(svn status -q)" ]; then \ - echo "*** There are uncommitted changes, aborting. ***" >&2; \ - exit 1; \ - fi - $(MAKE) && ./bdump -help > $$WWW/bdump-help.txt - mkdir -p $$WWW/biniou-doc - $(MAKE) doc && cp doc/* $$WWW/biniou-doc/ - rm -rf /tmp/biniou /tmp/biniou-$(VERSION) && \ - cd /tmp && \ - svn co "$(SVNURL)" && \ - for x in "." $(SUBDIRS); do \ - rm -rf /tmp/biniou/$$x/.svn; \ - done && \ - cd /tmp && cp -r biniou biniou-$(VERSION) && \ - tar czf biniou.tar.gz biniou && \ - tar cjf biniou.tar.bz2 biniou && \ - tar czf biniou-$(VERSION).tar.gz biniou-$(VERSION) && \ - tar cjf biniou-$(VERSION).tar.bz2 biniou-$(VERSION) - mv /tmp/biniou.tar.gz /tmp/biniou.tar.bz2 ../releases - mv /tmp/biniou-$(VERSION).tar.gz \ - /tmp/biniou-$(VERSION).tar.bz2 ../releases - cp ../releases/biniou.tar.gz $$WWW/ - cp ../releases/biniou.tar.bz2 $$WWW/ - cp ../releases/biniou-$(VERSION).tar.gz $$WWW/ - cp ../releases/biniou-$(VERSION).tar.bz2 $$WWW/ - cd ../releases && \ - svn add biniou.tar.gz biniou.tar.bz2 \ - biniou-$(VERSION).tar.gz biniou-$(VERSION).tar.bz2 && \ - svn commit -m "biniou version $(VERSION)" - cp LICENSE $$WWW/biniou-license.txt - cp Changes $$WWW/biniou-changes.txt - cp biniou-format.txt $$WWW/biniou-format.txt - echo 'let biniou_version = "$(VERSION)"' \ - > $$WWW/biniou-version.ml + jbuilder clean @@ -0,0 +1 @@ +1.2.0
\ No newline at end of file diff --git a/bin/jbuild b/bin/jbuild new file mode 100644 index 0000000..98962f1 --- /dev/null +++ b/bin/jbuild @@ -0,0 +1,8 @@ +(jbuild_version 1) + +(executable + ((name bdump) + (public_name bdump) + (package biniou) + (flags (-safe-string)) + (libraries (biniou)))) diff --git a/biniou.opam b/biniou.opam new file mode 100644 index 0000000..037005d --- /dev/null +++ b/biniou.opam @@ -0,0 +1,24 @@ +opam-version: "1.2" +maintainer: "martin@mjambon.com" +authors: ["Martin Jambon"] + +homepage: "https://github.com/mjambon/biniou" +bug-reports: "https://github.com/mjambon/biniou/issues" +dev-repo: "https://github.com/mjambon/biniou.git" +license: "BSD-3-Clause" + +build: [ + ["jbuilder" "build" "-p" name "-j" jobs] +] + +build-test: [ + ["jbuilder" "runtest" "-p" name] +] + +depends: [ + "conf-which" {build} + "jbuilder" {build} + "easy-format" +] + +available: [ocaml-version >= "4.02.3"]
\ No newline at end of file diff --git a/bi_dump.ml b/src/bi_dump.ml index c967260..c967260 100644 --- a/bi_dump.ml +++ b/src/bi_dump.ml diff --git a/bi_inbuf.ml b/src/bi_inbuf.ml index d30ecb0..3c34d0b 100644 --- a/bi_inbuf.ml +++ b/src/bi_inbuf.ml @@ -1,5 +1,5 @@ type t = { - mutable i_s : string; + mutable i_s : bytes; mutable i_pos : int; mutable i_len : int; mutable i_offs : int; @@ -35,14 +35,14 @@ let read ib n = let read_char ib = let pos = ib.i_pos in if ib.i_len - pos > 0 then ( - let c = String.unsafe_get ib.i_s pos in + let c = Bytes.unsafe_get ib.i_s pos in ib.i_pos <- pos + 1; c ) else if try_preread ib 1 > 0 then let pos = ib.i_pos in - let c = String.unsafe_get ib.i_s pos in + let c = Bytes.unsafe_get ib.i_s pos in ib.i_pos <- pos + 1; c else @@ -51,24 +51,26 @@ let read_char ib = let peek ib = let pos = ib.i_pos in if ib.i_len - pos > 0 then ( - String.unsafe_get ib.i_s pos + Bytes.unsafe_get ib.i_s pos ) else if try_preread ib 1 > 0 then - String.unsafe_get ib.i_s ib.i_pos + Bytes.unsafe_get ib.i_s ib.i_pos else raise End_of_input -let from_string ?(pos = 0) ?(shrlen = 16) 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. @@ -87,7 +89,7 @@ let refill_from_channel ic ib n = let rem_len = ib.i_len - ib.i_pos in if rem_len < n then let s = ib.i_s in - String.blit s ib.i_pos s 0 rem_len; + Bytes.blit s ib.i_pos s 0 rem_len; let to_read = n - rem_len in let really_read = not_really_input ic s rem_len to_read 0 in ib.i_offs <- ib.i_offs + ib.i_pos; @@ -96,7 +98,7 @@ let refill_from_channel ic ib n = ) let from_channel ?(len = 4096) ?(shrlen = 16) ic = { - i_s = String.create len; + i_s = Bytes.create len; i_pos = 0; i_len = 0; i_offs = 0; diff --git a/bi_inbuf.mli b/src/bi_inbuf.mli index 2998412..4a54c25 100644 --- a/bi_inbuf.mli +++ b/src/bi_inbuf.mli @@ -1,7 +1,7 @@ (** Input buffer *) type t = { - mutable i_s : string; + mutable i_s : bytes; (** This is the buffer string. It can be accessed for reading but should normally only be written to or replaced only by the [i_refill] function. @@ -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. @@ -79,17 +79,23 @@ let hash_name s = record fields. *) + +let mask_31bit = + let n = Bi_util.int_size - 31 in + assert (n >= 0); + fun x -> (x lsl n) lsr n + let write_hashtag ob h has_arg = - let h = h land 0x7fffffff in + let h = mask_31bit h in let pos = Bi_outbuf.alloc ob 4 in let s = ob.o_s in - String.unsafe_set s (pos+3) (Char.chr (h land 0xff)); + Bytes.unsafe_set s (pos+3) (Char.chr (h land 0xff)); let h = h lsr 8 in - String.unsafe_set s (pos+2) (Char.chr (h land 0xff)); + Bytes.unsafe_set s (pos+2) (Char.chr (h land 0xff)); let h = h lsr 8 in - String.unsafe_set s (pos+1) (Char.chr (h land 0xff)); + Bytes.unsafe_set s (pos+1) (Char.chr (h land 0xff)); let h = h lsr 8 in - String.unsafe_set s pos ( + Bytes.unsafe_set s pos ( Char.chr ( if has_arg then h lor 0x80 else h @@ -104,12 +110,12 @@ let string_of_hashtag h has_arg = let read_hashtag ib cont = let i = Bi_inbuf.read ib 4 in let s = ib.i_s in - let x0 = Char.code s.[i] 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 @@ -118,13 +124,13 @@ let read_hashtag ib cont = let read_field_hashtag ib = let i = Bi_inbuf.read ib 4 in let s = ib.i_s in - let x0 = Char.code (String.unsafe_get s i) 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) @@ -141,7 +147,7 @@ let write_numtag ob i has_arg = let read_numtag ib cont = let i = Bi_inbuf.read ib 1 in - let x = Char.code ib.i_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 @@ -213,11 +219,11 @@ let read_untagged_float64 ib = (match Lazy.force float_endianness with `Little -> for j = 0 to 7 do - String.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 - String.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) @@ -228,11 +234,11 @@ let write_untagged_float64 ob x = (match Lazy.force float_endianness with `Little -> for j = 0 to 7 do - String.unsafe_set s (i+j) (String.unsafe_get (Obj.magic x) (7-j)) + Bytes.unsafe_set s (i+j) (String.unsafe_get (Obj.magic x) (7-j)) done | `Big -> for j = 0 to 7 do - String.unsafe_set s (i+j) (String.unsafe_get (Obj.magic x) j) + Bytes.unsafe_set s (i+j) (String.unsafe_get (Obj.magic x) j) done ) @@ -520,16 +526,17 @@ let read_untagged_int8 ib = let read_untagged_int16 ib = let i = Bi_inbuf.read ib 2 in let s = ib.i_s in - ((Char.code s.[i]) lsl 8) lor (Char.code s.[i+1]) + ((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 = 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 = @@ -559,7 +566,7 @@ let read_untagged_int64 ib = let read_untagged_string ib = let len = Bi_vint.read_uvint ib in - let str = String.create len in + let str = Bytes.create len in let pos = ref 0 in let rem = ref len in while !rem > 0 do @@ -567,13 +574,13 @@ let read_untagged_string ib = if bytes_read = 0 then Bi_util.error "Corrupted data (string)" else ( - String.blit ib.i_s ib.i_pos str !pos bytes_read; + Bytes.blit ib.i_s ib.i_pos str !pos bytes_read; ib.i_pos <- ib.i_pos + bytes_read; pos := !pos + bytes_read; rem := !rem - bytes_read ) done; - str + Bytes.to_string str let read_untagged_uvint = Bi_vint.read_uvint let read_untagged_svint = Bi_vint.read_svint diff --git a/bi_io.mli b/src/bi_io.mli index 979c9dc..979c9dc 100644 --- a/bi_io.mli +++ b/src/bi_io.mli diff --git a/bi_outbuf.ml b/src/bi_outbuf.ml index d49f80c..dda23f1 100644 --- a/bi_outbuf.ml +++ b/src/bi_outbuf.ml @@ -1,5 +1,5 @@ type t = { - mutable o_s : string; + mutable o_s : bytes; mutable o_max_len : int; mutable o_len : int; mutable o_offs : int; @@ -21,23 +21,23 @@ let really_extend b n = else Sys.max_string_length in - let s = String.create slen in - String.blit b.o_s 0 s 0 b.o_len; + let s = Bytes.create slen in + Bytes.blit b.o_s 0 s 0 b.o_len; b.o_s <- s; b.o_max_len <- slen let flush_to_output abstract_output b n = - abstract_output b.o_s 0 b.o_len; + abstract_output (Bytes.to_string b.o_s) 0 b.o_len; b.o_offs <- b.o_offs + b.o_len; b.o_len <- 0; if n > b.o_max_len then really_extend b n -let flush_to_channel oc = flush_to_output (output oc) +let flush_to_channel oc = flush_to_output (output_substring oc) let create ?(make_room = really_extend) ?(shrlen = 16) n = { - o_s = String.create n; + o_s = Bytes.create n; o_max_len = n; o_len = 0; o_offs = 0; @@ -72,37 +72,43 @@ 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 - b.o_s.[pos] <- c + Bytes.set b.o_s pos c let unsafe_add_char b c = let len = b.o_len in - b.o_s.[len] <- c; + Bytes.set b.o_s len c; b.o_len <- len + 1 let add_char2 b c1 c2 = let pos = alloc b 2 in let s = b.o_s in - String.unsafe_set s pos c1; - String.unsafe_set s (pos+1) c2 + Bytes.unsafe_set s pos c1; + Bytes.unsafe_set s (pos+1) c2 let add_char4 b c1 c2 c3 c4 = let pos = alloc b 4 in let s = b.o_s in - String.unsafe_set s pos c1; - String.unsafe_set s (pos+1) c2; - String.unsafe_set s (pos+2) c3; - String.unsafe_set s (pos+3) c4 + Bytes.unsafe_set s pos c1; + Bytes.unsafe_set s (pos+1) c2; + Bytes.unsafe_set s (pos+2) c3; + Bytes.unsafe_set s (pos+3) c4 @@ -112,10 +118,10 @@ let clear b = Bi_share.Wr.clear b.o_shared let reset b = - if String.length b.o_s <> b.o_init_len then - b.o_s <- String.create b.o_init_len; + if Bytes.length b.o_s <> b.o_init_len then + b.o_s <- Bytes.create b.o_init_len; b.o_offs <- 0; b.o_len <- 0; b.o_shared <- Bi_share.Wr.create b.o_shared_init_len -let contents b = String.sub b.o_s 0 b.o_len +let contents b = Bytes.sub_string b.o_s 0 b.o_len diff --git a/bi_outbuf.mli b/src/bi_outbuf.mli index fb1482b..2192429 100644 --- a/bi_outbuf.mli +++ b/src/bi_outbuf.mli @@ -1,7 +1,7 @@ (** Output buffer *) type t = { - mutable o_s : string; + mutable o_s : bytes; (** Buffer string *) mutable o_max_len : int; @@ -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/bi_share.ml b/src/bi_share.ml index b864ebb..b864ebb 100644 --- a/bi_share.ml +++ b/src/bi_share.ml diff --git a/bi_share.mli b/src/bi_share.mli index a27d3af..a27d3af 100644 --- a/bi_share.mli +++ b/src/bi_share.mli diff --git a/bi_stream.ml b/src/bi_stream.ml index 88f8d7b..775c57a 100644 --- a/bi_stream.ml +++ b/src/bi_stream.ml @@ -53,9 +53,9 @@ let rec read_chunk of_string ic = error (sprintf "Corrupted stream: excessive chunk length (%i bytes)" len); - let s = String.create len in + let s = Bytes.create len in really_input ic s 0 len; - Some (of_string s) + Some (of_string (Bytes.to_string s)) | '\000' -> None diff --git a/bi_stream.mli b/src/bi_stream.mli index 6233749..6233749 100644 --- a/bi_stream.mli +++ b/src/bi_stream.mli diff --git a/bi_util.ml b/src/bi_util.ml index 222177c..9fb0583 100644 --- a/bi_util.ml +++ b/src/bi_util.ml @@ -8,18 +8,18 @@ let error s = raise (Error s) *) let string8_of_int x = - let s = String.create 8 in + let s = Bytes.create 8 in for i = 0 to 7 do - s.[7-i] <- Char.chr (0xff land (x lsr (8 * i))) + Bytes.set s (7-i) (Char.chr (0xff land (x lsr (8 * i)))) done; - s + Bytes.to_string s let string4_of_int x = - let s = String.create 4 in + let s = Bytes.create 4 in for i = 0 to 3 do - s.[3-i] <- Char.chr (0xff land (x lsr (8 * i))) + Bytes.set s (3-i) (Char.chr (0xff land (x lsr (8 * i)))) done; - s + Bytes.to_string s let print_bits ?(pos = 0) ?len s = let slen = String.length s in @@ -33,13 +33,23 @@ let print_bits ?(pos = 0) ?len s = else len in - let r = String.create (len * 9) in + let r = Bytes.create (len * 9) in for i = 0 to len - 1 do let k = i * 9 in let x = Char.code s.[pos+i] in for j = 0 to 7 do - r.[k+j] <- if (x lsr (7 - j)) land 1 = 0 then '0' else '1' + Bytes.set r (k+j) (if (x lsr (7 - j)) land 1 = 0 then '0' else '1') done; - r.[k+8] <- if (i + 1) mod 8 = 0 then '\n' else ' ' + Bytes.set r (k+8) (if (i + 1) mod 8 = 0 then '\n' else ' ') done; - r + Bytes.to_string r + +(* int size in bits *) +let int_size = + let c = ref 0 in + let r = ref (-1) in + while !r <> 0 do + r := !r lsr 1; + incr c + done; + !c diff --git a/bi_util.mli b/src/bi_util.mli index 7d6601b..34d99d3 100644 --- a/bi_util.mli +++ b/src/bi_util.mli @@ -13,3 +13,5 @@ val error : string -> 'a val string8_of_int : int -> string val string4_of_int : int -> string val print_bits : ?pos:int -> ?len:int -> string -> string + +val int_size : int diff --git a/bi_vint.ml b/src/bi_vint.ml index ac37297..9ef012a 100644 --- a/bi_vint.ml +++ b/src/bi_vint.ml @@ -6,25 +6,16 @@ open Bi_inbuf type uint = int -(* Word size in bytes *) -let word_size = - if 0x7fffffff = -1 then 4 - else 8 - -(* Maximum int size in bits *) -let max_int_bits = - 8 * word_size - 1 - (* Maximum length of a vint decodable into an OCaml int, maximum value of the highest byte of the largest vint supported *) let max_vint_bytes, max_highest_byte = - if max_int_bits mod 7 = 0 then - let m = max_int_bits / 7 in + if Bi_util.int_size mod 7 = 0 then + let m = Bi_util.int_size / 7 in let h = 1 lsl 7 - 1 in m, h else - let m = max_int_bits / 7 + 1 in - let h = 1 lsl (max_int_bits mod 7) - 1 in + let m = Bi_util.int_size / 7 + 1 in + let h = 1 lsl (Bi_util.int_size mod 7) - 1 in m, h let check_highest_byte x = @@ -89,7 +80,7 @@ let read_uvint ib = 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/bi_vint.mli b/src/bi_vint.mli index 5a4e247..5a4e247 100644 --- a/bi_vint.mli +++ b/src/bi_vint.mli diff --git a/src/jbuild b/src/jbuild new file mode 100644 index 0000000..153a346 --- /dev/null +++ b/src/jbuild @@ -0,0 +1,9 @@ +(jbuild_version 1) + +(library + ((name biniou) + (public_name biniou) + (synopsis "Extensible binary serialization format") + (wrapped false) + (flags (-safe-string)) + (libraries (easy-format)))) diff --git a/test/jbuild b/test/jbuild new file mode 100644 index 0000000..3ca83c9 --- /dev/null +++ b/test/jbuild @@ -0,0 +1,11 @@ +(jbuild_version 1) + +(executable + ((name test_biniou) + (flags (-safe-string)) + (libraries (biniou unix)))) + +(alias + ((name runtest) + (deps (test_biniou.exe)) + (action (run ${<})))) diff --git a/test_biniou.ml b/test/test_biniou.ml index f75b46d..f75b46d 100644 --- a/test_biniou.ml +++ b/test/test_biniou.ml |