Module Jstr
JavaScript strings
val v : string -> t
v s
is the UTF-8 encoded OCaml strings
as a JavaScript string.
val length : t -> int
length s
is the length ofs
.
val get : t -> int -> Stdlib.Uchar.t
get s i
is the Unicode character at positioni
ins
. If this happens to be a lone low or any high surrogate surrogate,Uchar
.rep is returned. RaisesInvalid_argument
ifi
is out of bounds.
Constants
val empty : t
empty
is an empty string.
val sp : t
sp
isJstr.v " "
.
val nl : t
nl
isJstr.v "\n"
.
Assembling
val concat : ?sep:t -> t list -> t
concat ?sep ss
is the concatenates the list of stringsss
insertingsep
between each of them (defaults toempty
).
val pad_start : ?pad:t -> int -> t -> t
pad_start ~pad n s
iss
withpad
strings prepended tos
until the length of the result isn
ors
iflength s >= n
. The first prependedpad
may be truncated to satisfy the constraint.pad
defaults tosp
.Warning. Since
length
is neither the number of Unicode characters ofs
nor its number of grapheme clusters, if you are using this for visual layout, it will fail in many cases. At least consider normalizings
to`NFC
before.
val pad_end : ?pad:t -> int -> t -> t
pad_end ~pad n s
iss
withpad
strings appended tos
until thelength
of the result isn
ors
iflength s >= n
. The last appendedpad
may be truncated to satisfy the constraint.pad
defaults tosp
.Warning. Since
length
is neither the number of Unicode characters ofs
nor its number of grapheme clusters, if you are using this for visual layout, it will fail in many cases. At least consider normalizings
to`NFC
before.
Finding
Breaking
val slice : ?start:int -> ?stop:int -> t -> t
slice ~start ~stop s
is the string s.start
, s.start+1
, ... s.stop - 1
.start
defaults to0
andstop
tolength s
.If
start
orstop
are negative they are subtracted fromlength s
. This means that-1
denotes the last character of the string.
val sub : ?start:int -> ?len:int -> t -> t
sub ~start ~len s
is the string s.start
, ... s.start + len - 1
.start
default to0
andlen
tolength s - start
.If
start
is negative it is subtracted fromlength s
. This means that-1
denotes the last character of the string. Iflen
is negative it is treated as0
.
Traversing and transforming
val fold_uchars : (Stdlib.Uchar.t -> 'a -> 'a) -> t -> 'a -> 'a
fold_uchars f acc s
foldsf
over the Unicode characters ofs
starting withacc
. Decoding errors (that is unpaired UTF-16 surrogates) are reported asUchar
.rep.
val fold_jstr_uchars : (t -> 'a -> 'a) -> t -> 'a -> 'a
fold_jstr_uchars
is likefold_uchars
but the characters are given as strings.
Normalization
For more information on normalization consult a short introduction, the UAX #15 Unicode Normalization Forms and normalization charts.
type normalization
=[
|
`NFD
|
`NFC
|
`NFKD
|
`NFKC
]
The type for normalization forms.
`NFD
normalization form D, canonical decomposition.`NFC
normalization form C, canonical decomposition followed by canonical composition.`NFKD
normalization form KD, compatibility decomposition.`NFKC
normalization form KC, compatibility decomposition, followed by canonical composition.
val normalized : normalization -> t -> t
normalized nf t
ist
normalized tonf
.
Case mapping
For more information about case see the Unicode case mapping FAQ and the case mapping charts. Note that these algorithms are insensitive to language and context and may produce sub-par results for some users.
Predicates and comparisons
val is_empty : t -> bool
is_empty s
istrue
iffs
is an empty string.
val starts_with : sub:t -> t -> bool
starts_with ~sub s
istrue
iffs
starts withsub
(as perequal
).
Conversions
val of_uchar : Stdlib.Uchar.t -> t
of_uchar u
is a string made ofu
.
val of_char : char -> t
of_char c
is a string made ofc
.
val to_string : t -> string
to_string s
iss
as an UTF-8 encoded OCaml string.
val of_string : string -> t
of_string s
is the UTF-8 encoded OCaml strings
as a JavaScript string.
val to_int : ?base:int -> t -> int option
to_int s
is the integer resulting from parsings
as a number in basebase
(guessed by default). The function usesNumber.parseInt
and mapsFloat
.nan results toNone
.
val of_int : ?base:int -> int -> t
of_int ~base i
formatsi
as a number in basebase
(defaults to10
). Conversion is performed viaNumber.toString
.
val to_float : t -> float
to_float s
is the floating point number resulting from parsings
. This always succeeds and returnsFloat
.nan on unparseable inputs. The function usesNumber.parseFloat
.
val of_float : ?frac:int -> float -> t
of_float ~frac n
formatsn
withfrac
fixed fractional digits (or as needed if unspecified). This function usesNumber.toFixed
iff
is specified andNumber.toString
otherwise.