Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add str.isspace() method #2586

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

Merged
merged 27 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3a050cd
Handle unescaping of `\f`
kmr-srbh Mar 7, 2024
c5c9e77
Added `isspace()` method implementation
kmr-srbh Mar 7, 2024
86663b9
Revert auto-formatting changes
kmr-srbh Mar 7, 2024
55e39b0
Fix branch errors
kmr-srbh Mar 7, 2024
f69ebba
Update lpython_builtin.py
kmr-srbh Mar 7, 2024
f56cfec
Update lpython_builtin.py
kmr-srbh Mar 7, 2024
28c3e93
Merge branch 'main' into isspace-method
kmr-srbh Mar 8, 2024
504aaa0
Update test references
kmr-srbh Mar 9, 2024
5149ef4
Minor logic change
kmr-srbh Mar 9, 2024
27c16ad
Add tests
kmr-srbh Mar 9, 2024
7e6e8f4
Update test references
kmr-srbh Mar 9, 2024
c1c50ba
Merge branch 'main' into isspace-method
kmr-srbh Mar 9, 2024
8b84118
Update variable names in tests
kmr-srbh Mar 10, 2024
e48738e
Remove redundant list
kmr-srbh Mar 10, 2024
3184d12
Update tests and test references
kmr-srbh Mar 10, 2024
9d31ba5
Update test references
kmr-srbh Mar 10, 2024
dfce891
Remove unnecesary test
kmr-srbh Mar 10, 2024
72dc26b
Undo deleting file
kmr-srbh Mar 10, 2024
4a073f1
Update test references
kmr-srbh Mar 10, 2024
bf71e24
Tests: Add testcase and update test references
kmr-srbh Mar 10, 2024
dafd330
Remove unnecesary variable
kmr-srbh Mar 10, 2024
2f65c62
Merge branch 'main' into isspace-method
kmr-srbh Mar 11, 2024
b079a5e
Rename `char` to `ch`
kmr-srbh Mar 12, 2024
35f0d64
Tests: Fix old test and update references
kmr-srbh Mar 12, 2024
a610ee3
Delete tests/reference/asr-array_02_decl-e8f6874.stderr
kmr-srbh Mar 12, 2024
9767ae2
Delete unnecessary files
kmr-srbh Mar 12, 2024
d7f9210
Merge branch 'main' into isspace-method
Thirumalai-Shaktivel Mar 14, 2024
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
36 changes: 36 additions & 0 deletions integration_tests/test_str_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,42 @@ def is_title():
assert " ".istitle() == False

def is_space():
s0: str = ""
assert s0.isspace() == False
assert "".isspace() == False

s1: str = " \t\n\v\f\r"
assert s1.isspace() == True
assert " \t\n\v\f\r".isspace() == True

s2: str = " \t\n\v\f\rabcd"
assert s2.isspace() == False
assert " \t\n\v\f\rabcd".isspace() == False

s3: str = "abcd \t\n\v\f\ref"
assert s3.isspace() == False
assert "abcd \t\n\v\f\ref".isspace() == False

s4: str = " \\t\n\v\f\r"
assert s4.isspace() == False
assert " \\t\n\v\f\r".isspace() == False

s5: str = " \\t\\n\\v\\f\\r"
assert s5.isspace() == False
assert " \\t\\n\\v\\f\\r".isspace() == False

s6: str = "Hello, LPython!\n"
assert s6.isspace() == False
assert "Hello, LPython!\n".isspace() == False

s7: str = "\t\tHello! \n"
assert s7.isspace() == False
assert "\t\tHello! \n".isspace() == False

s8: str = " \t \n \v \f \r "
assert s8.isspace() == True
assert " \t \n \v \f \r ".isspace() == True

assert "\n".isspace() == True
assert " ".isspace() == True
assert "\r".isspace() == True
Expand Down
3 changes: 3 additions & 0 deletions src/libasr/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ char* str_unescape_c(Allocator &al, LCompilers::Str &s) {
} else if (s[idx] == '\\' && s[idx+1] == 'v') {
x += "\v";
idx++;
} else if (s[idx] == '\\' && s[idx + 1] == 'f') {
x += "\f";
idx++;
} else if (s[idx] == '\\' && s[idx+1] == '\\') {
x += "\\";
idx++;
Expand Down
37 changes: 31 additions & 6 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,6 @@ def _lpython_str_istitle(s: str) -> bool:

return True if not only_whitespace else False



@overload
def _lpython_str_find(s: str, sub: str) -> i32:
s_len :i32; sub_len :i32; flag: bool; _len: i32;
Expand Down Expand Up @@ -1051,16 +1049,42 @@ def _lpython_str_isascii(s: str) -> bool:
return False
return True

def _lpython_str_isspace(s:str) -> bool:
def _lpython_str_isspace(s: str) -> bool:
# A Unicode character is considered a 'whitespace' if it has has a bidirectional
# type 'WS', 'B' or 'S'; or the category 'Zs'.
if len(s) == 0:
return False
ch: str

ch: str
for ch in s:
if ch != ' ' and ch != '\t' and ch != '\n' and ch != '\r' and ch != '\f' and ch != '\v':
if not (ch == " " or # SPACE
ch == "\n" or # LINE FEED (LF)
ch == "\r" or # CARRIAGE RETURN (CR)
ch == "\t" or # CHARACTER TABULATION (HT)
ch == "\v" or # VERTICAL TAB (VT)
ch == "\f" or # FORM FEED (FF)
ch == "\u00A0" or # NO-BREAK SPACE
ch == "\u1680" or # OGHAM SPACE MARK
ch == "\u2000" or # EN QUAD
ch == "\u2001" or # EM QUAD
ch == "\u2002" or # EN SPACE
ch == "\u2003" or # EM SPACE
ch == "\u2004" or # THREE-PER-EM SPACE
ch == "\u2005" or # FOUR-PER-EM SPACE
ch == "\u2006" or # SIX-PER-EM SPACE
ch == "\u2007" or # FIGURE SPACE
ch == "\u2008" or # PUNCTUATION SPACE
ch == "\u2009" or # THIN SPACE
ch == "\u200A" or # HAIR SPACE
ch == "\u2028" or # LINE SEPARATOR
ch == "\u2029" or # PARAGRAPH SEPARATOR
ch == "\u202F" or # NARROW NO-BREAK SPACE
ch == "\u205F" or # MEDIUM MATHEMATICAL SPACE
ch == "\u3000" # IDEOGRAPHIC SPACE
):
return False
return True


def list(s: str) -> list[str]:
l: list[str] = []
i: i32
Expand All @@ -1069,3 +1093,4 @@ def list(s: str) -> list[str]:
for i in range(len(s)):
l.append(s[i])
return l