Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

/development/std/char/scalar.nct

scalar.nct

//! Unicode scalar construction, observation, and ordering.

see ./index.nct

use /internal/character
use /internal/ptr

construct char {
    noalloc func from_u32(value: u32): Self? {
        if value > 1114111 { return none }
        if value >= 55296 && value <= 57343 { return none }
        return character.char_from_u32_unchecked(value)
    }
}

instance char {
    noalloc method self.code_point(): u32 {
        return character.char_code_point_raw(self)
    }

    noalloc method self.utf8_len(): usize {
        let value = self.code_point()
        if value <= 127 { return 1 }
        if value <= 2047 { return 2 }
        if value <= 65535 { return 3 }
        return 4
    }

    noalloc method self.is_ascii(): bool {
        return self.code_point() <= 127
    }

    noalloc method self.is_ascii_digit(): bool {
        let value = self.code_point()
        return value >= 48 && value <= 57
    }

    noalloc operator (&self == other: &Self): bool {
        return character.char_code_point_raw(ptr.copy_value(self))
            == character.char_code_point_raw(ptr.copy_value(other))
    }

    noalloc operator (&self < other: &Self): bool {
        return character.char_code_point_raw(ptr.copy_value(self))
            < character.char_code_point_raw(ptr.copy_value(other))
    }
}