mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: simplify return if ... constructs to make more code compatible with -autofree
This commit is contained in:
@@ -982,10 +982,15 @@ pub fn (s string) capitalize() string {
|
||||
if s.len == 0 {
|
||||
return ''
|
||||
}
|
||||
return s[0].ascii_str().to_upper() + s[1..]
|
||||
// sl := s.to_lower()
|
||||
// cap := sl[0].str().to_upper() + sl[1..]
|
||||
// return cap
|
||||
s0 := s[0]
|
||||
letter := s0.ascii_str()
|
||||
uletter := letter.to_upper()
|
||||
if s.len == 1 {
|
||||
return uletter
|
||||
}
|
||||
srest := s[1..]
|
||||
res := uletter + srest
|
||||
return res
|
||||
}
|
||||
|
||||
// is_capital returns `true` if the first character in the string is a capital letter.
|
||||
@@ -1042,16 +1047,6 @@ pub fn (s string) find_between(start string, end string) string {
|
||||
return val[..end_pos]
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn (a []string) to_c() voidptr {
|
||||
mut res := malloc(sizeof(byteptr) * a.len)
|
||||
for i in 0..a.len {
|
||||
val := a[i]
|
||||
res[i] = val.str
|
||||
}
|
||||
return res
|
||||
}
|
||||
*/
|
||||
// is_space returns `true` if the byte is a white space character.
|
||||
// The following list is considered white space characters: ` `, `\n`, `\t`, `\v`, `\f`, `\r`, 0x85, 0xa0
|
||||
// Example: assert byte(` `).is_space() == true
|
||||
@@ -1141,7 +1136,10 @@ pub fn (s string) trim_right(cutset string) string {
|
||||
}
|
||||
pos--
|
||||
}
|
||||
return if pos < 0 { '' } else { s[..pos + 1] }
|
||||
if pos < 0 {
|
||||
return ''
|
||||
}
|
||||
return s[..pos + 1]
|
||||
}
|
||||
|
||||
// trim_prefix strips `str` from the start of the string.
|
||||
|
||||
@@ -12,6 +12,7 @@ import os
|
||||
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
fn C.tcgetattr(fd int, termios_p &Termios) int
|
||||
|
||||
fn C.tcsetattr(fd int, optional_actions int, termios_p &Termios) int
|
||||
@@ -172,19 +173,36 @@ pub fn read_line(prompt string) ?string {
|
||||
// analyse returns an `Action` based on the type of input byte given in `c`.
|
||||
fn (r Readline) analyse(c int) Action {
|
||||
match byte(c) {
|
||||
`\0`, 0x3, 0x4, 255 { return .eof } // NUL, End of Text, End of Transmission
|
||||
`\n`, `\r` { return .commit_line }
|
||||
`\f` { return .clear_screen } // CTRL + L
|
||||
`\b`, 127 { return .delete_left } // BS, DEL
|
||||
27 { return r.analyse_control() } // ESC
|
||||
1 { return .move_cursor_begining } // ^A
|
||||
5 { return .move_cursor_end } // ^E
|
||||
26 { return .suspend } // CTRL + Z, SUB
|
||||
else { return if c >= ` ` {
|
||||
Action.insert_character
|
||||
} else {
|
||||
Action.nothing
|
||||
} }
|
||||
`\0`, 0x3, 0x4, 255 {
|
||||
return .eof
|
||||
} // NUL, End of Text, End of Transmission
|
||||
`\n`, `\r` {
|
||||
return .commit_line
|
||||
}
|
||||
`\f` {
|
||||
return .clear_screen
|
||||
} // CTRL + L
|
||||
`\b`, 127 {
|
||||
return .delete_left
|
||||
} // BS, DEL
|
||||
27 {
|
||||
return r.analyse_control()
|
||||
} // ESC
|
||||
1 {
|
||||
return .move_cursor_begining
|
||||
} // ^A
|
||||
5 {
|
||||
return .move_cursor_end
|
||||
} // ^E
|
||||
26 {
|
||||
return .suspend
|
||||
} // CTRL + Z, SUB
|
||||
else {
|
||||
if c >= ` ` {
|
||||
return Action.insert_character
|
||||
}
|
||||
return Action.nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,8 +336,11 @@ fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count
|
||||
out[0] = x
|
||||
out[1] = y
|
||||
for chars_remaining := char_count; chars_remaining > 0; {
|
||||
chars_this_row := if (x + chars_remaining) < screen_columns { chars_remaining } else { screen_columns -
|
||||
x }
|
||||
chars_this_row := if (x + chars_remaining) < screen_columns {
|
||||
chars_remaining
|
||||
} else {
|
||||
screen_columns - x
|
||||
}
|
||||
out[0] = x + chars_this_row
|
||||
out[1] = y
|
||||
chars_remaining -= chars_this_row
|
||||
@@ -382,8 +403,8 @@ fn (mut r Readline) insert_character(c int) {
|
||||
if !r.overwrite || r.cursor == r.current.len {
|
||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(r.cursor).ustring())
|
||||
} else {
|
||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(r.cursor +
|
||||
1).ustring())
|
||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(
|
||||
r.cursor + 1).ustring())
|
||||
}
|
||||
r.cursor++
|
||||
// Refresh the line to add the new character
|
||||
@@ -445,6 +466,7 @@ fn (mut r Readline) move_cursor_begining() {
|
||||
r.cursor = 0
|
||||
r.refresh_line()
|
||||
}
|
||||
|
||||
// move_cursor_end moves the cursor to the end of the current line.
|
||||
fn (mut r Readline) move_cursor_end() {
|
||||
r.cursor = r.current.len
|
||||
|
||||
@@ -29,19 +29,28 @@ pub fn can_show_color_on_stderr() bool {
|
||||
// ok_message returns a colored string with green color.
|
||||
// If colors are not allowed, returns a given string.
|
||||
pub fn ok_message(s string) string {
|
||||
return if can_show_color_on_stdout() { green(' $s ') } else { s }
|
||||
if can_show_color_on_stdout() {
|
||||
return green(' $s ')
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// fail_message returns a colored string with red color.
|
||||
// If colors are not allowed, returns a given string.
|
||||
pub fn fail_message(s string) string {
|
||||
return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
|
||||
if can_show_color_on_stdout() {
|
||||
return inverse(bg_white(bold(red(' $s '))))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// warn_message returns a colored string with yellow color.
|
||||
// If colors are not allowed, returns a given string.
|
||||
pub fn warn_message(s string) string {
|
||||
return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
|
||||
if can_show_color_on_stdout() {
|
||||
return bright_yellow(' $s ')
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// colorize returns a colored string by running the specified `cfn` over
|
||||
@@ -59,10 +68,11 @@ pub fn colorize(cfn fn (string) string, s string) string {
|
||||
// If an empty string is passed in, print enough spaces to make a new line
|
||||
pub fn h_divider(divider string) string {
|
||||
cols, _ := get_terminal_size()
|
||||
result := if divider.len > 0 {
|
||||
divider.repeat(1 + (cols / divider.len))
|
||||
mut result := ''
|
||||
if divider.len > 0 {
|
||||
result = divider.repeat(1 + (cols / divider.len))
|
||||
} else {
|
||||
' '.repeat(1 + cols)
|
||||
result = ' '.repeat(1 + cols)
|
||||
}
|
||||
return result[0..cols]
|
||||
}
|
||||
@@ -83,10 +93,11 @@ pub fn header(text string, divider string) string {
|
||||
})
|
||||
tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
|
||||
tstart := imax(0, (cols - tlimit_alligned) / 2)
|
||||
ln := if divider.len > 0 {
|
||||
divider.repeat(1 + cols / divider.len)[0..cols]
|
||||
mut ln := ''
|
||||
if divider.len > 0 {
|
||||
ln = divider.repeat(1 + cols / divider.len)[0..cols]
|
||||
} else {
|
||||
' '.repeat(1 + cols)
|
||||
ln = ' '.repeat(1 + cols)
|
||||
}
|
||||
if ln.len == 1 {
|
||||
return ln + ' ' + text[0..tlimit] + ' ' + ln
|
||||
|
||||
@@ -1262,7 +1262,10 @@ fn (mut s Scanner) ident_char() string {
|
||||
}
|
||||
}
|
||||
// Escapes a `'` character
|
||||
return if c == "'" { '\\' + c } else { c }
|
||||
if c == "'" {
|
||||
return '\\' + c
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
[inline]
|
||||
|
||||
@@ -705,11 +705,10 @@ pub fn (mut t Table) find_or_register_array(elem_type Type) int {
|
||||
}
|
||||
|
||||
pub fn (mut t Table) find_or_register_array_with_dims(elem_type Type, nr_dims int) int {
|
||||
return if nr_dims == 1 {
|
||||
t.find_or_register_array(elem_type)
|
||||
} else {
|
||||
t.find_or_register_array(t.find_or_register_array_with_dims(elem_type, nr_dims - 1))
|
||||
if nr_dims == 1 {
|
||||
return t.find_or_register_array(elem_type)
|
||||
}
|
||||
return t.find_or_register_array(t.find_or_register_array_with_dims(elem_type, nr_dims - 1))
|
||||
}
|
||||
|
||||
pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int) int {
|
||||
|
||||
@@ -98,18 +98,27 @@ fn (mut p Parser) decode_value() ?Any {
|
||||
kind := p.tok.kind
|
||||
p.next_with_err() ?
|
||||
if p.convert_type {
|
||||
return if kind == .float { Any(tl.f64()) } else { Any(tl.i64()) }
|
||||
if kind == .float {
|
||||
return Any(tl.f64())
|
||||
}
|
||||
return Any(tl.i64())
|
||||
}
|
||||
return Any(tl)
|
||||
}
|
||||
.bool_ {
|
||||
lit := p.tok.lit.bytestr()
|
||||
p.next_with_err() ?
|
||||
return if p.convert_type { Any(lit.bool()) } else { Any(lit) }
|
||||
if p.convert_type {
|
||||
return Any(lit.bool())
|
||||
}
|
||||
return Any(lit)
|
||||
}
|
||||
.null {
|
||||
p.next_with_err() ?
|
||||
return if p.convert_type { Any(null) } else { Any('null') }
|
||||
if p.convert_type {
|
||||
return Any(null)
|
||||
}
|
||||
return Any('null')
|
||||
}
|
||||
.str_ {
|
||||
str := p.tok.lit.bytestr()
|
||||
|
||||
@@ -75,11 +75,17 @@ pub fn (f Any) json_str() string {
|
||||
}
|
||||
f32 {
|
||||
str_f32 := f.str()
|
||||
return if str_f32.ends_with('.') { '${str_f32}0' } else { str_f32 }
|
||||
if str_f32.ends_with('.') {
|
||||
return '${str_f32}0'
|
||||
}
|
||||
return str_f32
|
||||
}
|
||||
f64 {
|
||||
str_f64 := f.str()
|
||||
return if str_f64.ends_with('.') { '${str_f64}0' } else { str_f64 }
|
||||
if str_f64.ends_with('.') {
|
||||
return '${str_f64}0'
|
||||
}
|
||||
return str_f64
|
||||
}
|
||||
bool {
|
||||
return f.str()
|
||||
@@ -135,10 +141,10 @@ fn json_string(s string) string {
|
||||
for char_len in char_lens {
|
||||
if char_len == 1 {
|
||||
chr := s[i]
|
||||
if chr in json2.important_escapable_chars {
|
||||
for j := 0 ; j < json2.important_escapable_chars.len; j++ {
|
||||
if chr == json2.important_escapable_chars[j] {
|
||||
sb.write_string(escaped_chars[j])
|
||||
if chr in important_escapable_chars {
|
||||
for j := 0; j < important_escapable_chars.len; j++ {
|
||||
if chr == important_escapable_chars[j] {
|
||||
sb.write_string(json2.escaped_chars[j])
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -148,7 +154,7 @@ fn json_string(s string) string {
|
||||
sb.write_b(chr)
|
||||
}
|
||||
} else {
|
||||
slice := s[i .. i + char_len]
|
||||
slice := s[i..i + char_len]
|
||||
hex_code := slice.utf32_code().hex()
|
||||
if hex_code.len == 4 {
|
||||
sb.write_string('\\u$hex_code')
|
||||
|
||||
Reference in New Issue
Block a user