1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: use operator overloading on strings (p. 2) (#10183)

This commit is contained in:
Enzo 2021-05-24 10:38:31 +02:00 committed by GitHub
parent 40f11b265e
commit 886f69bfcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 195 additions and 224 deletions

View File

@ -516,6 +516,10 @@ fn (s string) eq(a string) bool {
} }
} }
fn (s string) == (a string) bool {
return s.eq(a)
}
// ne implements the `s != a` (not equal) operator. // ne implements the `s != a` (not equal) operator.
fn (s string) ne(a string) bool { fn (s string) ne(a string) bool {
return !s.eq(a) return !s.eq(a)
@ -536,6 +540,10 @@ fn (s string) lt(a string) bool {
return false return false
} }
fn (s string) < (a string) bool {
return s.lt(a)
}
// le implements the `s <= a` (less than or equal to) operator. // le implements the `s <= a` (less than or equal to) operator.
fn (s string) le(a string) bool { fn (s string) le(a string) bool {
return s.lt(a) || s.eq(a) return s.lt(a) || s.eq(a)
@ -551,9 +559,8 @@ fn (s string) ge(a string) bool {
return !s.lt(a) return !s.lt(a)
} }
// TODO `fn (s string) + (a string)` ? To be consistent with operator overloading syntax.
// add concatenates string with the string given in `s`. // add concatenates string with the string given in `s`.
pub fn (s string) add(a string) string { fn (s string) add(a string) string {
new_len := a.len + s.len new_len := a.len + s.len
mut res := string{ mut res := string{
str: unsafe { malloc(new_len + 1) } str: unsafe { malloc(new_len + 1) }
@ -575,6 +582,10 @@ pub fn (s string) add(a string) string {
return res return res
} }
fn (s string) + (a string) string {
return s.add(a)
}
// split splits the string to an array by `delim`. // split splits the string to an array by `delim`.
// Example: assert 'A B C'.split(' ') == ['A','B','C'] // Example: assert 'A B C'.split(' ') == ['A','B','C']
// If `delim` is empty the string is split by it's characters. // If `delim` is empty the string is split by it's characters.

View File

@ -169,11 +169,11 @@ fn test_complex_mulinv() {
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
println(c2.str()) println(c2.str())
println(result.str()) println(result.str())
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.12, -0.16) c2 = cmplx.complex(-0.12, -0.16)
result = c1.mulinv() result = c1.mulinv()
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.2, 0.4) c2 = cmplx.complex(-0.2, 0.4)
result = c1.mulinv() result = c1.mulinv()
@ -201,17 +201,17 @@ fn test_complex_pow() {
mut c2 := cmplx.complex(-24.0, 70.0) mut c2 := cmplx.complex(-24.0, 70.0)
mut result := c1.pow(2) mut result := c1.pow(2)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(117, 44) c2 = cmplx.complex(117, 44)
result = c1.pow(3) result = c1.pow(3)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-7, -24) c2 = cmplx.complex(-7, -24)
result = c1.pow(4) result = c1.pow(4)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_root() { fn test_complex_root() {
@ -220,17 +220,17 @@ fn test_complex_root() {
mut c2 := cmplx.complex(2.607904, 1.342074) mut c2 := cmplx.complex(2.607904, 1.342074)
mut result := c1.root(2) mut result := c1.root(2)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(1.264953, 1.150614) c2 = cmplx.complex(1.264953, 1.150614)
result = c1.root(3) result = c1.root(3)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(1.068059, -0.595482) c2 = cmplx.complex(1.068059, -0.595482)
result = c1.root(4) result = c1.root(4)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_exp() { fn test_complex_exp() {
@ -239,17 +239,17 @@ fn test_complex_exp() {
mut c2 := cmplx.complex(111.889015, 97.505457) mut c2 := cmplx.complex(111.889015, 97.505457)
mut result := c1.exp() mut result := c1.exp()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.032543, -0.037679) c2 = cmplx.complex(-0.032543, -0.037679)
result = c1.exp() result = c1.exp()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.153092, -0.334512) c2 = cmplx.complex(-0.153092, -0.334512)
result = c1.exp() result = c1.exp()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_ln() { fn test_complex_ln() {
@ -258,17 +258,17 @@ fn test_complex_ln() {
mut c2 := cmplx.complex(2.152033, 0.950547) mut c2 := cmplx.complex(2.152033, 0.950547)
mut result := c1.ln() mut result := c1.ln()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(1.609438, 2.214297) c2 = cmplx.complex(1.609438, 2.214297)
result = c1.ln() result = c1.ln()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(0.804719, -2.034444) c2 = cmplx.complex(0.804719, -2.034444)
result = c1.ln() result = c1.ln()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_arg() { fn test_complex_arg() {
@ -297,19 +297,19 @@ fn test_complex_log() {
mut c2 := cmplx.complex(0.232873, -1.413175) mut c2 := cmplx.complex(0.232873, -1.413175)
mut result := c1.log(b1) mut result := c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
b1 = cmplx.complex(3, -1) b1 = cmplx.complex(3, -1)
c2 = cmplx.complex(0.152198, -0.409312) c2 = cmplx.complex(0.152198, -0.409312)
result = c1.log(b1) result = c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
b1 = cmplx.complex(0, 9) b1 = cmplx.complex(0, 9)
c2 = cmplx.complex(-0.298243, 1.197981) c2 = cmplx.complex(-0.298243, 1.197981)
result = c1.log(b1) result = c1.log(b1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_cpow() { fn test_complex_cpow() {
@ -319,19 +319,19 @@ fn test_complex_cpow() {
mut c2 := cmplx.complex(11.022341, -0.861785) mut c2 := cmplx.complex(11.022341, -0.861785)
mut result := c1.cpow(r1) mut result := c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
r1 = cmplx.complex(-4, -2) r1 = cmplx.complex(-4, -2)
c2 = cmplx.complex(0.118303, 0.063148) c2 = cmplx.complex(0.118303, 0.063148)
result = c1.cpow(r1) result = c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
r1 = cmplx.complex(8, -9) r1 = cmplx.complex(8, -9)
c2 = cmplx.complex(-0.000000, 0.000007) c2 = cmplx.complex(-0.000000, 0.000007)
result = c1.cpow(r1) result = c1.cpow(r1)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_sin() { fn test_complex_sin() {
@ -340,17 +340,17 @@ fn test_complex_sin() {
mut c2 := cmplx.complex(-525.794515, 155.536550) mut c2 := cmplx.complex(-525.794515, 155.536550)
mut result := c1.sin() mut result := c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-3.853738, -27.016813) c2 = cmplx.complex(-3.853738, -27.016813)
result = c1.sin() result = c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-3.165779, -1.959601) c2 = cmplx.complex(-3.165779, -1.959601)
result = c1.sin() result = c1.sin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_cos() { fn test_complex_cos() {
@ -359,17 +359,17 @@ fn test_complex_cos() {
mut c2 := cmplx.complex(155.536809, 525.793641) mut c2 := cmplx.complex(155.536809, 525.793641)
mut result := c1.cos() mut result := c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-27.034946, 3.851153) c2 = cmplx.complex(-27.034946, 3.851153)
result = c1.cos() result = c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(2.032723, -3.051898) c2 = cmplx.complex(2.032723, -3.051898)
result = c1.cos() result = c1.cos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_tan() { fn test_complex_tan() {
@ -378,17 +378,17 @@ fn test_complex_tan() {
mut c2 := cmplx.complex(-0.000001, 1.000001) mut c2 := cmplx.complex(-0.000001, 1.000001)
mut result := c1.tan() mut result := c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(0.000187, 0.999356) c2 = cmplx.complex(0.000187, 0.999356)
result = c1.tan() result = c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.033813, -1.014794) c2 = cmplx.complex(-0.033813, -1.014794)
result = c1.tan() result = c1.tan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_cot() { fn test_complex_cot() {
@ -397,17 +397,17 @@ fn test_complex_cot() {
mut c2 := cmplx.complex(-0.000001, -0.999999) mut c2 := cmplx.complex(-0.000001, -0.999999)
mut result := c1.cot() mut result := c1.cot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(0.000188, -1.000644) c2 = cmplx.complex(0.000188, -1.000644)
result = c1.cot() result = c1.cot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.032798, 0.984329) c2 = cmplx.complex(-0.032798, 0.984329)
result = c1.cot() result = c1.cot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_sec() { fn test_complex_sec() {
@ -416,17 +416,17 @@ fn test_complex_sec() {
mut c2 := cmplx.complex(0.000517, -0.001749) mut c2 := cmplx.complex(0.000517, -0.001749)
mut result := c1.sec() mut result := c1.sec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.036253, -0.005164) c2 = cmplx.complex(-0.036253, -0.005164)
result = c1.sec() result = c1.sec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(0.151176, 0.226974) c2 = cmplx.complex(0.151176, 0.226974)
result = c1.sec() result = c1.sec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_csc() { fn test_complex_csc() {
@ -435,17 +435,17 @@ fn test_complex_csc() {
mut c2 := cmplx.complex(-0.001749, -0.000517) mut c2 := cmplx.complex(-0.001749, -0.000517)
mut result := c1.csc() mut result := c1.csc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.005174, 0.036276) c2 = cmplx.complex(-0.005174, 0.036276)
result = c1.csc() result = c1.csc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.228375, 0.141363) c2 = cmplx.complex(-0.228375, 0.141363)
result = c1.csc() result = c1.csc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_asin() { fn test_complex_asin() {
@ -454,17 +454,17 @@ fn test_complex_asin() {
mut c2 := cmplx.complex(0.617064, 2.846289) mut c2 := cmplx.complex(0.617064, 2.846289)
mut result := c1.asin() mut result := c1.asin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.633984, 2.305509) c2 = cmplx.complex(-0.633984, 2.305509)
result = c1.asin() result = c1.asin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.427079, -1.528571) c2 = cmplx.complex(-0.427079, -1.528571)
result = c1.asin() result = c1.asin()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_acos() { fn test_complex_acos() {
@ -473,17 +473,17 @@ fn test_complex_acos() {
mut c2 := cmplx.complex(0.953732, -2.846289) mut c2 := cmplx.complex(0.953732, -2.846289)
mut result := c1.acos() mut result := c1.acos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(2.204780, -2.305509) c2 = cmplx.complex(2.204780, -2.305509)
result = c1.acos() result = c1.acos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(1.997875, 1.528571) c2 = cmplx.complex(1.997875, 1.528571)
result = c1.acos() result = c1.acos()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_atan() { fn test_complex_atan() {
@ -492,17 +492,17 @@ fn test_complex_atan() {
mut c2 := cmplx.complex(1.502727, 0.094441) mut c2 := cmplx.complex(1.502727, 0.094441)
mut result := c1.atan() mut result := c1.atan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-1.448307, 0.158997) c2 = cmplx.complex(-1.448307, 0.158997)
result = c1.atan() result = c1.atan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-1.338973, -0.402359) c2 = cmplx.complex(-1.338973, -0.402359)
result = c1.atan() result = c1.atan()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_acot() { fn test_complex_acot() {
@ -511,17 +511,17 @@ fn test_complex_acot() {
mut c2 := cmplx.complex(0.068069, -0.094441) mut c2 := cmplx.complex(0.068069, -0.094441)
mut result := c1.acot() mut result := c1.acot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.122489, -0.158997) c2 = cmplx.complex(-0.122489, -0.158997)
result = c1.acot() result = c1.acot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.231824, 0.402359) c2 = cmplx.complex(-0.231824, 0.402359)
result = c1.acot() result = c1.acot()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_asec() { fn test_complex_asec() {
@ -530,17 +530,17 @@ fn test_complex_asec() {
mut c2 := cmplx.complex(1.503480, 0.094668) mut c2 := cmplx.complex(1.503480, 0.094668)
mut result := c1.asec() mut result := c1.asec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(1.689547, 0.160446) c2 = cmplx.complex(1.689547, 0.160446)
result = c1.asec() result = c1.asec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(1.757114, -0.396568) c2 = cmplx.complex(1.757114, -0.396568)
result = c1.asec() result = c1.asec()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_acsc() { fn test_complex_acsc() {
@ -549,17 +549,17 @@ fn test_complex_acsc() {
mut c2 := cmplx.complex(0.067317, -0.094668) mut c2 := cmplx.complex(0.067317, -0.094668)
mut result := c1.acsc() mut result := c1.acsc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.118751, -0.160446) c2 = cmplx.complex(-0.118751, -0.160446)
result = c1.acsc() result = c1.acsc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.186318, 0.396568) c2 = cmplx.complex(-0.186318, 0.396568)
result = c1.acsc() result = c1.acsc()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_sinh() { fn test_complex_sinh() {
@ -568,17 +568,17 @@ fn test_complex_sinh() {
mut c2 := cmplx.complex(55.941968, 48.754942) mut c2 := cmplx.complex(55.941968, 48.754942)
mut result := c1.sinh() mut result := c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(6.548120, -7.619232) c2 = cmplx.complex(6.548120, -7.619232)
result = c1.sinh() result = c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(0.489056, -1.403119) c2 = cmplx.complex(0.489056, -1.403119)
result = c1.sinh() result = c1.sinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_cosh() { fn test_complex_cosh() {
@ -587,17 +587,17 @@ fn test_complex_cosh() {
mut c2 := cmplx.complex(55.947047, 48.750515) mut c2 := cmplx.complex(55.947047, 48.750515)
mut result := c1.cosh() mut result := c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-6.580663, 7.581553) c2 = cmplx.complex(-6.580663, 7.581553)
result = c1.cosh() result = c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.642148, 1.068607) c2 = cmplx.complex(-0.642148, 1.068607)
result = c1.cosh() result = c1.cosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_tanh() { fn test_complex_tanh() {
@ -606,17 +606,17 @@ fn test_complex_tanh() {
mut c2 := cmplx.complex(0.999988, 0.000090) mut c2 := cmplx.complex(0.999988, 0.000090)
mut result := c1.tanh() mut result := c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-1.000710, 0.004908) c2 = cmplx.complex(-1.000710, 0.004908)
result = c1.tanh() result = c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-1.166736, 0.243458) c2 = cmplx.complex(-1.166736, 0.243458)
result = c1.tanh() result = c1.tanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_coth() { fn test_complex_coth() {
@ -625,17 +625,17 @@ fn test_complex_coth() {
mut c2 := cmplx.complex(1.000012, -0.000090) mut c2 := cmplx.complex(1.000012, -0.000090)
mut result := c1.coth() mut result := c1.coth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.999267, -0.004901) c2 = cmplx.complex(-0.999267, -0.004901)
result = c1.coth() result = c1.coth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.821330, -0.171384) c2 = cmplx.complex(-0.821330, -0.171384)
result = c1.coth() result = c1.coth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_sech() { fn test_complex_sech() {
@ -644,17 +644,17 @@ fn test_complex_sech() {
mut c2 := cmplx.complex(0.010160, -0.008853) mut c2 := cmplx.complex(0.010160, -0.008853)
mut result := c1.sech() mut result := c1.sech()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.065294, -0.075225) c2 = cmplx.complex(-0.065294, -0.075225)
result = c1.sech() result = c1.sech()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.413149, -0.687527) c2 = cmplx.complex(-0.413149, -0.687527)
result = c1.sech() result = c1.sech()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_csch() { fn test_complex_csch() {
@ -663,17 +663,17 @@ fn test_complex_csch() {
mut c2 := cmplx.complex(0.010159, -0.008854) mut c2 := cmplx.complex(0.010159, -0.008854)
mut result := c1.csch() mut result := c1.csch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(0.064877, 0.075490) c2 = cmplx.complex(0.064877, 0.075490)
result = c1.csch() result = c1.csch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(0.221501, 0.635494) c2 = cmplx.complex(0.221501, 0.635494)
result = c1.csch() result = c1.csch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_asinh() { fn test_complex_asinh() {
@ -682,17 +682,17 @@ fn test_complex_asinh() {
mut c2 := cmplx.complex(2.844098, 0.947341) mut c2 := cmplx.complex(2.844098, 0.947341)
mut result := c1.asinh() mut result := c1.asinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-2.299914, 0.917617) c2 = cmplx.complex(-2.299914, 0.917617)
result = c1.asinh() result = c1.asinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-1.469352, -1.063440) c2 = cmplx.complex(-1.469352, -1.063440)
result = c1.asinh() result = c1.asinh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_acosh() { fn test_complex_acosh() {
@ -701,17 +701,17 @@ fn test_complex_acosh() {
mut c2 := cmplx.complex(2.846289, 0.953732) mut c2 := cmplx.complex(2.846289, 0.953732)
mut result := c1.acosh() mut result := c1.acosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(2.305509, 2.204780) c2 = cmplx.complex(2.305509, 2.204780)
result = c1.acosh() result = c1.acosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(1.528571, -1.997875) c2 = cmplx.complex(1.528571, -1.997875)
result = c1.acosh() result = c1.acosh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_atanh() { fn test_complex_atanh() {
@ -720,17 +720,17 @@ fn test_complex_atanh() {
mut c2 := cmplx.complex(0.067066, 1.476056) mut c2 := cmplx.complex(0.067066, 1.476056)
mut result := c1.atanh() mut result := c1.atanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.117501, 1.409921) c2 = cmplx.complex(-0.117501, 1.409921)
result = c1.atanh() result = c1.atanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.173287, -1.178097) c2 = cmplx.complex(-0.173287, -1.178097)
result = c1.atanh() result = c1.atanh()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_acoth() { fn test_complex_acoth() {
@ -739,17 +739,17 @@ fn test_complex_acoth() {
mut c2 := cmplx.complex(0.067066, -0.094740) mut c2 := cmplx.complex(0.067066, -0.094740)
mut result := c1.acoth() mut result := c1.acoth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.117501, -0.160875) c2 = cmplx.complex(-0.117501, -0.160875)
result = c1.acoth() result = c1.acoth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.173287, 0.392699) c2 = cmplx.complex(-0.173287, 0.392699)
result = c1.acoth() result = c1.acoth()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
// fn test_complex_asech() { // fn test_complex_asech() {
@ -758,17 +758,17 @@ fn test_complex_acoth() {
// mut c2 := cmplx.complex(0.094668,-1.503480) // mut c2 := cmplx.complex(0.094668,-1.503480)
// mut result := c1.asech() // mut result := c1.asech()
// // Some issue with precision comparison in f64 using == operator hence serializing to string // // Some issue with precision comparison in f64 using == operator hence serializing to string
// assert result.str().eq(c2.str()) // assert result.str() == c2.str()
// c1 = cmplx.complex(-3,4) // c1 = cmplx.complex(-3,4)
// c2 = cmplx.complex(0.160446,-1.689547) // c2 = cmplx.complex(0.160446,-1.689547)
// result = c1.asech() // result = c1.asech()
// // Some issue with precision comparison in f64 using == operator hence serializing to string // // Some issue with precision comparison in f64 using == operator hence serializing to string
// assert result.str().eq(c2.str()) // assert result.str() c2.str()
// c1 = cmplx.complex(-1,-2) // c1 = cmplx.complex(-1,-2)
// c2 = cmplx.complex(0.396568,1.757114) // c2 = cmplx.complex(0.396568,1.757114)
// result = c1.asech() // result = c1.asech()
// // Some issue with precision comparison in f64 using == operator hence serializing to string // // Some issue with precision comparison in f64 using == operator hence serializing to string
// assert result.str().eq(c2.str()) // assert result.str() == c2.str()
// } // }
fn test_complex_acsch() { fn test_complex_acsch() {
@ -777,17 +777,17 @@ fn test_complex_acsch() {
mut c2 := cmplx.complex(0.067819, -0.094518) mut c2 := cmplx.complex(0.067819, -0.094518)
mut result := c1.acsch() mut result := c1.acsch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-3, 4) c1 = cmplx.complex(-3, 4)
c2 = cmplx.complex(-0.121246, -0.159507) c2 = cmplx.complex(-0.121246, -0.159507)
result = c1.acsch() result = c1.acsch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
c1 = cmplx.complex(-1, -2) c1 = cmplx.complex(-1, -2)
c2 = cmplx.complex(-0.215612, 0.401586) c2 = cmplx.complex(-0.215612, 0.401586)
result = c1.acsch() result = c1.acsch()
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert result.str().eq(c2.str()) assert result.str() == c2.str()
} }
fn test_complex_re_im() { fn test_complex_re_im() {

View File

@ -44,8 +44,8 @@ fn test_geometric_mean() {
data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)] data = [f64(-3.0), f64(67.31), f64(4.4), f64(1.89)]
o = stats.geometric_mean(data) o = stats.geometric_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('nan') || o.str().eq('-nan') || o.str().eq('-1.#IND00') || o == f64(0) assert o.str() == 'nan' || o.str() == '-nan' || o.str() == '-1.#IND00' || o == f64(0)
|| o.str().eq('-nan(ind)') // Because in math it yields a complex number || o.str() == '-nan(ind)' // Because in math it yields a complex number
data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)] data = [f64(12.0), f64(7.88), f64(76.122), f64(54.83)]
o = stats.geometric_mean(data) o = stats.geometric_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string // Some issue with precision comparison in f64 using == operator hence serializing to string

View File

@ -290,11 +290,11 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
mut op1, mut op2 := '', '' mut op1, mut op2 := '', ''
if infix_expr.left_type == ast.string_type { if infix_expr.left_type == ast.string_type {
if is_reverse { if is_reverse {
op1 = 'string_gt(a_, b_)' op1 = 'string__lt(b_, a_)'
op2 = 'string_lt(a_, b_)' op2 = 'string__lt(a_, b_)'
} else { } else {
op1 = 'string_lt(a_, b_)' op1 = 'string__lt(a_, b_)'
op2 = 'string_gt(a_, b_)' op2 = 'string__lt(b_, a_)'
} }
} else { } else {
deref_str := if infix_expr.left_type.is_ptr() { '*' } else { '' } deref_str := if infix_expr.left_type.is_ptr() { '*' } else { '' }

View File

@ -23,7 +23,7 @@ fn (mut g Gen) gen_sumtype_equality_fn(left ast.Type) string {
fn_builder.writeln('\tif (a._typ == $typ) {') fn_builder.writeln('\tif (a._typ == $typ) {')
name := '_$sym.cname' name := '_$sym.cname'
if sym.kind == .string { if sym.kind == .string {
fn_builder.writeln('\t\tif (string_ne(*a.$name, *b.$name)) {') fn_builder.writeln('\t\tif (!string__eq(*a.$name, *b.$name)) {')
} else if sym.kind == .sum_type && !typ.is_ptr() { } else if sym.kind == .sum_type && !typ.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(typ) eq_fn := g.gen_sumtype_equality_fn(typ)
fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(*a.$name, *b.$name)) {') fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(*a.$name, *b.$name)) {')
@ -73,7 +73,7 @@ fn (mut g Gen) gen_struct_equality_fn(left ast.Type) string {
} }
fn_builder.writeln('static bool ${ptr_typ}_struct_eq($ptr_typ a, $ptr_typ b) {') fn_builder.writeln('static bool ${ptr_typ}_struct_eq($ptr_typ a, $ptr_typ b) {')
// orverloaded // overloaded
if left_sym.has_method('==') { if left_sym.has_method('==') {
fn_builder.writeln('\treturn ${ptr_typ}__eq(a, b);') fn_builder.writeln('\treturn ${ptr_typ}__eq(a, b);')
fn_builder.writeln('}') fn_builder.writeln('}')
@ -83,7 +83,7 @@ fn (mut g Gen) gen_struct_equality_fn(left ast.Type) string {
for field in info.fields { for field in info.fields {
sym := g.table.get_type_symbol(field.typ) sym := g.table.get_type_symbol(field.typ)
if sym.kind == .string { if sym.kind == .string {
fn_builder.writeln('\tif (string_ne(a.$field.name, b.$field.name)) {') fn_builder.writeln('\tif (!string__eq(a.$field.name, b.$field.name)) {')
} else if sym.kind == .sum_type && !field.typ.is_ptr() { } else if sym.kind == .sum_type && !field.typ.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(field.typ) eq_fn := g.gen_sumtype_equality_fn(field.typ)
fn_builder.writeln('\tif (!${eq_fn}_sumtype_eq(a.$field.name, b.$field.name)) {') fn_builder.writeln('\tif (!${eq_fn}_sumtype_eq(a.$field.name, b.$field.name)) {')
@ -128,7 +128,7 @@ fn (mut g Gen) gen_alias_equality_fn(left ast.Type) string {
fn_builder.writeln('static bool ${ptr_typ}_alias_eq($ptr_typ a, $ptr_typ b) {') fn_builder.writeln('static bool ${ptr_typ}_alias_eq($ptr_typ a, $ptr_typ b) {')
sym := g.table.get_type_symbol(info.parent_type) sym := g.table.get_type_symbol(info.parent_type)
if sym.kind == .string { if sym.kind == .string {
fn_builder.writeln('\tif (string_ne(a, b)) {') fn_builder.writeln('\tif (!string__eq(a, b)) {')
} else if sym.kind == .sum_type && !left.is_ptr() { } else if sym.kind == .sum_type && !left.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(info.parent_type) eq_fn := g.gen_sumtype_equality_fn(info.parent_type)
fn_builder.writeln('\tif (!${eq_fn}_sumtype_eq(a, b)) {') fn_builder.writeln('\tif (!${eq_fn}_sumtype_eq(a, b)) {')
@ -176,7 +176,7 @@ fn (mut g Gen) gen_array_equality_fn(left ast.Type) string {
fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {') fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {')
// compare every pair of elements of the two arrays // compare every pair of elements of the two arrays
if elem_sym.kind == .string { if elem_sym.kind == .string {
fn_builder.writeln('\t\tif (string_ne(*(($ptr_elem_typ*)((byte*)a.data+(i*a.element_size))), *(($ptr_elem_typ*)((byte*)b.data+(i*b.element_size))))) {') fn_builder.writeln('\t\tif (!string__eq(*(($ptr_elem_typ*)((byte*)a.data+(i*a.element_size))), *(($ptr_elem_typ*)((byte*)b.data+(i*b.element_size))))) {')
} else if elem_sym.kind == .sum_type && !elem_typ.is_ptr() { } else if elem_sym.kind == .sum_type && !elem_typ.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(elem_typ) eq_fn := g.gen_sumtype_equality_fn(elem_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq((($ptr_elem_typ*)a.data)[i], (($ptr_elem_typ*)b.data)[i])) {') fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq((($ptr_elem_typ*)a.data)[i], (($ptr_elem_typ*)b.data)[i])) {')
@ -226,7 +226,7 @@ fn (mut g Gen) gen_fixed_array_equality_fn(left ast.Type) string {
fn_builder.writeln('\tfor (int i = 0; i < $size; ++i) {') fn_builder.writeln('\tfor (int i = 0; i < $size; ++i) {')
// compare every pair of elements of the two fixed arrays // compare every pair of elements of the two fixed arrays
if elem_sym.kind == .string { if elem_sym.kind == .string {
fn_builder.writeln('\t\tif (string_ne(a[i], b[i])) {') fn_builder.writeln('\t\tif (!string__eq(a[i], b[i])) {')
} else if elem_sym.kind == .sum_type && !elem_typ.is_ptr() { } else if elem_sym.kind == .sum_type && !elem_typ.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(elem_typ) eq_fn := g.gen_sumtype_equality_fn(elem_typ)
fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(a[i], b[i])) {') fn_builder.writeln('\t\tif (!${eq_fn}_sumtype_eq(a[i], b[i])) {')

View File

@ -106,11 +106,11 @@ fn (mut g Gen) gen_str_default(sym ast.TypeSymbol, styp string, str_fn_name stri
g.type_definitions.writeln('string ${str_fn_name}($styp it); // auto') g.type_definitions.writeln('string ${str_fn_name}($styp it); // auto')
g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) {') g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) {')
if convertor == 'bool' { if convertor == 'bool' {
g.auto_str_funcs.writeln('\tstring tmp1 = string_add(_SLIT("${styp}("), ($convertor)it ? _SLIT("true") : _SLIT("false"));') g.auto_str_funcs.writeln('\tstring tmp1 = string__plus(_SLIT("${styp}("), ($convertor)it ? _SLIT("true") : _SLIT("false"));')
} else { } else {
g.auto_str_funcs.writeln('\tstring tmp1 = string_add(_SLIT("${styp}("), tos3(${typename_}_str(($convertor)it).str));') g.auto_str_funcs.writeln('\tstring tmp1 = string__plus(_SLIT("${styp}("), tos3(${typename_}_str(($convertor)it).str));')
} }
g.auto_str_funcs.writeln('\tstring tmp2 = string_add(tmp1, _SLIT(")"));') g.auto_str_funcs.writeln('\tstring tmp2 = string__plus(tmp1, _SLIT(")"));')
g.auto_str_funcs.writeln('\tstring_free(&tmp1);') g.auto_str_funcs.writeln('\tstring_free(&tmp1);')
g.auto_str_funcs.writeln('\treturn tmp2;') g.auto_str_funcs.writeln('\treturn tmp2;')
g.auto_str_funcs.writeln('}') g.auto_str_funcs.writeln('}')
@ -234,13 +234,13 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string
g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp it, int indent_count) {') g.auto_str_funcs.writeln('static string indent_${str_fn_name}($styp it, int indent_count) {')
g.auto_str_funcs.writeln('\tstring indents = _SLIT("");') g.auto_str_funcs.writeln('\tstring indents = _SLIT("");')
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {') g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
g.auto_str_funcs.writeln('\t\tindents = string_add(indents, _SLIT(" "));') g.auto_str_funcs.writeln('\t\tindents = string__plus(indents, _SLIT(" "));')
g.auto_str_funcs.writeln('\t}') g.auto_str_funcs.writeln('\t}')
g.auto_str_funcs.writeln('\treturn str_intp(3, _MOV((StrIntpData[]){ g.auto_str_funcs.writeln('\treturn str_intp(3, _MOV((StrIntpData[]){
{_SLIT0, $c.si_s_code, {.d_s = indents }}, {_SLIT0, $c.si_s_code, {.d_s = indents }},
{_SLIT("${clean_type_v_type_name}("), $c.si_s_code, {.d_s = ${parent_str_fn_name}(it) }}, {_SLIT("${clean_type_v_type_name}("), $c.si_s_code, {.d_s = ${parent_str_fn_name}(it) }},
{_SLIT(")"), 0, {.d_c = 0 }} {_SLIT(")"), 0, {.d_c = 0 }}
}));\n') }));\n')
// g.auto_str_funcs.writeln('\treturn _STR("%.*s\\000${clean_type_v_type_name}(%.*s\\000)", 3, indents, ${parent_str_fn_name}(it));') // g.auto_str_funcs.writeln('\treturn _STR("%.*s\\000${clean_type_v_type_name}(%.*s\\000)", 3, indents, ${parent_str_fn_name}(it));')
@ -317,9 +317,9 @@ fn (mut g Gen) gen_str_for_enum(info ast.Enum, styp string, str_fn_name string)
g.auto_str_funcs.writeln('\tstring ret = _SLIT("$clean_name{");') g.auto_str_funcs.writeln('\tstring ret = _SLIT("$clean_name{");')
g.auto_str_funcs.writeln('\tint first = 1;') g.auto_str_funcs.writeln('\tint first = 1;')
for i, val in info.vals { for i, val in info.vals {
g.auto_str_funcs.writeln('\tif (it & (1 << $i)) {if (!first) {ret = string_add(ret, _SLIT(" | "));} ret = string_add(ret, _SLIT(".$val")); first = 0;}') g.auto_str_funcs.writeln('\tif (it & (1 << $i)) {if (!first) {ret = string__plus(ret, _SLIT(" | "));} ret = string__plus(ret, _SLIT(".$val")); first = 0;}')
} }
g.auto_str_funcs.writeln('\tret = string_add(ret, _SLIT("}"));') g.auto_str_funcs.writeln('\tret = string__plus(ret, _SLIT("}"));')
g.auto_str_funcs.writeln('\treturn ret;') g.auto_str_funcs.writeln('\treturn ret;')
} else { } else {
g.auto_str_funcs.writeln('\tswitch(it) {') g.auto_str_funcs.writeln('\tswitch(it) {')

View File

@ -84,7 +84,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
// generate ident / indent length = 4 spaces // generate ident / indent length = 4 spaces
g.auto_str_funcs.writeln('\tstring indents = _SLIT("");') g.auto_str_funcs.writeln('\tstring indents = _SLIT("");')
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {') g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
g.auto_str_funcs.writeln('\t\tindents = string_add(indents, _SLIT(" "));') g.auto_str_funcs.writeln('\t\tindents = string__plus(indents, _SLIT(" "));')
g.auto_str_funcs.writeln('\t}') g.auto_str_funcs.writeln('\t}')
if info.fields.len == 0 { if info.fields.len == 0 {
g.auto_str_funcs.write_string('\treturn _SLIT("$clean_struct_v_type_name{}");') g.auto_str_funcs.write_string('\treturn _SLIT("$clean_struct_v_type_name{}");')
@ -290,7 +290,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
// generate ident / indent length = 4 spaces // generate ident / indent length = 4 spaces
g.auto_str_funcs.writeln('\tstring indents = _SLIT("");') g.auto_str_funcs.writeln('\tstring indents = _SLIT("");')
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {') g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
g.auto_str_funcs.writeln('\t\tindents = string_add(indents, _SLIT(" "));') g.auto_str_funcs.writeln('\t\tindents = string__plus(indents, _SLIT(" "));')
g.auto_str_funcs.writeln('\t}') g.auto_str_funcs.writeln('\t}')
if info.fields.len == 0 { if info.fields.len == 0 {
g.auto_str_funcs.write_string('\treturn _SLIT("$clean_struct_v_type_name{}");') g.auto_str_funcs.write_string('\treturn _SLIT("$clean_struct_v_type_name{}");')

View File

@ -2498,13 +2498,13 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
mut op_overloaded := false mut op_overloaded := false
if var_type == ast.string_type_idx && assign_stmt.op == .plus_assign { if var_type == ast.string_type_idx && assign_stmt.op == .plus_assign {
if left is ast.IndexExpr { if left is ast.IndexExpr {
// a[0] += str => `array_set(&a, 0, &(string[]) {string_add(...))})` // a[0] += str => `array_set(&a, 0, &(string[]) {string__plus(...))})`
g.expr(left) g.expr(left)
g.write('string_add(') g.write('string__plus(')
} else { } else {
// str += str2 => `str = string_add(str, str2)` // str += str2 => `str = string__plus(str, str2)`
g.expr(left) g.expr(left)
g.write(' = /*f*/string_add(') g.write(' = /*f*/string__plus(')
} }
g.is_assign_lhs = false g.is_assign_lhs = false
str_add = true str_add = true
@ -3575,7 +3575,7 @@ fn (mut g Gen) infix_gen_equality(node ast.InfixExpr, left_type ast.Type, left_s
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} }
.struct_ { .string, .struct_ {
// Auto generate both `==` and `!=` // Auto generate both `==` and `!=`
ptr_typ := g.gen_struct_equality_fn(left_type) ptr_typ := g.gen_struct_equality_fn(left_type)
if node.op == .eq { if node.op == .eq {
@ -3671,6 +3671,7 @@ fn (mut g Gen) infix_in_or_not_in(node ast.InfixExpr, left_sym ast.TypeSymbol, r
} }
fn (mut g Gen) infix_expr(node ast.InfixExpr) { fn (mut g Gen) infix_expr(node ast.InfixExpr) {
// TODO lot of clean required here
if node.auto_locked != '' { if node.auto_locked != '' {
g.writeln('sync__RwMutex_lock(&$node.auto_locked->mtx);') g.writeln('sync__RwMutex_lock(&$node.auto_locked->mtx);')
} }
@ -3682,21 +3683,26 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
// println('>>$node') // println('>>$node')
left_sym := g.table.get_type_symbol(left_type) left_sym := g.table.get_type_symbol(left_type)
left_final_sym := g.table.get_final_type_symbol(left_type) left_final_sym := g.table.get_final_type_symbol(left_type)
// TODO cleanup: linked to left/right_final_sym, unaliasing done twice
unaliased_left := if left_sym.kind == .alias { unaliased_left := if left_sym.kind == .alias {
(left_sym.info as ast.Alias).parent_type (left_sym.info as ast.Alias).parent_type
} else { } else {
left_type left_type
} }
op_is_key_in_or_not_in := node.op in [.key_in, .not_in]
op_is_eq_or_ne := node.op in [.eq, .ne] op_is_eq_or_ne := node.op in [.eq, .ne]
right_sym := g.table.get_type_symbol(node.right_type) right_sym := g.table.get_type_symbol(node.right_type)
right_final_sym := g.table.get_final_type_symbol(node.right_type) right_final_sym := g.table.get_final_type_symbol(node.right_type)
if node.op in [.key_in, .not_in] {
// TODO cleanup: handle the same as is / !is
g.infix_in_or_not_in(node, left_final_sym, right_final_sym)
return
}
unaliased_right := if right_sym.info is ast.Alias { unaliased_right := if right_sym.info is ast.Alias {
right_sym.info.parent_type right_sym.info.parent_type
} else { } else {
node.right_type node.right_type
} }
if unaliased_left == ast.ustring_type_idx && !op_is_key_in_or_not_in { if unaliased_left == ast.ustring_type_idx {
fn_name := match node.op { fn_name := match node.op {
.plus { .plus {
'ustring_add(' 'ustring_add('
@ -3729,54 +3735,20 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.write(', ') g.write(', ')
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if unaliased_left == ast.string_type_idx && !op_is_key_in_or_not_in { } 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
if node.op in [.eq, .ne] && node.right is ast.StringLiteral g.write('(')
&& (node.right as ast.StringLiteral).val == '' { g.expr(node.left)
arrow := if left_type.is_ptr() { '->' } else { '.' } g.write(')')
g.write('(') arrow := if left_type.is_ptr() { '->' } else { '.' }
g.expr(node.left) g.write('${arrow}len $node.op 0')
g.write(')')
g.write('${arrow}len $node.op 0')
} else {
fn_name := match node.op {
.plus {
'string_add('
}
.eq {
'string_eq('
}
.ne {
'string_ne('
}
.lt {
'string_lt('
}
.le {
'string_le('
}
.gt {
'string_gt('
}
.ge {
'string_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 op_is_eq_or_ne && left_sym.kind == right_sym.kind } else if op_is_eq_or_ne && left_sym.kind == right_sym.kind
&& left_sym.kind in [.array, .array_fixed, .alias, .map, .struct_, .sum_type] { && left_sym.kind in [.array, .array_fixed, .alias, .map, .struct_, .sum_type] {
g.infix_gen_equality(node, left_type, left_sym, right_sym) g.infix_gen_equality(node, left_type, left_sym, right_sym)
} else if op_is_key_in_or_not_in { } else if op_is_eq_or_ne && left_sym.kind == .alias && unaliased_right == ast.string_type {
g.infix_in_or_not_in(node, left_final_sym, right_final_sym) // TODO cleanup: almost copy of above
g.infix_gen_equality(node, unaliased_left, left_final_sym, right_sym)
} else if node.op == .left_shift && left_final_sym.kind == .array { } else if node.op == .left_shift && left_final_sym.kind == .array {
// arr << val // arr << val
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
@ -3869,35 +3841,45 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.expr(node.left) g.expr(node.left)
g.write(')') g.write(')')
} else { } else {
a := (left_sym.name[0].is_capital() || left_sym.name.contains('.')) // this likely covers more than V struct, but no idea what...
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.language != .c) || left_sym.kind == .string
b := left_sym.kind != .alias is_alias := left_sym.kind == .alias
c := left_sym.kind == .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
d := !b is_struct_alias := is_alias
&& g.typ((left_sym.info as ast.Alias).parent_type).split('__').last()[0].is_capital() && g.typ((left_sym.info as ast.Alias).parent_type).split('__').last()[0].is_capital()
// Do not generate operator overloading with these `right_sym.kind`. // Do not generate operator overloading with these `right_sym.kind`.
e := right_sym.kind !in [.voidptr, .int_literal, .int] not_exception := right_sym.kind !in [.voidptr, .int_literal, .int]
if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && ((a && b && e) || c || d) { if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq]
&& ((is_v_struct && !is_alias && not_exception) || is_c_alias
|| is_struct_alias) {
// Overloaded operators // Overloaded operators
the_left_type := if !d the_left_type := if !is_struct_alias
|| g.table.get_type_symbol((left_sym.info as ast.Alias).parent_type).kind in [.array, .array_fixed, .map] { || g.table.get_type_symbol((left_sym.info as ast.Alias).parent_type).kind in [.array, .array_fixed, .map] {
left_type left_type
} else { } else {
(left_sym.info as ast.Alias).parent_type (left_sym.info as ast.Alias).parent_type
} }
g.write(g.typ(the_left_type)) nr_muls := '*'.repeat(the_left_type.nr_muls())
g.write(g.typ(the_left_type.set_nr_muls(0)))
g.write('_') g.write('_')
g.write(util.replace_op(node.op.str())) g.write(util.replace_op(node.op.str()))
g.write('(') g.write('($nr_muls')
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', $nr_muls')
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.ne, .gt, .ge, .le] && ((a && b && e) || c || d) { } else if node.op in [.ne, .gt, .ge, .le] && ((is_v_struct && !is_alias && not_exception)
the_left_type := if !d { left_type } else { (left_sym.info as ast.Alias).parent_type } || is_c_alias || is_struct_alias) {
typ := g.typ(the_left_type) the_left_type := if !is_struct_alias {
left_type
} else {
(left_sym.info as ast.Alias).parent_type
}
nr_muls := '*'.repeat(the_left_type.nr_muls())
typ := g.typ(the_left_type.set_nr_muls(0))
if node.op == .gt { if node.op == .gt {
g.write('$typ') g.write('$typ')
} else { } else {
@ -3910,15 +3892,15 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.write('_lt') g.write('_lt')
} }
if node.op in [.le, .gt] { if node.op in [.le, .gt] {
g.write('(') g.write('($nr_muls')
g.expr(node.right) g.expr(node.right)
g.write(', ') g.write(', $nr_muls')
g.expr(node.left) g.expr(node.left)
g.write(')') g.write(')')
} else { } else {
g.write('(') g.write('($nr_muls')
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', $nr_muls')
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} }
@ -4239,7 +4221,7 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
if expr is ast.StringLiteral && (expr as ast.StringLiteral).val == '' { if expr is ast.StringLiteral && (expr as ast.StringLiteral).val == '' {
g.write('${cond_var}.len == 0') g.write('${cond_var}.len == 0')
} else { } else {
g.write('string_eq(') g.write('string__eq(')
g.write(cond_var) g.write(cond_var)
g.write(', ') g.write(', ')
g.expr(expr) g.expr(expr)
@ -5962,7 +5944,7 @@ fn (mut g Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
is_array := elem_sym.kind == .array is_array := elem_sym.kind == .array
for i, array_expr in right.exprs { for i, array_expr in right.exprs {
if is_str { if is_str {
g.write('string_eq(') g.write('string__eq(')
} else if is_array { } else if is_array {
ptr_typ := g.gen_array_equality_fn(right.elem_type) ptr_typ := g.gen_array_equality_fn(right.elem_type)
g.write('${ptr_typ}_arr_eq(') g.write('${ptr_typ}_arr_eq(')
@ -5983,19 +5965,6 @@ fn (mut g Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
} }
} }
fn op_to_fn_name(name string) string {
return match name {
'+' { '_op_plus' }
'-' { '_op_minus' }
'*' { '_op_mul' }
'/' { '_op_div' }
'%' { '_op_mod' }
'<' { '_op_lt' }
'>' { '_op_gt' }
else { 'bad op $name' }
}
}
[inline] [inline]
fn c_name(name_ string) string { fn c_name(name_ string) string {
name := util.no_dots(name_) name := util.no_dots(name_)

View File

@ -156,7 +156,7 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) {
if j > 0 { if j > 0 {
g.write(' else ') g.write(' else ')
} }
g.write('if (string_eq($node.method_name, _SLIT("$method.name"))) ') g.write('if (string__eq($node.method_name, _SLIT("$method.name"))) ')
} }
g.write('${util.no_dots(node.sym.name)}_${method.name}($amp ') g.write('${util.no_dots(node.sym.name)}_${method.name}($amp ')
g.expr(node.left) g.expr(node.left)

View File

@ -225,7 +225,7 @@ fn (mut g Gen) decode_array(value_type ast.Type) string {
noscan := g.check_noscan(value_type) noscan := g.check_noscan(value_type)
return ' return '
if(root && !cJSON_IsArray(root) && !cJSON_IsNull(root)) { if(root && !cJSON_IsArray(root) && !cJSON_IsNull(root)) {
return (Option_Array_$styp){.state = 2, .err = v_error(string_add(_SLIT("Json element is not an array: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; return (Option_Array_$styp){.state = 2, .err = v_error(string__plus(_SLIT("Json element is not an array: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}};
} }
res = __new_array${noscan}(0, 0, sizeof($styp)); res = __new_array${noscan}(0, 0, sizeof($styp));
const cJSON *jsval = NULL; const cJSON *jsval = NULL;
@ -269,7 +269,7 @@ fn (mut g Gen) decode_map(key_type ast.Type, value_type ast.Type) string {
} }
return ' return '
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) { if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
return (Option_Map_${styp}_$styp_v){ .state = 2, .err = v_error( string_add(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; return (Option_Map_${styp}_$styp_v){ .state = 2, .err = v_error(string__plus(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}};
} }
res = new_map(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn); res = new_map(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn);
cJSON *jsval = NULL; cJSON *jsval = NULL;

View File

@ -737,7 +737,7 @@ fn (mut g Gen) mysql_bind(val string, typ ast.Type) {
g.sql_buf.write_string(')') g.sql_buf.write_string(')')
} }
} else { } else {
g.sql_buf.write_string('string_add(_SLIT("\'"), string_add(((string) $val), _SLIT("\'")))') g.sql_buf.write_string('string__plus(_SLIT("\'"), string__plus(((string) $val), _SLIT("\'")))')
} }
g.sql_buf.writeln(');') g.sql_buf.writeln(');')
} }
@ -1031,7 +1031,7 @@ fn (mut g Gen) psql_select_expr(node ast.SqlExpr, sub bool, line string, typ Sql
} else if field.typ == ast.i8_type { } else if field.typ == ast.i8_type {
g.writeln('${tmp}.$field.name = (i8) string_${name}($fld);') g.writeln('${tmp}.$field.name = (i8) string_${name}($fld);')
} else if field.typ == ast.bool_type { } else if field.typ == ast.bool_type {
g.writeln('${tmp}.$field.name = string_eq($fld, _SLIT("0")) ? false : true;') g.writeln('${tmp}.$field.name = string__eq($fld, _SLIT("0")) ? false : true;')
} else { } else {
g.writeln('${tmp}.$field.name = string_${name}($fld);') g.writeln('${tmp}.$field.name = string_${name}($fld);')
} }
@ -1126,7 +1126,7 @@ fn (mut g Gen) psql_bind(val string, typ ast.Type) {
g.sql_buf.write_string(')') g.sql_buf.write_string(')')
} }
} else { } else {
g.sql_buf.write_string('string_add(_SLIT("\'"), string_add(((string) $val), _SLIT("\'")))') g.sql_buf.write_string('string__plus(_SLIT("\'"), string__plus(((string) $val), _SLIT("\'")))')
} }
g.sql_buf.writeln(');') g.sql_buf.writeln(');')
} }

View File

@ -373,25 +373,16 @@ pub fn skip_bom(file_content string) string {
} }
pub fn replace_op(s string) string { pub fn replace_op(s string) string {
if s.len == 1 { return match s {
last_char := s[s.len - 1] '+' { '_plus' }
suffix := match last_char { '-' { '_minus' }
`+` { '_plus' } '*' { '_mult' }
`-` { '_minus' } '/' { '_div' }
`*` { '_mult' } '%' { '_mod' }
`/` { '_div' } '<' { '_lt' }
`%` { '_mod' } '>' { '_gt' }
`<` { '_lt' } '==' { '_eq' }
`>` { '_gt' } else { '' }
else { '' }
}
return s[..s.len - 1] + suffix
} else {
suffix := match s {
'==' { '_eq' }
else { '' }
}
return s[..s.len - 2] + suffix
} }
} }