1
0
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:
Delyan Angelov 2021-03-22 16:45:29 +02:00
parent a53aaaf9e7
commit c76c69ec35
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
9 changed files with 129 additions and 71 deletions

View File

@ -49,7 +49,7 @@ const (
// Snooped from cmd/v/v.v, vlib/v/pref/pref.v
const (
auto_complete_commands = [
/* simple_cmd */
// simple_cmd
'fmt',
'up',
'vet',
@ -69,7 +69,7 @@ const (
'setup-freetype',
'doc',
'doctor',
/* commands */
// commands
'help',
'new',
'init',
@ -219,7 +219,8 @@ fn auto_complete(args []string) {
shell := sub_args[1]
mut setup := ''
match shell {
'bash' { setup = '
'bash' {
setup = '
_v_completions() {
local src
local limit
@ -233,15 +234,19 @@ _v_completions() {
}
complete -o nospace -F _v_completions v
' }
'fish' { setup = '
'
}
'fish' {
setup = '
function __v_completions
# Send all words up to the one before the cursor
$vexe complete fish (commandline -cop)
end
complete -f -c v -a "(__v_completions)"
' }
'zsh' { setup = '
'
}
'zsh' {
setup = '
#compdef v
_v() {
local src
@ -253,15 +258,18 @@ _v() {
fi
}
compdef _v v
' }
'powershell' { setup = '
'
}
'powershell' {
setup = '
Register-ArgumentCompleter -Native -CommandName v -ScriptBlock {
param(\$commandName, \$wordToComplete, \$cursorPosition)
$vexe complete powershell "\$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_)
}
}
' }
'
}
else {}
}
println(setup)
@ -307,11 +315,10 @@ Register-ArgumentCompleter -Native -CommandName v -ScriptBlock {
// append_separator_if_dir is a utility function.that returns the input `path` appended an
// OS dependant path separator if the `path` is a directory.
fn append_separator_if_dir(path string) string {
return if os.is_dir(path) && !path.ends_with(os.path_separator) {
path + os.path_separator
} else {
path
if os.is_dir(path) && !path.ends_with(os.path_separator) {
return path + os.path_separator
}
return path
}
// auto_complete_request retuns a list of completions resolved from a full argument list.

View File

@ -356,7 +356,10 @@ fn html_highlight(code string, tb &table.Table) string {
} else {
tok.lit
}
return if typ in [.unone, .name] { lit } else { '<span class="token $typ">$lit</span>' }
if typ in [.unone, .name] {
return lit
}
return '<span class="token $typ">$lit</span>'
}
mut s := scanner.new_scanner(code, .parse_comments, &pref.Preferences{})
mut tok := s.scan()

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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 {

View File

@ -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()

View File

@ -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')