| 1 | module builtin |
| 2 | |
| 3 | // input_rune returns a single rune from the standart input (an unicode codepoint). |
| 4 | // It expects, that the input is utf8 encoded. |
| 5 | // It will return `none` on EOF. |
| 6 | pub fn input_rune() ?rune { |
| 7 | x := input_character() |
| 8 | if x <= 0 { |
| 9 | return none |
| 10 | } |
| 11 | char_len := utf8_char_len(u8(x)) |
| 12 | if char_len == 1 { |
| 13 | return x |
| 14 | } |
| 15 | mut b := u8(x) |
| 16 | b = b << char_len |
| 17 | mut res := rune(b) |
| 18 | mut shift := 6 - char_len |
| 19 | for i := 1; i < char_len; i++ { |
| 20 | c := rune(input_character()) |
| 21 | res = rune(res) << shift |
| 22 | res |= c & 63 // 0x3f |
| 23 | shift = 6 |
| 24 | } |
| 25 | return res |
| 26 | } |
| 27 | |
| 28 | // InputRuneIterator is an iterator over the input runes. |
| 29 | pub struct InputRuneIterator {} |
| 30 | |
| 31 | // next returns the next rune from the input stream. |
| 32 | pub fn (mut self InputRuneIterator) next() ?rune { |
| 33 | return input_rune() |
| 34 | } |
| 35 | |
| 36 | // input_rune_iterator returns an iterator to allow for `for i, r in input_rune_iterator() {`. |
| 37 | // When the input stream is closed, the loop will break. |
| 38 | pub fn input_rune_iterator() InputRuneIterator { |
| 39 | return InputRuneIterator{} |
| 40 | } |
| 41 | |