Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library

    std.parser.takeWhileAsciiAlpha

    Finds the end of an ASCII-letter span.

    takeWhileAsciiAlpha(text: string, start: int) -> int
    Receiver
    Not applicable
    Minimum arity
    2
    Maximum arity
    2

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.parser in language mode 5.21.

    The current compiler selects std/parser.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Parameter constraints

    Each scan uses the class named by its corresponding ASCII predicate. The starting position and result follow the zero based scalar positions.

    Clamp a negative start to zero. Clamp a start greater than the scalar length to that length. The first position examined is the clamped start.

    Inspect consecutive positions from left to right. Stop at the first nonmatching scalar or at the scalar length. Return that exclusive end position.

    If the first scalar does not match, return the clamped start. Starting at end of input returns that end. Every scan of an empty string returns zero.

    A non ASCII scalar ends the span. Scanning does not skip it, normalize it, or advance across it to find a later match.

    The text expression is evaluated before the start expression, once each. Scans do not modify the text or perform I/O. An out of range start produces a bounded result without a diagnostic.

    For a clamped start begin and returned end, text.slice(begin, end) is the matched text. The scan reports a position rather than a character count.

    Result

    Effects

    Failures

    Result and effects

    Link

    Clamp the start to the scalar bounds and return the exclusive end of the maximal matching ASCII span.

    Related entries

    Link

    std.parser

    Scan a maximal ASCII span

    Link
    Ruletopaz.stdlib.contract.parser-ascii-spans.rule

    Scan a maximal ASCII span

    Each scan uses the class named by its corresponding ASCII predicate. The starting position and result follow the zero based scalar positions.

    Clamp a negative start to zero. Clamp a start greater than the scalar length to that length. The first position examined is the clamped start.

    Inspect consecutive positions from left to right. Stop at the first nonmatching scalar or at the scalar length. Return that exclusive end position.

    If the first scalar does not match, return the clamped start. Starting at end of input returns that end. Every scan of an empty string returns zero.

    A non ASCII scalar ends the span. Scanning does not skip it, normalize it, or advance across it to find a later match.

    The text expression is evaluated before the start expression, once each. Scans do not modify the text or perform I/O. An out of range start produces a bounded result without a diagnostic.

    For a clamped start begin and returned end, text.slice(begin, end) is the matched text. The scan reports a position rather than a character count.

    Why

    Maximal spans let callers resume at the first unconsumed scalar. Clamping uses the same bounds as string slicing and gives empty input a useful result.

    Source evidence

    Find the exclusive end for every ASCII class

    Link
    main.tpzFind the exclusive end for every ASCII classOpen in Playground
    1. import std.parser
    2. import std.io
    3. let digit = parser.takeWhileAsciiDigit("123x", 0)
    4. let alpha = parser.takeWhileAsciiAlpha("abZ9", 0)
    5. let alnum = parser.takeWhileAsciiAlnum("a9Z_", 0)
    6. let space = parser.takeWhileAsciiWhitespace("\t\n\r x", 0)
    7. io.writeLine("{digit}/{alpha}/{alnum}/{space}")
    Expected outputexit 0
    3/3/3/4
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 3/3/3/4
    Source evidence

    Stop at non ASCII scalars without changing position units

    Link
    main.tpzStop at non ASCII scalars without changing position unitsOpen in Playground
    1. import std.parser
    2. import std.io
    3. let digit = parser.takeWhileAsciiDigit("é12x", 1)
    4. let combining = parser.takeWhileAsciiAlpha("éA", 0)
    5. let suffix = parser.takeWhileAsciiAlpha("éA", 2)
    6. let nonbreaking = parser.takeWhileAsciiWhitespace("  ", 0)
    7. let alnum = parser.takeWhileAsciiAlnum("Aé", 0)
    8. io.writeLine("{digit}/{combining}/{suffix}/{nonbreaking}/{alnum}")
    Expected outputexit 0
    3/1/3/0/1
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 3/1/3/0/1
    Source evidence

    Clamp starts and keep the first failing position

    Link
    main.tpzClamp starts and keep the first failing positionOpen in Playground
    1. import std.parser
    2. import std.io
    3. let text = "12"
    4. for start in ([-3, 0, 1, 2, 99]) {
    5. let digit = parser.takeWhileAsciiDigit(text, start)
    6. let alpha = parser.takeWhileAsciiAlpha(text, start)
    7. let alnum = parser.takeWhileAsciiAlnum(text, start)
    8. let space = parser.takeWhileAsciiWhitespace(text, start)
    9. io.writeLine("{digit}/{alpha}/{alnum}/{space}")
    10. }
    11. let empty = ""
    12. let digit = parser.takeWhileAsciiDigit(empty, 99)
    13. let alpha = parser.takeWhileAsciiAlpha(empty, -1)
    14. let alnum = parser.takeWhileAsciiAlnum(empty, 0)
    15. let space = parser.takeWhileAsciiWhitespace(empty, 1)
    16. io.writeLine("{digit}/{alpha}/{alnum}/{space}")
    Expected outputexit 0
    2/0/2/0
    2/0/2/0
    2/1/2/1
    2/2/2/2
    2/2/2/2
    0/0/0/0
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 2/0/2/0
    2. 2/0/2/0
    3. 2/1/2/1
    4. 2/2/2/2
    5. 2/2/2/2
    6. 0/0/0/0
    Source evidence

    Menu

    Download