builtin: add input_character/0 and print_character/1 (#19502)

This commit is contained in:
Delyan Angelov 2023-10-04 04:40:03 +03:00 committed by GitHub
parent 0904a9e892
commit c79f84d406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,43 @@
module builtin
// input_character gives back a single character, read from the standard input.
// It returns -1 on error (when the input is finished (EOF), on a broken pipe etc).
pub fn input_character() int {
mut ch := 0
$if freestanding {
// TODO
return -1
} $else $if vinix {
// TODO
return -1
} $else {
ch = C.getchar()
if ch == C.EOF {
return -1
}
}
return ch
}
// print_character writes the single character `ch` to the standard output.
// It returns -1 on error (when the output is closed, on a broken pipe, etc).
// Note: this function does not allocate memory, unlike `print(ch.ascii_str())`
// which does, and is thus cheaper to call, which is important, if you have
// to output many characters one by one. If you instead want to print entire
// strings at once, use `print(your_string)`.
pub fn print_character(ch u8) int {
$if android && !termux {
C.android_print(C.stdout, c'%.*s', 1, voidptr(&ch))
} $else $if freestanding {
bare_print(voidptr(&ch), u64(1))
} $else $if vinix {
// TODO
return 0
} $else {
x := C.putchar(ch)
if x == C.EOF {
return -1
}
}
return ch
}

View File

@ -0,0 +1,18 @@
fn test_print_character() {
assert print_character(u8(`a`)) > 0
assert print_character(u8(`A`)) > 0
println('')
assert print_character(rune(`a`)) > 0
assert print_character(rune(`A`)) > 0
println('')
assert print_character(u8(char(`a`))) > 0
assert print_character(u8(char(`A`))) > 0
println('')
assert print_character(u8(int(`a`))) > 0
assert print_character(u8(int(`A`))) > 0
println('')
}
fn test_input_character() {
// TODO: add an `expect` based test on Linux
}