Skip to content

Commit

Permalink
Merge pull request #249 from jpdeplaix/safe-string
Browse files Browse the repository at this point in the history
Enable ocp-indent to be safe-string compatible
  • Loading branch information
AltGr committed Jul 19, 2017
2 parents d3f250b + 6503a1b commit cae4e8c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions opam
Expand Up @@ -19,6 +19,7 @@ build: [
depends: [
"ocp-build" {>= "1.99.6-beta"}
"cmdliner" {>= "1.0.0"}
"base-bytes"
]
post-messages: [
"This package requires additional configuration for use in editors. Install package 'user-setup', or manually:
Expand Down
30 changes: 16 additions & 14 deletions src/approx_lexer.mll
Expand Up @@ -107,7 +107,7 @@ let disable_extensions () =

(* To buffer string literals *)

let initial_string_buffer = String.create 256
let initial_string_buffer = Bytes.create 256
let string_buff = ref initial_string_buffer
let string_index = ref 0

Expand All @@ -116,18 +116,18 @@ let reset_string_buffer () =
string_index := 0

let store_string_char c =
if !string_index >= String.length (!string_buff) then begin
let new_buff = String.create (String.length (!string_buff) * 2) in
String.blit (!string_buff) 0 new_buff 0 (String.length (!string_buff));
if !string_index >= Bytes.length (!string_buff) then begin
let new_buff = Bytes.create (Bytes.length (!string_buff) * 2) in
Bytes.blit (!string_buff) 0 new_buff 0 (Bytes.length (!string_buff));
string_buff := new_buff
end;
String.unsafe_set (!string_buff) (!string_index) c;
Bytes.unsafe_set (!string_buff) (!string_index) c;
incr string_index

let get_stored_string () =
let s = String.sub (!string_buff) 0 (!string_index) in
let s = Bytes.sub (!string_buff) 0 (!string_index) in
string_buff := initial_string_buffer;
s
Bytes.to_string s

(* To store the position of the beginning of a string and comment *)
let string_start_loc = ref (-1);;
Expand Down Expand Up @@ -218,15 +218,17 @@ let cvt_nativeint_literal s =
(* Remove underscores from float literals *)

let remove_underscores s =
let l = String.length s in
let s = Bytes.of_string s in
let l = Bytes.length s in
let rec remove src dst =
if src >= l then
if dst >= l then s else String.sub s 0 dst
if dst >= l then s else Bytes.sub s 0 dst
else
match s.[src] with
match Bytes.get s src with
'_' -> remove (src + 1) dst
| c -> s.[dst] <- c; remove (src + 1) (dst + 1)
in remove 0 0
| c -> Bytes.set s dst c; remove (src + 1) (dst + 1)
in
Bytes.to_string (remove 0 0)

(* Update the current location with file name and line number. *)

Expand Down Expand Up @@ -387,7 +389,7 @@ rule parse_token = parse
token
| _ ->
rewind lexbuf 1;
match lexbuf.lex_buffer.[lexbuf.lex_curr_pos - 1] with
match Bytes.get lexbuf.lex_buffer (lexbuf.lex_curr_pos - 1) with
| ']' -> RBRACKET
| 'v' -> LIDENT "v"
| _ -> assert false
Expand Down Expand Up @@ -525,7 +527,7 @@ and comment = parse
| _s -> assert false
in
let block =
match lexbuf.lex_buffer.[lexbuf.lex_curr_pos - 1] with
match Bytes.get lexbuf.lex_buffer (lexbuf.lex_curr_pos - 1) with
| '[' -> Code
| 'v' -> Verbatim
| _ -> assert false
Expand Down
9 changes: 5 additions & 4 deletions src/indentConfig.ml
Expand Up @@ -191,11 +191,12 @@ let man =
let pre s =
List.fold_right
(fun line acc ->
let i = ref 0 and line = String.copy line in
while !i < String.length line && line.[!i] = ' ' do
line.[!i] <- '\xa0'; incr i done;
let i = ref 0 and line = Bytes.copy line in
while !i < Bytes.length line && Bytes.get line (!i) = ' ' do
Bytes.set line (!i) '\xa0'; incr i done;
let line = Bytes.to_string line in
`P line :: (if acc = [] then [] else `Noblank :: acc))
(Util.string_split '\n' s) []
(List.map Bytes.of_string (Util.string_split '\n' s)) []
in
[ `P "A configuration definition is a list of bindings in the form \
$(i,NAME=VALUE) or of $(i,PRESET), separated by commas or newlines";
Expand Down
4 changes: 2 additions & 2 deletions src/nstream.ml
Expand Up @@ -36,7 +36,7 @@ let of_string ?(start_pos=Position.zero) ?(start_offset=0) string =
let lexbuf = {
Lexing.
refill_buff = (fun lexbuf -> lexbuf.Lexing.lex_eof_reached <- true);
lex_buffer = string;
lex_buffer = Bytes.of_string string;
lex_buffer_len = String.length string;
lex_abs_pos = start_offset;
lex_start_pos = start_offset;
Expand Down Expand Up @@ -88,7 +88,7 @@ let of_channel ?(start_pos=Position.zero) ic =
let buf = Buffer.create 511 in
let reader str count =
let n = input ic str 0 count in
Buffer.add_substring buf str 0 n;
Buffer.add_substring buf (Bytes.to_string str) 0 n;
n
in
let lexbuf = Lexing.from_function reader in
Expand Down
5 changes: 5 additions & 0 deletions src/ocp-indent.ocp
Expand Up @@ -6,6 +6,9 @@ begin library "ocp-indent.lexer"
"approx_tokens.ml"
"approx_lexer.mll"
]
requires = [
"bytes"
]
bundle = [ "ocp-indent" ]
end

Expand All @@ -22,6 +25,7 @@ begin library "ocp-indent.utils"
files += [ "ocaml_4/compat.ml" ]
}
requires = [
"bytes"
"ocp-indent.lexer"
]
bundle = [ "ocp-indent" ]
Expand All @@ -48,6 +52,7 @@ begin library "ocp-indent.lib"
"indentPrinter.ml"
]
requires = [
"bytes"
"ocp-indent.lexer"
"ocp-indent.utils"
]
Expand Down

0 comments on commit cae4e8c

Please sign in to comment.