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

[stdlib] pythonic isupper & islower #3095

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions stdlib/src/builtin/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,21 @@ fn isupper(c: UInt8) -> Bool:
return _is_ascii_uppercase(c)


fn isupper(c: String) -> Bool:
"""Determines whether the given character is an uppercase character.

This currently only respects the default "C" locale, i.e. returns True iff
the character specified is one of "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

Args:
c: The character to check.

Returns:
True if the character is uppercase.
"""
return _is_ascii_uppercase(ord(c))


fn _is_ascii_uppercase(c: UInt8) -> Bool:
alias ord_a = ord("A")
alias ord_z = ord("Z")
Expand All @@ -578,6 +593,21 @@ fn islower(c: UInt8) -> Bool:
return _is_ascii_lowercase(c)


fn islower(c: String) -> Bool:
"""Determines whether the given character is an lowercase character.

This currently only respects the default "C" locale, i.e. returns True iff
the character specified is one of "abcdefghijklmnopqrstuvwxyz".

Args:
c: The character to check.

Returns:
True if the character is lowercase.
"""
return _is_ascii_lowercase(ord(c))


fn _is_ascii_lowercase(c: UInt8) -> Bool:
alias ord_a = ord("a")
alias ord_z = ord("z")
Expand Down Expand Up @@ -2193,6 +2223,28 @@ struct String(
return False
return True

fn islower(self) -> Bool:
"""Returns True if all characters in the string are lowercase.

Returns:
True if all characters are lowercase else False.
"""
for c in self:
if not islower(c):
return False
return True

fn isupper(self) -> Bool:
"""Returns True if all characters in the string are uppercase.

Returns:
True if all characters are uppercase else False.
"""
for c in self:
if not isupper(c):
return False
return True


# ===----------------------------------------------------------------------=== #
# Utilities
Expand Down
24 changes: 24 additions & 0 deletions stdlib/test/builtin/test_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -833,28 +833,52 @@ fn test_splitlines() raises:

fn test_isupper() raises:
assert_true(isupper(ord("A")))
assert_true(isupper("A"))
assert_true(isupper(ord("B")))
assert_true(isupper("B"))
assert_true(isupper(ord("Y")))
assert_true(isupper("Y"))
assert_true(isupper(ord("Z")))
assert_true(isupper("Z"))

assert_false(isupper(ord("A") - 1))
assert_false(isupper("a"))
assert_false(isupper(ord("Z") + 1))
assert_false(isupper("z"))

assert_false(isupper(ord("!")))
assert_false(isupper("!"))
assert_false(isupper(ord("0")))
assert_false(isupper("0"))

assert_true(String("ABC").isupper())
assert_false(String("abc").isupper())
assert_false(String("ABC123").isupper())


fn test_islower() raises:
assert_true(islower(ord("a")))
assert_true(islower("a"))
assert_true(islower(ord("b")))
assert_true(islower("b"))
assert_true(islower(ord("y")))
assert_true(islower("y"))
assert_true(islower(ord("z")))
assert_true(islower("z"))

assert_false(islower(ord("a") - 1))
assert_false(islower("A"))
assert_false(islower(ord("z") + 1))
assert_false(islower("Z"))

assert_false(islower(ord("!")))
assert_false(islower("!"))
assert_false(islower(ord("0")))
assert_false(islower("0"))

assert_true(String("abc").islower())
assert_false(String("ABC").islower())
assert_false(String("abc123").islower())


fn test_lower() raises:
Expand Down