Skip to content

Commit

Permalink
Backport function.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jun 27, 2023
1 parent 4b44317 commit 20d11ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/lib/sedlexing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ exception MalFormed

module Uchar = struct
(* This for compatibility with ocaml < 4.14.0 *)
let utf_8_byte_length _ = 1
let utf_16_byte_length _ = 1
let utf_8_byte_length u =
match Uchar.to_int u with
| u when u < 0 -> assert false
| u when u <= 0x007F -> 1
| u when u <= 0x07FF -> 2
| u when u <= 0xFFFF -> 3
| u when u <= 0x10FFFF -> 4
| _ -> assert false

let utf_16_byte_length u =
match Uchar.to_int u with
| u when u < 0 -> assert false
| u when u <= 0xFFFF -> 2
| u when u <= 0x10FFFF -> 4
| _ -> assert false

let () =
ignore utf_8_byte_length;
Expand Down
10 changes: 3 additions & 7 deletions src/lib/sedlexing.mli
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ val lexeme_start : lexbuf -> int

(** [Sedlexing.lexeme_start lexbuf] returns the offset in the
input stream of the first byte of the matched string.
The first code point of the stream has offset 0. Returned
value is the same as its code point equivalent when compiled with
OCaml < 4.14.0 *)
The first code point of the stream has offset 0. *)
val lexeme_bytes_start : lexbuf -> int

(** [Sedlexing.lexeme_end lexbuf] returns the offset in the input
Expand All @@ -105,8 +103,7 @@ val lexeme_end : lexbuf -> int
(** [Sedlexing.lexeme_end lexbuf] returns the offset in the input
stream of the byte following the last code point of the
matched string. The first character of the stream has offset
0. Returned value is the same as its code point equivalent when
compiled with OCaml < 4.14.0 *)
0. *)
val lexeme_bytes_end : lexbuf -> int

(** [Sedlexing.loc lexbuf] returns the pair
Expand Down Expand Up @@ -140,8 +137,7 @@ val lexing_positions : lexbuf -> Lexing.position * Lexing.position
(** [Sedlexing.lexing_bytes_positions lexbuf] returns the start and end
positions, in bytes, of the current token, using a record of type
[Lexing.position]. This is intended for consumption
by parsers like those generated by [Menhir]. Returned value is the
same as its code point equivalent when compiled with OCaml < 4.14.0 *)
by parsers like those generated by [Menhir]. *)
val lexing_bytes_positions : lexbuf -> Lexing.position * Lexing.position

(** [Sedlexing.new_line lexbuf] increments the line count and
Expand Down

0 comments on commit 20d11ab

Please sign in to comment.