#! @file u8.ext.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what a byte of text is asked: whether it is a letter, a digit of #! some base, whitespace, or one of several bytes, and its letter's other case. module core; #! The zero byte, which ends text C reads and marks the end of text a scanner #! reads. pub const nullbyte: u8 = '\0'; #! The line feed byte, `'\n'`, which ends a line. pub const newline: u8 = '\n'; #! Whether the byte is the zero byte. #! @param this The byte to check. #! @returns Whether it is `'\0'`. pub val u8::is_null: bool { get => this == nullbyte; } #! Whether the byte marks the end of text, as the zero byte does to a scanner. #! @param this The byte to check. #! @returns Whether it is `'\0'`. pub val u8::is_eof: bool { get => this == nullbyte; } #! Whether the byte ends a line. #! @param this The byte to check. #! @returns Whether it is `'\n'`. pub val u8::is_eol: bool { get => this == newline; } #! Whether the byte is whitespace. #! @param this The byte to check. #! @returns Whether it is a space, or a tab, line feed, vertical tab, form feed, #! or carriage return. @inline pub val u8::is_space: bool { get => this == ' ' || (this >= '\t' && this <= '\r'); } #! Whether the byte is an uppercase ASCII letter. #! @param this The byte to check. #! @returns Whether it is one of `A` to `Z`. @inline pub val u8::is_upper: bool { get => this >= 'A' && this <= 'Z'; } #! Whether the byte is a lowercase ASCII letter. #! @param this The byte to check. #! @returns Whether it is one of `a` to `z`. @inline pub val u8::is_lower: bool { get => this >= 'a' && this <= 'z'; } #! The byte in uppercase, where it is a lowercase ASCII letter. #! @param this The byte to convert. #! @returns The uppercase letter, or the byte as it is where it is no lowercase #! letter. @inline pub val u8::to_upper: u8 { get => this.is_lower ? this - 32 : this; } #! The byte in lowercase, where it is an uppercase ASCII letter. #! @param this The byte to convert. #! @returns The lowercase letter, or the byte as it is where it is no uppercase #! letter. @inline pub val u8::to_lower: u8 { get => this.is_upper ? this + 32 : this; } #! Whether the byte is an ASCII letter of either case. #! @param this The byte to check. #! @returns Whether it is a letter. pub val u8::is_alpha: bool { get => this.is_lower || this.is_upper; } #! Whether the byte is an ASCII letter or a decimal digit. #! @param this The byte to check. #! @returns Whether it is a letter or a digit. pub val u8::is_alnum: bool { get => this.is_alpha || this.is_digit; } #! Whether the byte may begin a name: a letter, or `_`. #! @param this The byte to check. #! @returns Whether it is a letter or `_`. pub val u8::is_name: bool { get => this.is_alpha || this == '_'; } #! Whether the byte may go on a name: a letter, a digit, or `_`. #! @param this The byte to check. #! @returns Whether it is a letter, a digit, or `_`. pub val u8::is_ident: bool { get => this.is_alnum || this == '_'; } #! Whether the byte is a decimal digit. #! @param this The byte to check. #! @returns Whether it is one of `0` to `9`. @inline pub val u8::is_digit: bool { get => this >= '0' && this <= '9'; } #! Whether the byte is a binary digit. #! @param this The byte to check. #! @returns Whether it is `0` or `1`. pub val u8::is_bin: bool { get => this == '0' || this == '1'; } #! Whether the byte is an octal digit. #! @param this The byte to check. #! @returns Whether it is one of `0` to `7`. pub val u8::is_oct: bool { get => this >= '0' && this <= '7'; } #! Whether the byte is a lowercase letter a hexadecimal digit may be. #! @param this The byte to check. #! @returns Whether it is one of `a` to `f`. pub val u8::is_hex_lower: bool { get => this >= 'a' && this <= 'f'; } #! Whether the byte is an uppercase letter a hexadecimal digit may be. #! @param this The byte to check. #! @returns Whether it is one of `A` to `F`. pub val u8::is_hex_upper: bool { get => this >= 'A' && this <= 'F'; } #! Whether the byte is a hexadecimal digit, of either case. #! @param this The byte to check. #! @returns Whether it is a decimal digit, or one of `a` to `f` or `A` to `F`. pub val u8::is_hex: bool { get => this.is_digit || this.is_hex_lower || this.is_hex_upper; } #! Gives the value of the current byte as a digit of any base up to 36, where #! the letters follow `9` in either case. #! @param this The byte to convert. #! @returns The value of the digit, or 99 when the byte is not a digit. pub val u8::digit_value: u64 { get { if this.is_digit { return this - '0'; } if this.is_lower { return this - 'a' + 10; } if this.is_upper { return this - 'A' + 10; } return 99; } } #! Checks if the current byte is a digit of the given base, of any base up to #! 36, where the letters follow `9` in either case, as `digit_value` reads them. #! @param this The byte that we are checking. #! @param base The base of the number we are checking for. #! @returns Whether the byte is a digit of the given base. pub def u8::is_base_digit(&this, base: u8): bool { return this.digit_value < base as u64; } #! Whether the byte is any of several bytes. #! @param this The byte to check. #! @param chars The bytes to look for. #! @returns Whether it is one of them. pub def u8::matches(&this, chars: u8...): bool { for value in chars { return true if this == value; } return false; }