#! @file text.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what reading and writing text formats share, whatever the format: #! checking and appending UTF-8, reading a number's text as an integer exactly, and #! finding where a byte of the text is by its line and column. import core::swar; module std::serial; # The most an exponent is read as, past which a number is zero or too large for # any type anyway. const exponent_limit: u64 = 100000; #! Whether text is UTF-8: no overlong forms, no surrogates, nothing past #! U+10FFFF, and nothing cut short. #! @param text The text to check. #! @returns Whether it is UTF-8. pub def is_utf8(text: strview): bool { val data := text.data; var at: u64 = 0; while at < text.length { if text[at] < 0x80 { # ASCII, which is passed a word at a time to the next byte past it. at++; while at + 8 <= text.length { val wide: swar::mask8 = swar::load(data + at) & 0x8080808080808080; if wide.any() { at += wide.first(); break; } at += 8; } continue; } val count := utf8_length(text, at); if count == 0 { return false; } at += count; } return true; } #! The length of the UTF-8 sequence beginning at a byte, or 0 where the bytes #! there are not one: overlong, a surrogate, past U+10FFFF, or cut short. #! @param text The text to read. #! @param at Where the sequence begins, which must be within the text. #! @returns How many bytes it takes, from 1 to 4, or 0. pub def utf8_length(text: strview, at: u64): u64 { val lead := text[at]; if lead < 0x80 { return 1; } # The second byte is narrowed where the lead alone would allow an overlong form, a # surrogate, or a character past U+10FFFF. var count: u64 = 0; var low: u8 = 0x80; var high: u8 = 0xBF; if lead >= 0xC2 && lead <= 0xDF { count = 2; } else if lead == 0xE0 { count = 3; low = 0xA0; } else if lead == 0xED { count = 3; high = 0x9F; } else if lead >= 0xE1 && lead <= 0xEF { count = 3; } else if lead == 0xF0 { count = 4; low = 0x90; } else if lead == 0xF4 { count = 4; high = 0x8F; } else if lead >= 0xF1 && lead <= 0xF3 { count = 4; } else { return 0; } if at + count > text.length || text[at + 1] < low || text[at + 1] > high { return 0; } for index in 2..count { val byte := text[at + index]; if byte < 0x80 || byte > 0xBF { return 0; } } return count; } #! Where a byte of the text is, by its line and its column, counting a line #! break as `\n`, `\r\n`, or a `\r` alone. #! @param text The text the byte is in. #! @param offset The byte, which is at most the text's length. #! @returns Where it is: its line counted from 1, and its column, the byte #! within the line, counted from 1. pub def place_of(text: strview, offset: u64): place { var line: u32 = 1; var begun: u64 = 0; val end := offset < text.length ? offset : text.length; for index in 0..end { val byte := text[index]; if byte == '\n' || (byte == '\r' && (index + 1 >= text.length || text[index + 1] != '\n')) { line++; begun = index + 1; } } return place { offset = offset, line = line, column = (offset - begun + 1) as u32 }; } # Where a run of decimal digits beginning at `at` ends. def digits_end(text: strview, at: u64): u64 { var index := at; while index < text.length && text[index].is_digit { index++; } return index; } #! What a number's text is as an integer: its magnitude and sign, and whether it #! is one and fits in 64 bits. pub struct integral { pub var magnitude: u64; #! The integer's magnitude, where it fits. pub var negative: bool; #! Whether it is below zero. pub var whole: bool; #! Whether it has no fraction, as `1.50` has and `1.5e1` has not. pub var fits: bool; #! Whether its magnitude fits in 64 bits. } #! Reads a decimal number's text as an integer exactly, as #! `[-+]?[0-9]*(\.[0-9]*)?([eE][-+]?[0-9]+)?` writes it, so `1.5e1` is 15. #! @param text The number's text, which follows that grammar and holds a #! digit. #! @returns Its magnitude and sign, whether it is whole, and whether it fits. pub def integral_of(text: strview): integral { var made := integral { whole = true, fits = true }; var at: u64 = 0; if text[0] == '-' || text[0] == '+' { made.negative = text[0] == '-'; at = 1; } # The digits, with where the fraction among them begins, and the exponent. val start := at; at = digits_end(text, at); val integers := at - start; var fraction := at; var fractions: u64 = 0; if at < text.length && text[at] == '.' { fraction = at + 1; at = digits_end(text, fraction); fractions = at - fraction; } var exponent: u64 = 0; var lowered: bool = false; if at < text.length { at++; if text[at] == '+' || text[at] == '-' { lowered = text[at] == '-'; at++; } while at < text.length { exponent = exponent * 10 + text[at].digit_value; if exponent > exponent_limit { exponent = exponent_limit; } at++; } } # The value is the digits times ten to the exponent, less one for each digit of # the fraction, so that many digits from the end are the fraction where it is # negative, and that many zeros follow where it is not. var kept := integers + fractions; var shift: u64 = 0; if lowered || exponent < fractions { val dropped := lowered ? exponent + fractions : fractions - exponent; if dropped > kept { made.whole = !any_but_zero(text, start, integers, fraction, fractions, 0, kept); return made; } made.whole = !any_but_zero(text, start, integers, fraction, fractions, kept - dropped, kept); kept -= dropped; } else { shift = exponent - fractions; } for index in 0..kept { val digit := digit_at(text, start, integers, fraction, index); if made.magnitude > (0xFFFFFFFFFFFFFFFFu - digit) / 10 { made.fits = false; return made; } made.magnitude = made.magnitude * 10 + digit; } if made.magnitude == 0 { return made; } for _ in 0..shift { if made.magnitude > 0xFFFFFFFFFFFFFFFFu / 10 { made.fits = false; return made; } made.magnitude *= 10; } return made; } # The digit at an index among a number's digits, those of its integer part then # those of its fraction. def digit_at(text: strview, start: u64, integers: u64, fraction: u64, index: u64): u64 { return index < integers ? text[start + index].digit_value : text[fraction + index - integers].digit_value; } # Whether any digit from `from` to `to` among a number's digits is not zero. def any_but_zero(text: strview, start: u64, integers: u64, fraction: u64, fractions: u64, from: u64, to: u64): bool { for index in from..to { if digit_at(text, start, integers, fraction, index) != 0 { return true; } } return false; } #! Appends a character as UTF-8. #! @param code The character, which must be no more than U+10FFFF. #! @param into The string to append to. pub def append_utf8(code: u64, into: &mut string): void { if code < 0x80 { into.push(code as u8); } else if code < 0x800 { into.push((0xC0 | (code >> 6)) as u8); into.push((0x80 | (code & 0x3F)) as u8); } else if code < 0x10000 { into.push((0xE0 | (code >> 12)) as u8); into.push((0x80 | ((code >> 6) & 0x3F)) as u8); into.push((0x80 | (code & 0x3F)) as u8); } else { into.push((0xF0 | (code >> 18)) as u8); into.push((0x80 | ((code >> 12) & 0x3F)) as u8); into.push((0x80 | ((code >> 6) & 0x3F)) as u8); into.push((0x80 | (code & 0x3F)) as u8); } }