Module B0_text.Tloc

Text locations.

Text locations

type fpath = string

The type for file paths.

val no_file : fpath

no_file is Fpath.t "-", a path used when no file is specified.

type pos = int

The type for zero-based, absolute, byte positions in text.

type line = int

The type for one-based, line numbers in the text. Lines increment after a line feed '\n' (U+000A), a carriage return '\r' (U+000D) or a carriage return and a line feed "\r\n" (<U+000D,U+000A>).

type line_pos = line * pos

The type for line positions. The line number and the byte position of the first element on the line. The later is the byte position after the newline which may not exist (at the end of file).

type t

The type for text locations. A text location is a range of byte positions and the lines on which they occur in the UTF-8 encoded text of a particular file.

val v : file:fpath -> sbyte:pos -> ebyte:pos -> sline:line_pos -> eline:line_pos -> t

v ~file ~sbyte ~ebyte ~sline ~eline is a contructor for text locations. See corresponding accessors for the semantics. If you don't have a file use no_file.

val file : t -> fpath

file l is l's file.

val sbyte : t -> pos

sbyte l is l's start position.

val ebyte : t -> pos

ebyte l is l's end position.

val sline : t -> line_pos

sline l is the line position on which sbyte l lies.

val eline : t -> line_pos

elin l is the line position on which ebyte l lies.

val nil : t

loc_nil is an invalid location.

val merge : t -> t -> t

merge l0 l1 merges the location l0 and l1 to the smallest location that spans both location. The file path taken from l0.

val to_start : t -> t

to_start l has both start and end positions at l's start.

val to_end : t -> t

to_end l has both start and end positions at l's end.

val restart : at:t -> t -> t

restart ~at l is l with the start position of at.

val pp_ocaml : Stdlib.Format.formatter -> t -> unit

pp_ocaml formats location like the OCaml compiler.

val pp_gnu : Stdlib.Format.formatter -> t -> unit

pp_gnu formats location according to the GNU convention.

val pp : Stdlib.Format.formatter -> t -> unit

pp is pp_gnu.

val pp_dump : Stdlib.Format.formatter -> t -> unit

pp_dump formats raw data for debugging.

Substitutions and insertions

Strictly speaking this doesn't belong here but here you go.

val string_subrange : ?first:int -> ?last:int -> string -> string

string_subrange ~first ~last s are the consecutive bytes of s whose indices exist in the range [first;last].

first defaults to 0 and last to String.length s - 1.

Note that both first and last can be any integer. If first > last the interval is empty and the empty string is returned.

val string_replace : start:int -> stop:int -> rep:string -> string -> string

string_replace ~start ~stop ~rep s replaces the index range [start;stop-1] of s with rep as follows. If start = stop the rep is inserted before start. start and stop must be in range [0;String.length s] and start <= stop or Invalid_argument is raised.