mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
builtin: add input_character/0 and print_character/1 (#19502)
This commit is contained in:
parent
0904a9e892
commit
c79f84d406
43
vlib/builtin/character_inout.c.v
Normal file
43
vlib/builtin/character_inout.c.v
Normal 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
|
||||||
|
}
|
18
vlib/builtin/character_inout_test.v
Normal file
18
vlib/builtin/character_inout_test.v
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user