From 39c376bb5bb507b6af054fec9b36488fd8a41a94 Mon Sep 17 00:00:00 2001 From: Enzo Date: Tue, 25 May 2021 13:51:48 +0200 Subject: [PATCH] builtin,gen: use operator overloading on ustring (#10197) --- vlib/builtin/string.v | 34 ++++------------------------ vlib/readline/readline_linux.c.v | 13 ++++++----- vlib/v/gen/c/cgen.v | 38 +++----------------------------- 3 files changed, 14 insertions(+), 71 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 6efab518ba..fe1f1d1ac3 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1300,41 +1300,15 @@ pub fn (s string) ustring_tmp() ustring { return res } -// eq implements the `u == a` (equal) operator. -fn (u ustring) eq(a ustring) bool { - if u.len != a.len || u.s != a.s { - return false - } - return true +fn (u ustring) == (a ustring) bool { + return u.s == a.s } -// ne implements the `u != a` (not equal) operator. -fn (u ustring) ne(a ustring) bool { - return !u.eq(a) -} - -// lt implements the `u < a` (less than) operator. -fn (u ustring) lt(a ustring) bool { +fn (u ustring) < (a ustring) bool { return u.s < a.s } -// le implements the `u <= a` (less than or equal to) operator. -fn (u ustring) le(a ustring) bool { - return u.lt(a) || u.eq(a) -} - -// gt implements the `u > a` (greater than) operator. -fn (u ustring) gt(a ustring) bool { - return !u.le(a) -} - -// ge implements the `u >= a` (greater than or equal to) operator. -fn (u ustring) ge(a ustring) bool { - return !u.lt(a) -} - -// add concatenates ustring with the string given in `s`. -pub fn (u ustring) add(a ustring) ustring { +fn (u ustring) + (a ustring) ustring { mut res := ustring{ s: u.s + a.s runes: __new_array(0, u.s.len + a.s.len, int(sizeof(int))) diff --git a/vlib/readline/readline_linux.c.v b/vlib/readline/readline_linux.c.v index df00233245..d52ac6469f 100644 --- a/vlib/readline/readline_linux.c.v +++ b/vlib/readline/readline_linux.c.v @@ -401,10 +401,11 @@ fn (mut r Readline) eof() bool { // insert_character inserts the character `c` at current cursor position. 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()) + r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() + + 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() + utf32_to_str(u32(c)).ustring() + + r.current.right(r.cursor + 1).ustring() } r.cursor++ // Refresh the line to add the new character @@ -419,7 +420,7 @@ fn (mut r Readline) delete_character() { return } r.cursor-- - r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring()) + r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring() r.refresh_line() } @@ -428,7 +429,7 @@ fn (mut r Readline) suppr_character() { if r.cursor > r.current.len { return } - r.current = r.current.left(r.cursor).ustring().add(r.current.right(r.cursor + 1).ustring()) + r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring() r.refresh_line() } @@ -436,7 +437,7 @@ fn (mut r Readline) suppr_character() { fn (mut r Readline) commit_line() bool { r.previous_lines.insert(1, r.current) a := '\n'.ustring() - r.current = r.current.add(a) + r.current += a r.cursor = r.current.len if r.is_tty { r.refresh_line() diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 34e8db6403..2ff38ef567 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3702,41 +3702,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { } else { node.right_type } - if unaliased_left == ast.ustring_type_idx { - fn_name := match node.op { - .plus { - 'ustring_add(' - } - .eq { - 'ustring_eq(' - } - .ne { - 'ustring_ne(' - } - .lt { - 'ustring_lt(' - } - .le { - 'ustring_le(' - } - .gt { - 'ustring_gt(' - } - .ge { - 'ustring_ge(' - } - else { - verror('op error for type `$left_sym.name`') - '/*node error*/' - } - } - g.write(fn_name) - g.expr(node.left) - g.write(', ') - g.expr(node.right) - g.write(')') - } else if unaliased_left == ast.string_type_idx && op_is_eq_or_ne - && node.right is ast.StringLiteral && (node.right as ast.StringLiteral).val == '' { + if unaliased_left == ast.string_type_idx && op_is_eq_or_ne && node.right is ast.StringLiteral + && (node.right as ast.StringLiteral).val == '' { // `str == ''` -> `str.len == 0` optimization g.write('(') g.expr(node.left) @@ -3845,6 +3812,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { is_v_struct := ((left_sym.name[0].is_capital() || left_sym.name.contains('.')) && left_sym.kind !in [.enum_, .function, .interface_, .sum_type] && left_sym.language != .c) || left_sym.kind == .string + || unaliased_left == ast.ustring_type is_alias := left_sym.kind == .alias is_c_alias := is_alias && (left_sym.info as ast.Alias).language == .c // Check if aliased type is a struct