builtin,gen: use operator overloading on ustring (#10197)
parent
0d25106b4d
commit
39c376bb5b
|
@ -1300,41 +1300,15 @@ pub fn (s string) ustring_tmp() ustring {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// eq implements the `u == a` (equal) operator.
|
fn (u ustring) == (a ustring) bool {
|
||||||
fn (u ustring) eq(a ustring) bool {
|
return u.s == a.s
|
||||||
if u.len != a.len || u.s != a.s {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ne implements the `u != a` (not equal) operator.
|
fn (u ustring) < (a ustring) bool {
|
||||||
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 {
|
|
||||||
return u.s < a.s
|
return u.s < a.s
|
||||||
}
|
}
|
||||||
|
|
||||||
// le implements the `u <= a` (less than or equal to) operator.
|
fn (u ustring) + (a ustring) ustring {
|
||||||
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 {
|
|
||||||
mut res := ustring{
|
mut res := ustring{
|
||||||
s: u.s + a.s
|
s: u.s + a.s
|
||||||
runes: __new_array(0, u.s.len + a.s.len, int(sizeof(int)))
|
runes: __new_array(0, u.s.len + a.s.len, int(sizeof(int)))
|
||||||
|
|
|
@ -401,10 +401,11 @@ 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().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 {
|
} else {
|
||||||
r.current = r.current.left(r.cursor).ustring().add(utf32_to_str(u32(c)).ustring()).add(r.current.right(
|
r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() +
|
||||||
r.cursor + 1).ustring())
|
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
|
||||||
|
@ -419,7 +420,7 @@ fn (mut r Readline) delete_character() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.cursor--
|
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()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,7 +429,7 @@ 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().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()
|
r.refresh_line()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,7 +437,7 @@ fn (mut r Readline) suppr_character() {
|
||||||
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()
|
a := '\n'.ustring()
|
||||||
r.current = r.current.add(a)
|
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()
|
||||||
|
|
|
@ -3702,41 +3702,8 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
||||||
} else {
|
} else {
|
||||||
node.right_type
|
node.right_type
|
||||||
}
|
}
|
||||||
if unaliased_left == ast.ustring_type_idx {
|
if unaliased_left == ast.string_type_idx && op_is_eq_or_ne && node.right is ast.StringLiteral
|
||||||
fn_name := match node.op {
|
&& (node.right as ast.StringLiteral).val == '' {
|
||||||
.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 == '' {
|
|
||||||
// `str == ''` -> `str.len == 0` optimization
|
// `str == ''` -> `str.len == 0` optimization
|
||||||
g.write('(')
|
g.write('(')
|
||||||
g.expr(node.left)
|
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('.'))
|
is_v_struct := ((left_sym.name[0].is_capital() || left_sym.name.contains('.'))
|
||||||
&& left_sym.kind !in [.enum_, .function, .interface_, .sum_type]
|
&& left_sym.kind !in [.enum_, .function, .interface_, .sum_type]
|
||||||
&& left_sym.language != .c) || left_sym.kind == .string
|
&& left_sym.language != .c) || left_sym.kind == .string
|
||||||
|
|| unaliased_left == ast.ustring_type
|
||||||
is_alias := left_sym.kind == .alias
|
is_alias := left_sym.kind == .alias
|
||||||
is_c_alias := is_alias && (left_sym.info as ast.Alias).language == .c
|
is_c_alias := is_alias && (left_sym.info as ast.Alias).language == .c
|
||||||
// Check if aliased type is a struct
|
// Check if aliased type is a struct
|
||||||
|
|
Loading…
Reference in New Issue