Module Serialk_text.Tloc

Text locations.

File paths

type fpath = string

The type for file paths.

val no_file : fpath

no_file is "-". A file path to use when there isn't one.

Byte and line positions

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 newline which is either 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. If the text ends with a newline that position is equal to the text's length, it is not a valid byte index.

Text locations

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 nil : t

nil is an invalid text location.

val v : file:fpath -> first_byte:pos -> first_line:line_pos -> last_byte:pos -> last_line:line_pos -> t

v ~file ~first_byte ~first_line ~last_byte ~last_line is a text location with the given arguments, 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 first_byte : t -> pos

first_byte l is l's first byte.

val last_byte : t -> pos

last_byte l is l's last byte.

val first_line : t -> line_pos

first_line l is the line position on which first_byte l lies.

val last_line : t -> line_pos

last_line l is the line position on which last_byte l lies.

val to_first : t -> t

to_first l has both first and last positions set to l's first position.

val to_last : t -> t

to_last l has both first and last positions set to l's last position.

val span : t -> t -> t

span l0 l1 is the span from the smallest location of l0 and l1 to the greatest location of l0 and l1. The file path is taken from the greatest location.

val reloc : first:t -> last:t -> t

reloc ~first ~last uses the first position of first, the last position of last and the file of last.

Formatters

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.