lexer: lex_string: Use buffer directly.

This commit is contained in:
Julien Lepiller 2020-12-05 13:57:24 +01:00
parent c195c3b83e
commit eb98434c9f

View File

@ -74,30 +74,28 @@ let rec lex_string lexbuf =
let buff = get_next_buff lexbuf 200 in let buff = get_next_buff lexbuf 200 in
let i = ref 0 in let i = ref 0 in
let continue = ref true in let continue = ref true in
let str = ref [] in let buf = Buffer.create (Bytes.length buff) in
while (!continue = true) && (!i < Bytes.length buff) do while (!continue = true) && (!i < Bytes.length buff) do
begin match Bytes.get buff !i with begin match Bytes.get buff !i with
| '\\' -> begin match Bytes.get buff (!i+1) with | '\\' -> begin match Bytes.get buff (!i+1) with
| '\\' -> str := '\\' :: !str | '\\' -> Buffer.add_char buf '\\'
| '\'' -> str := '\'' :: !str | '\'' -> Buffer.add_char buf '\''
| '"' -> str := '"' :: !str | '"' -> Buffer.add_char buf '"'
| 'n' -> str := '\010' :: !str | 'n' -> Buffer.add_char buf '\010'
| 't' -> str := '\009' :: !str | 't' -> Buffer.add_char buf '\009'
| 'b' -> str := '\008' :: !str | 'b' -> Buffer.add_char buf '\008'
| 'r' -> str := '\013' :: !str | 'r' -> Buffer.add_char buf '\013'
| ' ' -> str := ' ' :: !str | ' ' -> Buffer.add_char buf ' '
(* We skip many cases that do not happen in lexer.mll, such as \\[0-9]{3}, (* We skip many cases that do not happen in lexer.mll, such as \\[0-9]{3},
* or \\ before a new line. *) * or \\ before a new line. *)
| _ -> raise Bad_rule | _ -> raise Bad_rule
end; incr i end; incr i
| '"' -> continue := false | '"' -> continue := false
| c -> str := c :: !str | c -> Buffer.add_char buf c
end; end;
incr i incr i
done; done;
advance lexbuf !i; advance lexbuf !i;
let buf = Buffer.create (List.length !str) in
List.iter (Buffer.add_char buf) !str;
if !continue = true if !continue = true
then (Buffer.contents buf)::(lex_string lexbuf) then (Buffer.contents buf)::(lex_string lexbuf)
else (Buffer.contents buf)::[];; else (Buffer.contents buf)::[];;