Home / Docs / Standard library / Prelude
find
A function of the prelude, in scope in every module. Edition 0.2 and later.
pub find(t: Text, text: Text) -> Option[Int]The position of the first occurrence of text in t, Some(0) for an empty text; it is Some exactly when contains_text(t, part: text) (spec/22 §2 rule 2).
Parameters
t: the texttext: the text to look for
Returns
Option[Int]: Some of the scalar position, from 0, of the first occurrence of text in t, Some(0) for an empty text, and None when it does not occur; when it is Some(p), 0 <= p and p + text.len <= t.len, which the verifier assumes after each call.
Use instead
contains_textwhen only whether it occurs matters
See also
Cost
Time O(b + m), plus O(1) for the scalar index from the checkpoints (ADR 0070), where:
b: the length oftin UTF-8 bytesm: the length oftextin UTF-8 bytes- index: on a text that is not all ASCII, the byte offset of every 64th scalar value, its checkpoints: built in O(b) by the first call that needs a position and kept with the value, so at most once per value; an ASCII text needs none (spec/22 §6)
Example
module shop
--- Where the range ends in an SKU. A position counts scalar values, so `É` counts as one.
dash(sku: Text) -> Text
=
when sku.find(text: "-")
is Some(at) -> "{sku}: the range ends at {at}"
is None -> "{sku}: no range"
pub main() -> Unit
uses @console: Console[print]
=
@console.print(dash("LAMP-01"))
@console.print(dash("CAFÉ-01"))
@console.print(dash("GIFTCARD"))una run . prints:
LAMP-01: the range ends at 4
CAFÉ-01: the range ends at 4
GIFTCARD: no rangeSpecification
Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.