package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module BatString.CapSource

Capabilities for strings.

This modules provides the same set of features as String, but with the added twist that strings can be made read-only or write-only. Read-only strings may then be safely shared and distributed.

  • since 2.8.0 the interface and implementation of the Cap

module changed to accommodate the -safe-string transition. OCaml now uses two distinct types for mutable and immutable string, which is a good design but is not as expressive as the present Cap interface, and actually makes implementing Cap harder than it previously was. We are aware that current state is not optimal for heavy Cap users; if you are one of them, please get in touch (on the Batteries issue tracker for example) so that we can discuss code refactoring and improvements for this sub-module.

Sourcetype 'a t

The type of capability strings.

If 'a contains [`Read], the contents of the string may be read. If 'a contains [`Write], the contents of the string may be written.

Other (user-defined) capabilities may be added without loss of performance or features. For instance, a string could be labelled [`Read | `UTF8] to state that it contains UTF-8 encoded data and may be used only for reading. Conversely, a string labelled with [] (i.e. nothing) can neither be read nor written. It can only be compared for textual equality using OCaml's built-in compare or for physical equality using OCaml's built-in ==.

Sourceval length : _ t -> int
Sourceval is_empty : _ t -> bool
Sourceval get : [> `Read ] t -> int -> char
Sourceval set : [> `Write ] t -> int -> char -> unit
Sourceval create : int -> _ t

Constructors

Sourceval of_string : Bytes.t -> _ t

Adopt a regular byte sequence.

One could give a perfectly safe semantics to an of_string : string -> _ t function, but this requires making a copy of the string. Previous versions of this interface advertised the absence of performance overhead, so it's better to warn the user and let them decide (through the use of either Bytes.of_string or Bytes.unsafe_of_string) whether they can safely avoid a copy or need to insert one.

  • deprecated Use Cap.of_bytes instead
Sourceval of_bytes : Bytes.t -> _ t

Adopt a regular byte sequence.

Note that adopting a byte sequence, even at the restrictive `Read type, does not make a copy. Having a `Read string prevents you (and anyone you pass it to) from writing it, but your parent may have knowledge of the string at a more permissive type and perform writes on it.

If you want to use a `Read string and assume it will not get written to, you should either properly "adopt" it by ensuring unique ownership (this cannot be guaranteed by the type system), or make a copy of it at adoption time: Cap.of_bytes (Bytes.copy buf).

  • since 2.8.0
Sourceval to_string : [ `Read | `Write ] t -> Bytes.t

Return a capability string as a regular byte sequence.

We cannot return a string here, and it would be incorrect to do so even if we required [< `Read] t as input. Indeed, one can start from a writeable byte sequence, and then use the read_only function below to cast it into a [`Read] t. Capabilities are used to enforce local protocol (only reads, only writes, both reads and writes...), they don't guarantee that other users of the same (shared) value all follow the same protocol. To safely reason about mutability one needs stronger ownership guarantees.

If you want to obtain an immutable string out of a capability string, you should first convert it to a mutable byte sequence and then copy it into an immutable string. If you have extra knowledge about the ownership of the value, you may use unsafe conversion functions to avoid the copy, see the documentation of unsafe conversion functions.

  • deprecated Use Cap.to_bytes instead
Sourceval to_bytes : [ `Read | `Write ] t -> Bytes.t

Return a capability string as a regular byte sequence.

  • since 2.8.0
Sourceval read_only : [> `Read ] t -> [ `Read ] t

Drop capabilities to read only.

Sourceval write_only : [> `Write ] t -> [ `Write ] t

Drop capabilities to write only.

Sourceval make : int -> char -> _ t
Sourceval init : int -> (int -> char) -> _ t
Sourceval enum : [> `Read ] t -> char BatEnum.t

Conversions

Sourceval of_enum : char BatEnum.t -> _ t
Sourceval backwards : [> `Read ] t -> char BatEnum.t
Sourceval of_backwards : char BatEnum.t -> _ t
Sourceval of_list : char list -> _ t
Sourceval to_list : [> `Read ] t -> char list
Sourceval of_int : int -> _ t
Sourceval of_float : float -> _ t
Sourceval of_char : char -> _ t
Sourceval to_int : [> `Read ] t -> int
Sourceval to_float : [> `Read ] t -> float

String traversals

Sourceval map : (char -> char) -> [> `Read ] t -> _ t
Sourceval mapi : (int -> char -> char) -> [> `Read ] t -> _ t
Sourceval fold_left : ('a -> char -> 'a) -> 'a -> [> `Read ] t -> 'a
Sourceval fold_lefti : ('a -> int -> char -> 'a) -> 'a -> [> `Read ] t -> 'a
Sourceval fold_right : (char -> 'a -> 'a) -> [> `Read ] t -> 'a -> 'a
Sourceval fold_righti : (int -> char -> 'a -> 'a) -> [> `Read ] t -> 'a -> 'a
Sourceval filter : (char -> bool) -> [> `Read ] t -> _ t
Sourceval filter_map : (char -> char option) -> [> `Read ] t -> _ t
Sourceval iter : (char -> unit) -> [> `Read ] t -> unit

Finding

Sourceval index : [> `Read ] t -> char -> int
Sourceval rindex : [> `Read ] t -> char -> int
Sourceval index_from : [> `Read ] t -> int -> char -> int
Sourceval rindex_from : [> `Read ] t -> int -> char -> int
Sourceval contains : [> `Read ] t -> char -> bool
Sourceval contains_from : [> `Read ] t -> int -> char -> bool
Sourceval rcontains_from : [> `Read ] t -> int -> char -> bool
Sourceval find : [> `Read ] t -> [> `Read ] t -> int
Sourceval find_from : [> `Read ] t -> int -> [> `Read ] t -> int
Sourceval rfind : [> `Read ] t -> [> `Read ] t -> int
Sourceval rfind_from : [> `Read ] t -> int -> [> `Read ] t -> int
Sourceval ends_with : [> `Read ] t -> [> `Read ] t -> bool
Sourceval starts_with : [> `Read ] t -> [> `Read ] t -> bool
Sourceval exists : [> `Read ] t -> [> `Read ] t -> bool
Sourceval count_char : [> `Read ] t -> char -> int

Transformations

Sourceval lchop : ?n:int -> [> `Read ] t -> _ t
Sourceval rchop : ?n:int -> [> `Read ] t -> _ t
Sourceval chop : ?l:int -> ?r:int -> [> `Read ] t -> _ t
Sourceval trim : [> `Read ] t -> _ t
Sourceval quote : [> `Read ] t -> string
Sourceval left : [> `Read ] t -> int -> _ t
Sourceval right : [> `Read ] t -> int -> _ t
Sourceval head : [> `Read ] t -> int -> _ t
Sourceval tail : [> `Read ] t -> int -> _ t
Sourceval strip : ?chars:[> `Read ] t -> [> `Read ] t -> _ t
Sourceval copy : [> `Read ] t -> _ t
Sourceval sub : [> `Read ] t -> int -> int -> _ t
Sourceval fill : [> `Write ] t -> int -> int -> char -> unit
Sourceval blit : [> `Read ] t -> int -> [> `Write ] t -> int -> int -> unit
Sourceval concat : [> `Read ] t -> [> `Read ] t list -> _ t
Sourceval escaped : [> `Read ] t -> _ t
Sourceval replace_chars : (char -> [> `Read ] t) -> [> `Read ] t -> _ t
Sourceval replace : str:[> `Read ] t -> sub:[> `Read ] t -> by:[> `Read ] t -> bool * _ t
Sourceval nreplace : str:[> `Read ] t -> sub:[> `Read ] t -> by:[> `Read ] t -> _ t
Sourceval repeat : [> `Read ] t -> int -> _ t
Sourceval split : [> `Read ] t -> by:[> `Read ] t -> _ t * _ t

Splitting around

Sourceval rsplit : [> `Read ] t -> by:[> `Read ] t -> _ t * _ t
Sourceval nsplit : [> `Read ] t -> by:[> `Read ] t -> _ t list
Sourceval splice : [ `Read | `Write ] t -> int -> int -> [> `Read ] t -> _ t
Sourceval join : [> `Read ] t -> [> `Read ] t list -> _ t
Sourceval slice : ?first:int -> ?last:int -> [> `Read ] t -> _ t
Sourceval explode : [> `Read ] t -> char list
Sourceval implode : char list -> _ t

Comparisons

Sourceval compare : [> `Read ] t -> [> `Read ] t -> int
Sourceval icompare : [> `Read ] t -> [> `Read ] t -> int

Printing

Sourceval print : 'a BatInnerIO.output -> [> `Read ] t -> unit
Sourceval println : 'a BatInnerIO.output -> [> `Read ] t -> unit
Sourceval print_quoted : 'a BatInnerIO.output -> [> `Read ] t -> unit
Sourcemodule Exceptionless : sig ... end

Exceptionless counterparts for error-raising operations

OCaml

Innovation. Community. Security.