Skip to content

Commit

Permalink
Fix compilation warnings for OCaml >= 4.02
Browse files Browse the repository at this point in the history
The warnings were related to the newly introduced bytes type.
  • Loading branch information
alavrik committed Jan 25, 2015
1 parent 7fe75a1 commit 8e5ff77
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 36 deletions.
1 change: 1 addition & 0 deletions opam
Expand Up @@ -18,4 +18,5 @@ depends: [
"xmlm"
"optcomp"
"base64" {>="2.0.0"}
"base-bytes"
]
2 changes: 1 addition & 1 deletion piqilib/Makefile
Expand Up @@ -11,7 +11,7 @@ LIBINSTALL_FILES += \

STATIC = 1 # force creation of static library (don't create dlls)

PACKS = ulex easy-format xmlm base64
PACKS = ulex easy-format xmlm base64 bytes


SOURCES = \
Expand Down
8 changes: 5 additions & 3 deletions piqilib/piq_lexer.ml
Expand Up @@ -153,13 +153,15 @@ let utf8_of_list l =


let string_of_list l =
let s = String.create (List.length l) in
let s = Bytes.create (List.length l) in
let rec aux i = function
| [] -> ()
| h::t ->
s.[i] <- Char.chr h; aux (i+1) t
Bytes.set s i (Char.chr h);
aux (i+1) t
in
aux 0 l; s
aux 0 l;
Bytes.unsafe_to_string s


let parse_string_literal s =
Expand Down
11 changes: 6 additions & 5 deletions piqilib/piqi_getopt.ml
Expand Up @@ -225,8 +225,9 @@ let parse_name_arg s =
let n = String.sub s 1 (String.length s - 1) in
if Piqi_name.is_valid_name n ~allow:"."
then (
s.[0] <- '.'; (* replace '-' with '.' to turn it into a Piq name *)
Piq_lexer.Word s
let s = Bytes.of_string s in
Bytes.set s 0 '.'; (* replace '-' with '.' to turn it into a Piq name *)
Piq_lexer.Word (Bytes.unsafe_to_string s)
)
else error ("invalid name: " ^ U.quote s)

Expand Down Expand Up @@ -304,9 +305,9 @@ let parse_argv start =
(* only letters are allowed as single-letter options *)
| 'a'..'z' | 'A'..'Z' ->
(* creating Piq name: '.' followed by the letter *)
let word = String.create 2 in
word.[0] <- '.'; word.[1] <- c;
let tok = Piq_lexer.Word word in
let word = Bytes.create 2 in
Bytes.set word 0 '.'; Bytes.set word 1 c;
let tok = Piq_lexer.Word (Bytes.unsafe_to_string word) in
(make_token i tok) :: (aux (j+1))
| _ ->
error i ("invalid single-letter argument: " ^ Char.escaped c)
Expand Down
7 changes: 4 additions & 3 deletions piqilib/piqi_json_parser.mll
Expand Up @@ -352,9 +352,10 @@ and finish_stringlit v = parse
| [^ '"' '\\' '\x00'-'\x1F'])* '"'
{
let len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in
let s = String.create (len+1) in
s.[0] <- '"';
String.blit lexbuf.lex_buffer lexbuf.lex_start_pos s 1 len;
let s = Bytes.create (len+1) in
Bytes.set s 0 '"';
Bytes.blit lexbuf.lex_buffer lexbuf.lex_start_pos s 1 len;
let s = Bytes.unsafe_to_string s in
check_adjust_utf8 v lexbuf s 1 len;
s
}
Expand Down
18 changes: 9 additions & 9 deletions piqilib/piqi_piqirun.ml
@@ -1,5 +1,5 @@
(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014 Anton Lavrik
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015 Anton Lavrik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -150,11 +150,11 @@ module IBuf =
res
| Channel x ->
let start_pos = pos_in x in
let s = String.create length in
let s = Bytes.create length in
(try Pervasives.really_input x s 0 length
with End_of_file -> raise End_of_buffer
);
of_string s start_pos
of_string (Bytes.unsafe_to_string s) start_pos


let of_string x =
Expand Down Expand Up @@ -1143,27 +1143,27 @@ let gen_varint64_field code x =
*)

let gen_fixed32_value x = (* little-endian *)
let s = String.create 4 in
let s = Bytes.create 4 in
let x = ref x in
for i = 0 to 3
do
let b = Char.chr (Int32.to_int (Int32.logand !x 0xFFl)) in
s.[i] <- b;
Bytes.set s i b;
x := Int32.shift_right_logical !x 8
done;
ios s
ios (Bytes.unsafe_to_string s)


let gen_fixed64_value x = (* little-endian *)
let s = String.create 8 in
let s = Bytes.create 8 in
let x = ref x in
for i = 0 to 7
do
let b = Char.chr (Int64.to_int (Int64.logand !x 0xFFL)) in
s.[i] <- b;
Bytes.set s i b;
x := Int64.shift_right_logical !x 8
done;
ios s
ios (Bytes.unsafe_to_string s)


let gen_fixed32_field code x =
Expand Down
19 changes: 11 additions & 8 deletions piqilib/piqi_util.ml
Expand Up @@ -24,12 +24,13 @@ let string_subst_char s x y =
then s
else
(* preserve the original string *)
let s = String.copy s in
for i = 0 to (String.length s) - 1
let s = Bytes.of_string s in
for i = 0 to (Bytes.length s) - 1
do
if s.[i] = x
then s.[i] <- y
done; s
if Bytes.get s i = x
then Bytes.set s i y
done;
Bytes.unsafe_to_string s


let list_of_string s =
Expand All @@ -42,13 +43,15 @@ let list_of_string s =


let string_of_list l =
let s = String.create (List.length l) in
let s = Bytes.create (List.length l) in
let rec aux i = function
| [] -> ()
| h::t ->
s.[i] <- h; aux (i+1) t
Bytes.set s i h;
aux (i+1) t
in
aux 0 l; s
aux 0 l;
Bytes.unsafe_to_string s


let dashes_to_underscores s =
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -12,7 +12,7 @@ PACKS = unix
# OCAMLPATH to reduce complexity of the windows build (e.g. avoid converting :
# path separators to ; along with reverting slashes); we can't simply symlink it
# to the build dir either because symlinks don't work on windows
PACKS += ulex easy-format xmlm base64
PACKS += ulex easy-format xmlm base64 bytes
INCDIRS = ../piqilib
LIBS = ../piqilib/piqilib

Expand Down
8 changes: 4 additions & 4 deletions src/piqi_http.ml
Expand Up @@ -219,7 +219,7 @@ let parse_response_header buf len =


let read_buf_size = 8096
let read_buf = String.create read_buf_size
let read_buf = Bytes.create read_buf_size


(* reading input in [read_buf_size] chunks *)
Expand All @@ -235,7 +235,7 @@ let read_body ch body_buf =
then () (* eof *)
else (
(* add a portion of input we've just read to the buffer *)
Buffer.add_substring body_buf read_buf 0 len;
Buffer.add_substring body_buf (Bytes.unsafe_to_string read_buf) 0 len;
aux ()
)
in aux ()
Expand All @@ -245,12 +245,12 @@ let read_response ch =
let len = read_next ch in
if len = 0 then error "response is empty";

let status_code, headers, body_offset = parse_response_header read_buf len in
let status_code, headers, body_offset = parse_response_header (Bytes.unsafe_to_string read_buf) len in

(* create a buffer for reading HTTP message body *)
let body_buf = Buffer.create read_buf_size in
(* add a piece of previously read body to the body buffer *)
Buffer.add_substring body_buf read_buf body_offset (len - body_offset);
Buffer.add_substring body_buf (Bytes.unsafe_to_string read_buf) body_offset (len - body_offset);

(* read the remainder of the body *)
(* XXX: use "Content-Length" header? *)
Expand Down
4 changes: 2 additions & 2 deletions src/piqi_rpc.ml
Expand Up @@ -52,9 +52,9 @@ let read_int32_be ch =


let read_string ch size =
let res = String.create size in
let res = Bytes.create size in
really_input ch res 0 size;
res
Bytes.unsafe_to_string res


(* serialize and send one length-delimited packet to the output channel *)
Expand Down

0 comments on commit 8e5ff77

Please sign in to comment.