Skip to content

Commit 9932057

Browse files
committed
Handle Equal/Unequal for Strings better.
Fixes #1127
1 parent 8c4b80c commit 9932057

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

Diff for: mathics/builtin/comparison.py

+37-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
import itertools
6+
from typing import Optional, Union
67

78
import sympy
89

@@ -26,6 +27,9 @@
2627
)
2728
from mathics.core.numbers import dps
2829

30+
def cmp(a, b) -> int:
31+
"Returns 0 if a == b, -1 if a < b and 1 if a > b"
32+
return (a > b) - (a < b)
2933

3034
class SameQ(BinaryOperator):
3135
"""
@@ -208,7 +212,7 @@ def numerify_args(items, evaluation):
208212
class _EqualityOperator(_InequalityOperator):
209213
"Compares all pairs e.g. a == b == c compares a == b, b == c, and a == c."
210214

211-
def do_compare(self, l1, l2):
215+
def do_compare(self, l1, l2) -> Union[bool, None]:
212216
if l1.same(l2):
213217
return True
214218
elif l1 == SymbolTrue and l2 == SymbolFalse:
@@ -254,7 +258,13 @@ def apply(self, items, evaluation):
254258
args = self.numerify_args(items, evaluation)
255259
wanted = operators[self.get_name()]
256260
for x, y in itertools.combinations(args, 2):
257-
c = do_cmp(x, y)
261+
if isinstance(x, String) or isinstance(y, String):
262+
if not (isinstance(x, String) and isinstance(y, String)):
263+
c = 1
264+
else:
265+
c = cmp(x.get_string_value(), y.get_string_value())
266+
else:
267+
c = do_cmp(x, y)
258268
if c is None:
259269
return
260270
elif c not in wanted:
@@ -345,7 +355,7 @@ def apply(self, items, evaluation):
345355
return Expression("And", *groups)
346356

347357

348-
def do_cmp(x1, x2):
358+
def do_cmp(x1, x2) -> Optional[int]:
349359

350360
# don't attempt to compare complex numbers
351361
for x in (x1, x2):
@@ -399,8 +409,8 @@ class Equal(_EqualityOperator, SympyComparison):
399409
"""
400410
<dl>
401411
<dt>'Equal[$x$, $y$]'
402-
<dt>'$x$ == $y$'
403-
<dd>yields 'True' if $x$ and $y$ are known to be equal, or
412+
<dt>'$x$ == $y$'
413+
<dd>yields 'True' if $x$ and $y$ are known to be equal, or
404414
'False' if $x$ and $y$ are known to be unequal.
405415
<dt>'$lhs$ == $rhs$'
406416
<dd>represents the equation $lhs$ = $rhs$.
@@ -413,6 +423,17 @@ class Equal(_EqualityOperator, SympyComparison):
413423
>> 1==1.
414424
= True
415425
426+
Strings are allowed:
427+
Equal["11", "11"]
428+
= True
429+
430+
Equal["121", "11"]
431+
= False
432+
433+
Comparision to mismatched types is False:
434+
Equal[11, "11"]
435+
= False
436+
416437
Lists are compared based on their elements:
417438
>> {{1}, {2}} == {{1}, {2}}
418439
= True
@@ -500,6 +521,17 @@ class Unequal(_EqualityOperator, SympyComparison):
500521
>> 1 != 1.
501522
= False
502523
524+
Strings are allowed:
525+
Unequal["11", "11"]
526+
= False
527+
528+
Equal["121", "11"]
529+
= True
530+
531+
Comparision to mismatched types is True:
532+
Equal[11, "11"]
533+
= True
534+
503535
Lists are compared based on their elements:
504536
>> {1} != {2}
505537
= True

0 commit comments

Comments
 (0)