add string comparison support (#294)
* enable relative imports * update per 1st review * remove symlink stuffs * fix cli run in make file * make resolving import path explicit * fix importDir * add string comparison operators * fix duplicates
This commit is contained in:
parent
d1dd01499f
commit
366c69902f
4 changed files with 53 additions and 6 deletions
|
@ -86,6 +86,13 @@
|
|||
- `(string) + (string) = (string)`: concatenation
|
||||
- `(string) + (other types) = (string)`: concatenation (after string-converted)
|
||||
|
||||
### Comparison Operators
|
||||
|
||||
- `(string) < (string) = (bool)`: less than
|
||||
- `(string) > (string) = (bool)`: greater than
|
||||
- `(string) <= (string) = (bool)`: less than or equal to
|
||||
- `(string) >= (string) = (bool)`: greater than or equal to
|
||||
|
||||
## Char
|
||||
|
||||
### Equality
|
||||
|
@ -184,4 +191,3 @@ Tests whether two _(immutable)_ maps contain the same key-objects.
|
|||
- `(immutable-map) != (immutable-map) = (bool)`: inequality
|
||||
- `(immutable-map) == (map) = (bool)`: equality
|
||||
- `(immutable-map) != (map) = (bool)`: inequality
|
||||
|
||||
|
|
|
@ -291,12 +291,12 @@ _In Tengo, all values can be either
|
|||
| `&^` | bitclear (AND NOT) | int |
|
||||
| `<<` | shift left | int |
|
||||
| `>>` | shift right | int |
|
||||
| `<` | less than | int, float, char, time |
|
||||
| `<=` | less than or equal to | int, float, char, time |
|
||||
| `>` | greater than | int, float, char, time |
|
||||
| `>=` | greater than or equal to | int, float, char, time |
|
||||
| `<` | less than | int, float, char, time, string |
|
||||
| `<=` | less than or equal to | int, float, char, time, string |
|
||||
| `>` | greater than | int, float, char, time, string |
|
||||
| `>=` | greater than or equal to | int, float, char, time, string |
|
||||
|
||||
_See [Operators](https://github.com/d5/tengo/blob/d5-patch-1/docs/operators.md)
|
||||
_See [Operators](https://github.com/d5/tengo/blob/master/docs/operators.md)
|
||||
for more details._
|
||||
|
||||
### Ternary Operators
|
||||
|
|
32
objects.go
32
objects.go
|
@ -1342,6 +1342,38 @@ func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) {
|
|||
}
|
||||
return &String{Value: o.Value + rhsStr}, nil
|
||||
}
|
||||
case token.Less:
|
||||
switch rhs := rhs.(type) {
|
||||
case *String:
|
||||
if o.Value < rhs.Value {
|
||||
return TrueValue, nil
|
||||
}
|
||||
return FalseValue, nil
|
||||
}
|
||||
case token.LessEq:
|
||||
switch rhs := rhs.(type) {
|
||||
case *String:
|
||||
if o.Value <= rhs.Value {
|
||||
return TrueValue, nil
|
||||
}
|
||||
return FalseValue, nil
|
||||
}
|
||||
case token.Greater:
|
||||
switch rhs := rhs.(type) {
|
||||
case *String:
|
||||
if o.Value > rhs.Value {
|
||||
return TrueValue, nil
|
||||
}
|
||||
return FalseValue, nil
|
||||
}
|
||||
case token.GreaterEq:
|
||||
switch rhs := rhs.(type) {
|
||||
case *String:
|
||||
if o.Value >= rhs.Value {
|
||||
return TrueValue, nil
|
||||
}
|
||||
return FalseValue, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrInvalidOperator
|
||||
}
|
||||
|
|
|
@ -3273,6 +3273,15 @@ func TestString(t *testing.T) {
|
|||
expectRun(t, `out = "Hello" != "Hello"`, nil, false)
|
||||
expectRun(t, `out = "Hello" != "World"`, nil, true)
|
||||
|
||||
expectRun(t, `out = "Hello" > "World"`, nil, false)
|
||||
expectRun(t, `out = "World" < "Hello"`, nil, false)
|
||||
expectRun(t, `out = "Hello" < "World"`, nil, true)
|
||||
expectRun(t, `out = "World" > "Hello"`, nil, true)
|
||||
expectRun(t, `out = "Hello" >= "World"`, nil, false)
|
||||
expectRun(t, `out = "Hello" <= "World"`, nil, true)
|
||||
expectRun(t, `out = "Hello" >= "Hello"`, nil, true)
|
||||
expectRun(t, `out = "World" <= "World"`, nil, true)
|
||||
|
||||
// index operator
|
||||
str := "abcdef"
|
||||
strStr := `"abcdef"`
|
||||
|
|
Loading…
Reference in a new issue