Home / Docs / Standard library / Prelude

find

Release v0.4.0Edition 0.2 and later

A function of the prelude, in scope in every module. Edition 0.2 and later.

una
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

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

See also

slice

Cost

Time O(b + m), plus O(1) for the scalar index from the checkpoints (ADR 0070), where:

Example

src/shop.unauna
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:

Output
LAMP-01: the range ends at 4
CAFÉ-01: the range ends at 4
GIFTCARD: no range

Specification

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.