← core

core/ascii.kur

//! ASCII character classes and case conversion.
// core::ascii -- ASCII character classes and case, and the text operations
// built on them: measuring the whitespace around a byte view (which
// `Str`'s `trim` methods cut off) and splitting text into lines or words.
// The bytes are read through whatever capability permits it (`Exclusive!`,
// `Shared!`, `StaticRead`), and every line or word is a part of the text.
use package as core;

use core::option::Option;
use core::slice::{ReadBytes, byte_or_zero};
use core::str::Str;

/// Whether `b` is an ASCII uppercase letter.
fn is_upper(b: u8) -> bool {
    b >= b'A' && b <= b'Z'
}

/// Whether `b` is an ASCII lowercase letter.
fn is_lower(b: u8) -> bool {
    b >= b'a' && b <= b'z'
}

/// Whether `b` is an ASCII letter.
fn is_alpha(b: u8) -> bool {
    is_upper(b) || is_lower(b)
}

/// Whether `b` is an ASCII decimal digit.
fn is_digit(b: u8) -> bool {
    b >= b'0' && b <= b'9'
}

/// Whether `b` is an ASCII hexadecimal digit, in either case.
fn is_hex_digit(b: u8) -> bool {
    is_digit(b) || (b >= b'a' && b <= b'f') || (b >= b'A' && b <= b'F')
}

/// Whether `b` is an ASCII letter or digit.
fn is_alnum(b: u8) -> bool {
    is_alpha(b) || is_digit(b)
}

/// Whether `b` is ASCII whitespace: space, tab, newline, vertical tab, form
/// feed or carriage return.
fn is_space(b: u8) -> bool {
    b == b' ' || (b >= b'\t' && b <= b'\r')
}

/// `b` in lowercase when it is an ASCII uppercase letter, else `b` itself.
fn to_lower(b: u8) -> u8 {
    if is_upper(b) { b + 32u8 } else { b }
}

/// `b` in uppercase when it is an ASCII lowercase letter, else `b` itself.
fn to_upper(b: u8) -> u8 {
    if is_lower(b) { b - 32u8 } else { b }
}

/// How many whitespace bytes (`is_space`) `bytes` starts with.
forall C, S where ReadBytes[C, S], Copy[S].
fn space_prefix(&cx: C, bytes: S) -> usize {
    let n = ReadBytes[C, S]::len(&cx, bytes);
    var start = 0usize;
    while start < n && is_space(byte_or_zero(&cx, bytes, start)) {
        start = start + 1usize;
    }
    start
}

/// How many whitespace bytes `bytes` ends with: all of them when it is
/// whitespace throughout.
forall C, S where ReadBytes[C, S], Copy[S].
fn space_suffix(&cx: C, bytes: S) -> usize {
    let n = ReadBytes[C, S]::len(&cx, bytes);
    var end = n;
    while end > 0usize && is_space(byte_or_zero(&cx, bytes, end - 1usize)) {
        end = end - 1usize;
    }
    n - end
}

/// The line starting at `start`, without its '\n', and where the next line
/// starts; nothing once `start` is past the end. A final line need not end
/// with '\n'.
forall C, 'k where ReadBytes[C, Str['k]].
fn line_at(&cx: C, bytes: Str['k], start: usize) -> Option[(Str['k], usize)] {
    let n = bytes.len();
    if start >= n {
        return Option::None;
    }
    var end = start;
    var scanning = true;
    while scanning && end < n {
        let b = byte_or_zero(&cx, bytes, end);
        if b == b'\n' {
            scanning = false;
        } else {
            end = end + 1usize;
        }
    }
    let line = match bytes.sub(start, end - start) {
        Some(view) => view,
        None => bytes,
    };
    Option::Some.((line, end + 1usize))
}

/// The next whitespace-separated word at or after `start`, and where the
/// search for the one after it starts; nothing when only whitespace is left.
forall C, 'k where ReadBytes[C, Str['k]].
fn word_at(&cx: C, bytes: Str['k], start: usize) -> Option[(Str['k], usize)] {
    let n = bytes.len();
    var begin = start;
    var scanning = true;
    while scanning && begin < n {
        let b = byte_or_zero(&cx, bytes, begin);
        if is_space(b) {
            begin = begin + 1usize;
        } else {
            scanning = false;
        }
    }
    if begin >= n {
        return Option::None;
    }
    var end = begin;
    scanning = true;
    while scanning && end < n {
        let b = byte_or_zero(&cx, bytes, end);
        if is_space(b) {
            scanning = false;
        } else {
            end = end + 1usize;
        }
    }
    let word = match bytes.sub(begin, end - begin) {
        Some(view) => view,
        None => bytes,
    };
    Option::Some.((word, end))
}