vlib: remove ustring usage (#10618)
parent
9d02ca51d1
commit
3881e97a40
12
doc/docs.md
12
doc/docs.md
|
@ -505,6 +505,18 @@ s := '42'
|
||||||
n := s.int() // 42
|
n := s.int() // 42
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Runes
|
||||||
|
A `rune` represents a unicode character and is an alias for `u32`. Runes can be created like this:
|
||||||
|
```v
|
||||||
|
x := `🚀`
|
||||||
|
```
|
||||||
|
|
||||||
|
A string can be converted to runes by the `.runes()` method.
|
||||||
|
```v
|
||||||
|
hello := 'Hello World 👋'
|
||||||
|
hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`, ` `, `👋`]
|
||||||
|
```
|
||||||
|
|
||||||
### String interpolation
|
### String interpolation
|
||||||
|
|
||||||
Basic interpolation syntax is pretty simple - use `$` before a variable name.
|
Basic interpolation syntax is pretty simple - use `$` before a variable name.
|
||||||
|
|
|
@ -28,6 +28,15 @@ pub fn (c rune) str() string {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// string converts a rune array to a string
|
||||||
|
pub fn (ra []rune) string() string {
|
||||||
|
mut res := ''
|
||||||
|
for r in ra {
|
||||||
|
res += r.str()
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// Define this on byte as well, so that we can do `s[0].is_capital()`
|
// Define this on byte as well, so that we can do `s[0].is_capital()`
|
||||||
pub fn (c byte) is_capital() bool {
|
pub fn (c byte) is_capital() bool {
|
||||||
return c >= `A` && c <= `Z`
|
return c >= `A` && c <= `Z`
|
||||||
|
@ -42,7 +51,7 @@ pub fn (b []byte) clone() []byte {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO remove this once runes are implemented
|
// TODO: remove this once runes are implemented
|
||||||
pub fn (b []byte) bytestr() string {
|
pub fn (b []byte) bytestr() string {
|
||||||
unsafe {
|
unsafe {
|
||||||
buf := malloc_noscan(b.len + 1)
|
buf := malloc_noscan(b.len + 1)
|
||||||
|
|
|
@ -73,7 +73,8 @@ pub fn (s string) runes() []rune {
|
||||||
for i := 0; i < s.len; i++ {
|
for i := 0; i < s.len; i++ {
|
||||||
char_len := utf8_char_len(unsafe { s.str[i] })
|
char_len := utf8_char_len(unsafe { s.str[i] })
|
||||||
if char_len > 1 {
|
if char_len > 1 {
|
||||||
mut r := unsafe { s[i..i + char_len] }
|
end := if s.len - 1 >= i + char_len { i + char_len } else { s.len }
|
||||||
|
mut r := unsafe { s[i..end] }
|
||||||
runes << r.utf32_code()
|
runes << r.utf32_code()
|
||||||
i += char_len - 1
|
i += char_len - 1
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import os.notify
|
import os.notify
|
||||||
import time
|
|
||||||
|
|
||||||
// make a pipe and return the (read, write) file descriptors
|
// make a pipe and return the (read, write) file descriptors
|
||||||
fn make_pipe() ?(int, int) {
|
fn make_pipe() ?(int, int) {
|
||||||
|
|
12
vlib/os/os.v
12
vlib/os/os.v
|
@ -90,18 +90,6 @@ pub fn read_lines(path string) ?[]string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_ulines reads the file in `path` into an array of ustring lines.
|
|
||||||
fn read_ulines(path string) ?[]ustring {
|
|
||||||
lines := read_lines(path) ?
|
|
||||||
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
|
||||||
mut ulines := []ustring{}
|
|
||||||
for myline in lines {
|
|
||||||
// ulines[i] = ustr
|
|
||||||
ulines << myline.ustring()
|
|
||||||
}
|
|
||||||
return ulines
|
|
||||||
}
|
|
||||||
|
|
||||||
// sigint_to_signal_name will translate `si` signal integer code to it's string code representation.
|
// sigint_to_signal_name will translate `si` signal integer code to it's string code representation.
|
||||||
pub fn sigint_to_signal_name(si int) string {
|
pub fn sigint_to_signal_name(si int) string {
|
||||||
// POSIX signals:
|
// POSIX signals:
|
||||||
|
|
|
@ -33,13 +33,13 @@ pub struct Readline {
|
||||||
mut:
|
mut:
|
||||||
is_raw bool
|
is_raw bool
|
||||||
orig_termios Termios // Linux
|
orig_termios Termios // Linux
|
||||||
current ustring // Line being edited
|
current []rune // Line being edited
|
||||||
cursor int // Cursor position
|
cursor int // Cursor position
|
||||||
overwrite bool
|
overwrite bool
|
||||||
cursor_row_offset int
|
cursor_row_offset int
|
||||||
prompt string
|
prompt string
|
||||||
prompt_offset int
|
prompt_offset int
|
||||||
previous_lines []ustring
|
previous_lines [][]rune
|
||||||
search_index int
|
search_index int
|
||||||
is_tty bool
|
is_tty bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,58 +17,58 @@ import os
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
pub fn (mut r Readline) read_line_utf8(prompt string) ?[]rune {
|
||||||
r.current = ''.ustring()
|
r.current = []rune{}
|
||||||
r.cursor = 0
|
r.cursor = 0
|
||||||
r.prompt = prompt
|
r.prompt = prompt
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
if r.previous_lines.len <= 1 {
|
if r.previous_lines.len <= 1 {
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
} else {
|
} else {
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
}
|
}
|
||||||
print(r.prompt)
|
print(r.prompt)
|
||||||
line := os.get_raw_line()
|
line := os.get_raw_line()
|
||||||
if line.len >= 0 {
|
if line.len >= 0 {
|
||||||
r.current = line.ustring()
|
r.current = line.runes()
|
||||||
}
|
}
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
if r.current.s == '' {
|
if r.current.len == 0 {
|
||||||
return error('empty line')
|
return error('empty line')
|
||||||
}
|
}
|
||||||
return r.current
|
return r.current
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` returned by `read_line_utf8`).
|
// (As opposed to `[]rune` returned by `read_line_utf8`).
|
||||||
pub fn (mut r Readline) read_line(prompt string) ?string {
|
pub fn (mut r Readline) read_line(prompt string) ?string {
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s.s
|
return s.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
// NOTE that this version of `read_line_utf8` is a standalone function without
|
// NOTE that this version of `read_line_utf8` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line_utf8(prompt string) ?ustring {
|
pub fn read_line_utf8(prompt string) ?[]rune {
|
||||||
mut r := Readline{}
|
mut r := Readline{}
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` as returned by `read_line_utf8`).
|
// (As opposed to `[]rune` as returned by `read_line_utf8`).
|
||||||
// NOTE that this version of `read_line` is a standalone function without
|
// NOTE that this version of `read_line` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line(prompt string) ?string {
|
pub fn read_line(prompt string) ?string {
|
||||||
|
|
|
@ -101,21 +101,21 @@ pub fn (r Readline) read_char() int {
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
pub fn (mut r Readline) read_line_utf8(prompt string) ?[]rune {
|
||||||
r.current = ''.ustring()
|
r.current = []rune{}
|
||||||
r.cursor = 0
|
r.cursor = 0
|
||||||
r.prompt = prompt
|
r.prompt = prompt
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
r.prompt_offset = get_prompt_offset(prompt)
|
r.prompt_offset = get_prompt_offset(prompt)
|
||||||
if r.previous_lines.len <= 1 {
|
if r.previous_lines.len <= 1 {
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
} else {
|
} else {
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
}
|
}
|
||||||
if !r.is_raw {
|
if !r.is_raw {
|
||||||
r.enable_raw_mode()
|
r.enable_raw_mode()
|
||||||
|
@ -129,39 +129,39 @@ pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
r.disable_raw_mode()
|
r.disable_raw_mode()
|
||||||
if r.current.s == '' {
|
if r.current.len == 0 {
|
||||||
return error('empty line')
|
return error('empty line')
|
||||||
}
|
}
|
||||||
return r.current
|
return r.current
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` returned by `read_line_utf8`).
|
// (As opposed to `[]rune` returned by `read_line_utf8`).
|
||||||
pub fn (mut r Readline) read_line(prompt string) ?string {
|
pub fn (mut r Readline) read_line(prompt string) ?string {
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s.s
|
return s.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
// NOTE that this version of `read_line_utf8` is a standalone function without
|
// NOTE that this version of `read_line_utf8` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line_utf8(prompt string) ?ustring {
|
pub fn read_line_utf8(prompt string) ?[]rune {
|
||||||
mut r := Readline{}
|
mut r := Readline{}
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` as returned by `read_line_utf8`).
|
// (As opposed to `[]rune` as returned by `read_line_utf8`).
|
||||||
// NOTE that this version of `read_line` is a standalone function without
|
// NOTE that this version of `read_line` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line(prompt string) ?string {
|
pub fn read_line(prompt string) ?string {
|
||||||
|
@ -373,14 +373,14 @@ fn (mut r Readline) refresh_line() {
|
||||||
mut end_of_input := [0, 0]
|
mut end_of_input := [0, 0]
|
||||||
end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len,
|
end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len,
|
||||||
end_of_input)
|
end_of_input)
|
||||||
end_of_input[1] += r.current.count('\n'.ustring())
|
end_of_input[1] += r.current.filter(it == `\n`).len
|
||||||
mut cursor_pos := [0, 0]
|
mut cursor_pos := [0, 0]
|
||||||
cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor,
|
cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor,
|
||||||
cursor_pos)
|
cursor_pos)
|
||||||
shift_cursor(0, -r.cursor_row_offset)
|
shift_cursor(0, -r.cursor_row_offset)
|
||||||
term.erase_toend()
|
term.erase_toend()
|
||||||
print(r.prompt)
|
print(r.prompt)
|
||||||
print(r.current)
|
print(r.current.string())
|
||||||
if end_of_input[0] == 0 && end_of_input[1] > 0 {
|
if end_of_input[0] == 0 && end_of_input[1] > 0 {
|
||||||
print('\n')
|
print('\n')
|
||||||
}
|
}
|
||||||
|
@ -401,11 +401,9 @@ fn (mut r Readline) eof() bool {
|
||||||
// insert_character inserts the character `c` at current cursor position.
|
// insert_character inserts the character `c` at current cursor position.
|
||||||
fn (mut r Readline) insert_character(c int) {
|
fn (mut r Readline) insert_character(c int) {
|
||||||
if !r.overwrite || r.cursor == r.current.len {
|
if !r.overwrite || r.cursor == r.current.len {
|
||||||
r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() +
|
r.current.insert(r.cursor, c)
|
||||||
r.current.right(r.cursor).ustring()
|
|
||||||
} else {
|
} else {
|
||||||
r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() +
|
r.current[r.cursor] = rune(c)
|
||||||
r.current.right(r.cursor + 1).ustring()
|
|
||||||
}
|
}
|
||||||
r.cursor++
|
r.cursor++
|
||||||
// Refresh the line to add the new character
|
// Refresh the line to add the new character
|
||||||
|
@ -420,7 +418,7 @@ fn (mut r Readline) delete_character() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.cursor--
|
r.cursor--
|
||||||
r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring()
|
r.current.delete(r.cursor)
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -429,15 +427,14 @@ fn (mut r Readline) suppr_character() {
|
||||||
if r.cursor > r.current.len {
|
if r.cursor > r.current.len {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring()
|
r.current.delete(r.cursor)
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
|
||||||
// commit_line adds a line break and then stops the main loop.
|
// commit_line adds a line break and then stops the main loop.
|
||||||
fn (mut r Readline) commit_line() bool {
|
fn (mut r Readline) commit_line() bool {
|
||||||
r.previous_lines.insert(1, r.current)
|
r.previous_lines.insert(1, r.current)
|
||||||
a := '\n'.ustring()
|
r.current << `\n`
|
||||||
r.current += a
|
|
||||||
r.cursor = r.current.len
|
r.cursor = r.current.len
|
||||||
if r.is_tty {
|
if r.is_tty {
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
|
@ -483,9 +480,9 @@ fn (r Readline) is_break_character(c string) bool {
|
||||||
// move_cursor_word_left moves the cursor relative one word length worth to the left.
|
// move_cursor_word_left moves the cursor relative one word length worth to the left.
|
||||||
fn (mut r Readline) move_cursor_word_left() {
|
fn (mut r Readline) move_cursor_word_left() {
|
||||||
if r.cursor > 0 {
|
if r.cursor > 0 {
|
||||||
for ; r.cursor > 0 && r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {
|
for ; r.cursor > 0 && r.is_break_character(r.current[r.cursor - 1].str()); r.cursor-- {
|
||||||
}
|
}
|
||||||
for ; r.cursor > 0 && !r.is_break_character(r.current.at(r.cursor - 1)); r.cursor-- {
|
for ; r.cursor > 0 && !r.is_break_character(r.current[r.cursor - 1].str()); r.cursor-- {
|
||||||
}
|
}
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
@ -494,9 +491,9 @@ fn (mut r Readline) move_cursor_word_left() {
|
||||||
// move_cursor_word_right moves the cursor relative one word length worth to the right.
|
// move_cursor_word_right moves the cursor relative one word length worth to the right.
|
||||||
fn (mut r Readline) move_cursor_word_right() {
|
fn (mut r Readline) move_cursor_word_right() {
|
||||||
if r.cursor < r.current.len {
|
if r.cursor < r.current.len {
|
||||||
for ; r.cursor < r.current.len && r.is_break_character(r.current.at(r.cursor)); r.cursor++ {
|
for ; r.cursor < r.current.len && r.is_break_character(r.current[r.cursor].str()); r.cursor++ {
|
||||||
}
|
}
|
||||||
for ; r.cursor < r.current.len && !r.is_break_character(r.current.at(r.cursor)); r.cursor++ {
|
for ; r.cursor < r.current.len && !r.is_break_character(r.current[r.cursor].str()); r.cursor++ {
|
||||||
}
|
}
|
||||||
r.refresh_line()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,55 +16,55 @@ import os
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
pub fn (mut r Readline) read_line_utf8(prompt string) ?[]rune {
|
||||||
r.current = ''.ustring()
|
r.current = []rune{}
|
||||||
r.cursor = 0
|
r.cursor = 0
|
||||||
r.prompt = prompt
|
r.prompt = prompt
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
if r.previous_lines.len <= 1 {
|
if r.previous_lines.len <= 1 {
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
r.previous_lines << ''.ustring()
|
r.previous_lines << []rune{}
|
||||||
} else {
|
} else {
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
}
|
}
|
||||||
print(r.prompt)
|
print(r.prompt)
|
||||||
r.current = os.get_raw_line().ustring()
|
r.current = os.get_raw_line().runes()
|
||||||
r.previous_lines[0] = ''.ustring()
|
r.previous_lines[0] = []rune{}
|
||||||
r.search_index = 0
|
r.search_index = 0
|
||||||
if r.current.s == '' {
|
if r.current.len == 0 {
|
||||||
return error('empty line')
|
return error('empty line')
|
||||||
}
|
}
|
||||||
return r.current
|
return r.current
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` returned by `read_line_utf8`).
|
// (As opposed to `[]rune` returned by `read_line_utf8`).
|
||||||
pub fn (mut r Readline) read_line(prompt string) ?string {
|
pub fn (mut r Readline) read_line(prompt string) ?string {
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s.s
|
return s.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line_utf8 blocks execution in a loop and awaits user input
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
||||||
// characters from a terminal until `EOF` or `Enter` key is encountered
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
||||||
// in the input stream.
|
// in the input stream.
|
||||||
// read_line_utf8 returns the complete input line as an UTF-8 encoded `ustring` or
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
||||||
// an error if the line is empty.
|
// an error if the line is empty.
|
||||||
// The `prompt` `string` is output as a prefix text for the input capturing.
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
||||||
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
||||||
// NOTE that this version of `read_line_utf8` is a standalone function without
|
// NOTE that this version of `read_line_utf8` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line_utf8(prompt string) ?ustring {
|
pub fn read_line_utf8(prompt string) ?[]rune {
|
||||||
mut r := Readline{}
|
mut r := Readline{}
|
||||||
s := r.read_line_utf8(prompt) ?
|
s := r.read_line_utf8(prompt) ?
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
||||||
// (As opposed to `ustring` as returned by `read_line_utf8`).
|
// (As opposed to `[]rune` as returned by `read_line_utf8`).
|
||||||
// NOTE that this version of `read_line` is a standalone function without
|
// NOTE that this version of `read_line` is a standalone function without
|
||||||
// persistent functionalities (e.g. history).
|
// persistent functionalities (e.g. history).
|
||||||
pub fn read_line(prompt string) ?string {
|
pub fn read_line(prompt string) ?string {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
struct User {
|
struct User {
|
||||||
name string // name
|
name string // name
|
||||||
name2 ustring // name2
|
name2 []rune // name2
|
||||||
very_long_field bool
|
very_long_field bool
|
||||||
age int // age
|
age int // age
|
||||||
very_long_type_field1 very_looooog_type // long
|
very_long_type_field1 very_looooog_type // long
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
struct User {
|
struct User {
|
||||||
name string // name
|
name string // name
|
||||||
name2 ustring // name2
|
name2 []rune // name2
|
||||||
very_long_field bool
|
very_long_field bool
|
||||||
age int // age
|
age int // age
|
||||||
very_long_type_field1 very_looooog_type // long
|
very_long_type_field1 very_looooog_type // long
|
||||||
|
|
|
@ -3149,7 +3149,7 @@ fn (mut g Gen) expr(node ast.Expr) {
|
||||||
} else {
|
} else {
|
||||||
// TODO: optimize use L-char instead of u32 when possible
|
// TODO: optimize use L-char instead of u32 when possible
|
||||||
if utf8_str_len(node.val) < node.val.len {
|
if utf8_str_len(node.val) < node.val.len {
|
||||||
g.write('((u32)0x$node.val.utf32_code().hex())')
|
g.write('((rune)0x$node.val.utf32_code().hex() /* `$node.val` */)')
|
||||||
} else {
|
} else {
|
||||||
g.write("'$node.val'")
|
g.write("'$node.val'")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1270,7 +1270,7 @@ fn (mut s Scanner) ident_char() string {
|
||||||
len--
|
len--
|
||||||
c := s.text[start + 1..s.pos]
|
c := s.text[start + 1..s.pos]
|
||||||
if len != 1 {
|
if len != 1 {
|
||||||
u := c.ustring()
|
u := c.runes()
|
||||||
if u.len != 1 {
|
if u.len != 1 {
|
||||||
s.error('invalid character literal (more than one character)\n' +
|
s.error('invalid character literal (more than one character)\n' +
|
||||||
'use quotes for strings, backticks for characters')
|
'use quotes for strings, backticks for characters')
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
type Str = string | ustring
|
type Str = rune | string
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
v int
|
v int
|
||||||
|
@ -13,8 +13,8 @@ type FooBar = Bar | Foo
|
||||||
fn test_sumtype_equality() {
|
fn test_sumtype_equality() {
|
||||||
s1 := Str('s')
|
s1 := Str('s')
|
||||||
s2 := Str('s2')
|
s2 := Str('s2')
|
||||||
u1 := Str('s1'.ustring())
|
u1 := Str(`A`)
|
||||||
u2 := Str('s2'.ustring())
|
u2 := Str(`B`)
|
||||||
assert s1 == s1
|
assert s1 == s1
|
||||||
assert u1 == u1
|
assert u1 == u1
|
||||||
assert s1 != s2
|
assert s1 != s2
|
||||||
|
|
Loading…
Reference in New Issue