Skip to content

Commit 8dc7035

Browse files
authored
Merge pull request #205 from namannimmo10/strcmp
Make runtime string comparison work
2 parents f81caec + d98f13a commit 8dc7035

File tree

1 file changed

+72
-8
lines changed

1 file changed

+72
-8
lines changed
Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def _lpython_str_equal(a: str, b: str) -> bool:
1+
def _lpython_strcmp_eq(a: str, b: str) -> bool:
22
if len(a) != len(b):
33
return False
44
i: i32
@@ -7,13 +7,77 @@ def _lpython_str_equal(a: str, b: str) -> bool:
77
return False
88
return True
99

10+
def _lpython_strcmp_noteq(a: str, b: str) -> bool:
11+
return not _lpython_strcmp_eq(a, b)
12+
13+
def _lpython_strcmp_lt(a: str, b: str) -> bool:
14+
l: i32
15+
l = len(a)
16+
if l > len(b):
17+
l = len(b)
18+
i: i32
19+
for i in range(l):
20+
if a[i] > b[i]:
21+
return False
22+
return True
23+
24+
def _lpython_strcmp_lteq(a: str, b: str) -> bool:
25+
return _lpython_strcmp_eq(a, b) or _lpython_strcmp_lt(a, b)
26+
27+
def _lpython_strcmp_gt(a: str, b: str) -> bool:
28+
l: i32
29+
l = len(a)
30+
if l > len(b):
31+
l = len(b)
32+
i: i32
33+
for i in range(l):
34+
if a[i] < b[i]:
35+
return False
36+
return True
37+
38+
def _lpython_strcmp_gteq(a: str, b: str) -> bool:
39+
return _lpython_strcmp_eq(a, b) or _lpython_strcmp_gt(a, b)
40+
1041
def f():
11-
assert _lpython_str_equal("a", "a")
12-
assert not _lpython_str_equal("a2", "a")
13-
assert not _lpython_str_equal("a", "a123")
14-
assert not _lpython_str_equal("a23", "a24")
15-
assert _lpython_str_equal("a24", "a24")
16-
assert _lpython_str_equal("abcdefg", "abcdefg")
17-
assert not _lpython_str_equal("abcdef3", "abcdefg")
42+
assert _lpython_strcmp_eq("a", "a")
43+
assert not _lpython_strcmp_eq("a2", "a")
44+
assert not _lpython_strcmp_eq("a", "a123")
45+
assert not _lpython_strcmp_eq("a23", "a24")
46+
assert _lpython_strcmp_eq("a24", "a24")
47+
assert _lpython_strcmp_eq("abcdefg", "abcdefg")
48+
assert not _lpython_strcmp_eq("abcdef3", "abcdefg")
49+
50+
assert _lpython_strcmp_noteq("a2", "a")
51+
assert _lpython_strcmp_noteq("a", "a123")
52+
assert _lpython_strcmp_noteq("a23", "a24")
53+
assert _lpython_strcmp_noteq("abcdef3", "abcdefg")
54+
assert not _lpython_strcmp_noteq("a24", "a24")
55+
56+
assert _lpython_strcmp_lt("a", "a2")
57+
assert _lpython_strcmp_lt("a", "a123")
58+
assert _lpython_strcmp_lt("a23", "a24")
59+
assert _lpython_strcmp_lt("abcdef3", "gbc")
60+
assert not _lpython_strcmp_lt("bg", "abc")
61+
assert not _lpython_strcmp_lt("abcdefg", "abcdef3")
62+
63+
assert _lpython_strcmp_lteq("a", "a2")
64+
assert _lpython_strcmp_lteq("a123", "a123")
65+
assert _lpython_strcmp_lteq("a23", "a24")
66+
assert not _lpython_strcmp_lteq("abcdefg", "abcdef3")
67+
68+
assert _lpython_strcmp_gt("a2", "a")
69+
assert _lpython_strcmp_gt("a123", "a")
70+
assert _lpython_strcmp_gt("a24", "a23")
71+
assert _lpython_strcmp_gt("z", "abcdef3")
72+
assert not _lpython_strcmp_gt("abcdef3", "abcdefg")
73+
74+
a: str
75+
a = "abcdefg"
76+
b: str
77+
b = "abcdef3"
78+
assert _lpython_strcmp_gteq("a2", "a")
79+
assert _lpython_strcmp_gteq("a123", "a123")
80+
assert _lpython_strcmp_gteq("bg", "abc")
81+
assert _lpython_strcmp_gteq(a, b)
1882

1983
f()

0 commit comments

Comments
 (0)