-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mojo-stdlib] Implement compare operation for String #2378
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -52,6 +52,23 @@ fn _ctlz(val: SIMD) -> __type_of(val): | |||||
) | ||||||
|
||||||
|
||||||
@always_inline | ||||||
fn _str_compare(str1: String, str2: String) -> Int: | ||||||
var min_len = len(str1) if len(str1) < len(str2) else len(str2) | ||||||
ConnorGray marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
var cmp = memcmp(str1._as_ptr(), str2._as_ptr(), min_len) | ||||||
if cmp < 0: | ||||||
return -1 | ||||||
elif cmp > 0: | ||||||
return 1 | ||||||
# now check length for str1 and str2 | ||||||
if len(str1) == len(str2): | ||||||
return 0 | ||||||
elif len(str1) > len(str2): | ||||||
return 1 | ||||||
else: | ||||||
return -1 | ||||||
|
||||||
|
||||||
# ===----------------------------------------------------------------------===# | ||||||
# ord | ||||||
# ===----------------------------------------------------------------------===# | ||||||
|
@@ -714,13 +731,8 @@ struct String( | |||||
Returns: | ||||||
True if the Strings are equal and False otherwise. | ||||||
""" | ||||||
if len(self) != len(other): | ||||||
return False | ||||||
|
||||||
if int(self._as_ptr()) == int(other._as_ptr()): | ||||||
return True | ||||||
|
||||||
return memcmp(self._as_ptr(), other._as_ptr(), len(self)) == 0 | ||||||
return _str_compare(self, other) == 0 | ||||||
|
||||||
@always_inline | ||||||
fn __ne__(self, other: String) -> Bool: | ||||||
|
@@ -734,6 +746,54 @@ struct String( | |||||
""" | ||||||
return not (self == other) | ||||||
|
||||||
@always_inline | ||||||
fn __gt__(self, other: String) -> Bool: | ||||||
"""Compares two Strings if self greater than the other. | ||||||
|
||||||
Args: | ||||||
other: The rhs of the operation. | ||||||
|
||||||
Returns: | ||||||
True if self are greater than other string. | ||||||
""" | ||||||
return _str_compare(self, other) == 1 | ||||||
|
||||||
@always_inline | ||||||
fn __ge__(self, other: String) -> Bool: | ||||||
"""Compares two Strings if self greater equal the other. | ||||||
|
||||||
Args: | ||||||
other: The rhs of the operation. | ||||||
|
||||||
Returns: | ||||||
True if self are greater equal the other string. | ||||||
""" | ||||||
return _str_compare(self, other) == 0 or _str_compare(self, other) == 1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ensure
Suggested change
|
||||||
|
||||||
@always_inline | ||||||
fn __lt__(self, other: String) -> Bool: | ||||||
"""Compares two Strings if self less than the other. | ||||||
|
||||||
Args: | ||||||
other: The rhs of the operation. | ||||||
|
||||||
Returns: | ||||||
True if self are less than other string. | ||||||
""" | ||||||
return _str_compare(self, other) == -1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
@always_inline | ||||||
fn __le__(self, other: String) -> Bool: | ||||||
"""Compares two Strings if self less equal the other. | ||||||
|
||||||
Args: | ||||||
other: The rhs of the operation. | ||||||
|
||||||
Returns: | ||||||
True if self are less equal other string. | ||||||
""" | ||||||
return _str_compare(self, other) == 0 or _str_compare(self, other) == -1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
@always_inline | ||||||
fn __add__(self, other: String) -> String: | ||||||
"""Creates a string by appending another string at the end. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,20 @@ fn test_equality_operators() raises: | |
assert_not_equal(s0, "notabc") | ||
|
||
|
||
fn test_compare_operators() raises: | ||
assert_true(str("feefef") > str("feefe")) | ||
assert_false(str("hello") > str("hello")) | ||
assert_true(str("hello") >= str("hello")) | ||
assert_false(str("hello") < str("hello")) | ||
assert_true(str("hello") <= str("hello")) | ||
assert_false(str("apple") > str("banana")) | ||
assert_false(str("apple") > str("banana")) | ||
assert_true(str("apple") > str("Banana")) | ||
assert_false(str("apple123") > str("apple456")) | ||
assert_false(str("appl2$") > str("banana")) | ||
assert_false(str("") > str("a")) | ||
Comment on lines
+102
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please modify these tests so that they use the dunder methods directly (as opposed to the operators). |
||
|
||
|
||
fn test_add() raises: | ||
var s1 = String("123") | ||
var s2 = String("abc") | ||
|
@@ -697,6 +711,7 @@ def main(): | |
test_constructors() | ||
test_copy() | ||
test_equality_operators() | ||
test_compare_operators() | ||
test_add() | ||
test_stringable() | ||
test_string_join() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please provide a summary of what the logic is in the docstring? Also, please document that this can return {-1, 0, 1}.