Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 66 additions & 6 deletions stdlib/src/builtin/string.mojo
Expand Up @@ -52,6 +52,23 @@ fn _ctlz(val: SIMD) -> __type_of(val):
)


@always_inline
fn _str_compare(str1: String, str2: String) -> Int:
Copy link
Contributor

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}.

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
# ===----------------------------------------------------------------------===#
Expand Down Expand Up @@ -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:
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure _str_compare is called only once.

Suggested change
return _str_compare(self, other) == 0 or _str_compare(self, other) == 1
return _str_compare(self, other) != -1


@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return _str_compare(self, other) == -1
return not self.__ge__(other)


@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return _str_compare(self, other) == 0 or _str_compare(self, other) == -1
return not self.__gt__(other)


@always_inline
fn __add__(self, other: String) -> String:
"""Creates a string by appending another string at the end.
Expand Down
15 changes: 15 additions & 0 deletions stdlib/test/builtin/test_string.mojo
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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")
Expand Down Expand Up @@ -697,6 +711,7 @@ def main():
test_constructors()
test_copy()
test_equality_operators()
test_compare_operators()
test_add()
test_stringable()
test_string_join()
Expand Down