Grav v1.14.0

Byte

import "std/types" (included in import "std") · namespace std.types

Indexing a string gives a byte (s[i]). Byte holds the ASCII tests and case changes for them, as methods of any byte (which also has Integer's).

grav
import "std"

fn main() {
    let s = "a1 B"
    print("${s[0].isLetter()} ${s[1].isDigit()} ${s[2].isSpace()} ${s[3].isUpper()}")
    print(s[0].toUpper().toChar() + " " + s[3].toLower().toChar() + " " + s[0].toHex())
    print("f"[0].digitValue())
}
Output
true true true true
A b 61
15

The tests are ASCII only: the bytes of other UTF-8 characters are none of them.

API

class Byte

Bytes (see the top of this file).

Static members

  • static readonly MAX: int = 255
  • static readonly MIN: int = 0
  • static fn isDigit(b: byte) -> bool — ASCII tests (false for bytes of other UTF-8 characters).
  • static fn isLetter(b: byte) -> bool
  • static fn isLetterOrDigit(b: byte) -> bool
  • static fn isUpper(b: byte) -> bool
  • static fn isLower(b: byte) -> bool
  • static fn isSpace(b: byte) -> bool — A space, tab, newline, carriage return, vertical tab or form feed.
  • static fn isHexDigit(b: byte) -> bool
  • static fn isPunctuation(b: byte) -> bool
  • static fn isAscii(b: byte) -> bool — Below 128: a whole character in itself.
  • static fn toUpper(b: byte) -> byte — The ASCII letter in the other case (other bytes as they are).
  • static fn toLower(b: byte) -> byte
  • static fn digitValue(b: byte) -> int — The digit's value: 0..9 for '0'..'9', 10..35 for letters (either case); -1 otherwise.
  • static fn compare(a: byte, b: byte) -> int — Comparators for sorting: by value; and ignoring ASCII case.
  • static fn compareIgnoreCase(a: byte, b: byte) -> int
  • static fn toChar(b: byte) -> string — The byte as a one-byte string ("A" for 65).
  • static fn toHex(b: byte) -> string — Two hex digits ("0a", "ff").

Edit this page on GitHub