Module B0_std.String.Ascii

module Ascii: sig .. end

US-ASCII string support.

The following functions act only on US-ASCII code points, that is on the bytes in range [0x00;0x7F]. The functions can be safely used on UTF-8 encoded strings but they will, of course, only deal with US-ASCII related matters.

References.


Predicates

val is_valid : string -> bool

is_valid s is true iff only for all indices i of s, s.[i] is an US-ASCII character, i.e. a byte in the range [0x00;0x1F].

Casing transforms

The functions can be safely used on UTF-8 encoded strings; they will of course only deal with US-ASCII casings.

val uppercase : string -> string

uppercase s is s with US-ASCII characters 'a' to 'z' mapped to 'A' to 'Z'.

val lowercase : string -> string

lowercase s is s with US-ASCII characters 'A' to 'Z' mapped to 'a' to 'z'.

val capitalize : string -> string

capitalize s is like B0_std.String.Ascii.uppercase but performs the map only on s.[0].

val uncapitalize : string -> string

uncapitalize s is like B0_std.String.Ascii.lowercase but performs the map only on s.[0].

Converting to US-ASCII hexadecimal characters

val to_hex : string -> string

to_hex s is the sequence of bytes of s as US-ASCII lowercase hexadecimal digits.

val of_hex : string -> (string, int) Stdlib.result

of_hex h parses a sequence of US-ASCII (lower or upper cased) hexadecimal digits from h into its corresponding byte sequence. Error n is returned either with n an index in the string which is not a hexadecimal digit or the length of h if it there is a missing digit at the end.

Converting to printable US-ASCII characters

val escape : string -> string

escape s escapes bytes of s to a representation that uses only US-ASCII printable characters. More precisely:

Use B0_std.String.Ascii.unescape to unescape. The invariant unescape (escape s) = Ok s holds.

val unescape : string -> (string, int) Stdlib.result

unescape s unescapes from s the escapes performed by B0_std.String.Ascii.escape. More precisely:

Any other escape following a '\\' not defined above makes the function return Error i with i the index of the error in the string.

val ocaml_string_escape : string -> string

ocaml_string_escape s escapes the bytes of s to a representation that uses only US-ASCII printable characters and according to OCaml's conventions for string literals. More precisely:

Use B0_std.String.Ascii.ocaml_unescape to unescape. The invariant ocaml_unescape (ocaml_string_escape s) = Ok s holds.

val ocaml_unescape : string -> (string, int) Stdlib.result

ocaml_unescape s unescapes from s the escape sequences afforded by OCaml string and char literals. More precisely:

Any other escape following a '\\' not defined above makes the function return Error i with i the location of the error in the string.