Skip to content

Commit 6222816

Browse files
committed
Add isspace API in str
1 parent 37eca1a commit 6222816

File tree

66 files changed

+1301
-1263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1301
-1263
lines changed

integration_tests/test_str_attributes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ def is_ascii():
276276
s = "123 45 6"
277277
assert s.isascii() == True
278278

279+
280+
def isspace():
281+
assert "\n".isspace() == True
282+
assert " ".isspace() == True
283+
assert "\r".isspace() == True
284+
285+
s:str = " "
286+
assert s.isspace() == True
287+
s: str = "a"
288+
assert s.isspace() == False
289+
279290
def check():
280291
capitalize()
281292
lower()
@@ -290,5 +301,6 @@ def check():
290301
is_upper()
291302
is_decimal()
292303
is_ascii()
304+
isspace()
293305

294306
check()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6831,7 +6831,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
68316831
/*
68326832
String Validation Methods i.e all "is" based functions are handled here
68336833
*/
6834-
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii"}; // Database of validation methods supported
6834+
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii","space"}; // Database of validation methods supported
68356835
std::string method_name = attr_name.substr(2);
68366836

68376837
if(std::find(validation_methods.begin(),validation_methods.end(), method_name) == validation_methods.end()) {
@@ -7099,7 +7099,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
70997099
* islower() method is limited to English Alphabets currently
71007100
* TODO: We can support other characters from Unicode Library
71017101
*/
7102-
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii"}; // Database of validation methods supported
7102+
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii","space"}; // Database of validation methods supported
71037103
std::string method_name = attr_name.substr(2);
71047104
if(std::find(validation_methods.begin(),validation_methods.end(), method_name) == validation_methods.end()) {
71057105
throw SemanticError("String method not implemented: " + attr_name, loc);
@@ -7179,6 +7179,22 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
71797179
tmp = ASR::make_LogicalConstant_t(al, loc, is_ascii,
71807180
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
71817181
return;
7182+
} else if(attr_name == "isspace") {
7183+
/*
7184+
* Specification:
7185+
Return true if all characters in the input string are considered whitespace characters,
7186+
as defined by the std::isspace function. Return false otherwise.
7187+
*/
7188+
bool is_space = true;
7189+
for (char i : s_var) {
7190+
if (!std::isspace(static_cast<unsigned char>(i))) {
7191+
is_space = false;
7192+
break;
7193+
}
7194+
}
7195+
tmp = ASR::make_LogicalConstant_t(al, loc, is_space,
7196+
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
7197+
return;
71827198
} else {
71837199
throw SemanticError("'str' object has no attribute '" + attr_name + "'", loc);
71847200
}

src/lpython/semantics/python_comptime_eval.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ struct PythonIntrinsicProcedures {
9292
{"_lpython_str_islower", {m_builtin, &not_implemented}},
9393
{"_lpython_str_isupper", {m_builtin, &not_implemented}},
9494
{"_lpython_str_isdecimal", {m_builtin, &not_implemented}},
95-
{"_lpython_str_isascii", {m_builtin, &not_implemented}}
95+
{"_lpython_str_isascii", {m_builtin, &not_implemented}},
96+
{"_lpython_str_isspace", {m_builtin, &not_implemented}}
9697
};
9798
}
9899

src/runtime/lpython_builtin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,15 @@ def _lpython_str_isascii(s: str) -> bool:
945945
return False
946946
return True
947947

948+
@overload
949+
def _lpython_str_isspace(s:str) -> bool:
950+
ch:str
951+
for ch in s:
952+
if ch != ' ' and ch != '\t' and ch != '\n' and ch != '\r' and ch != '\f' and ch != '\v':
953+
return False
954+
return True
955+
956+
948957
def list(s: str) -> list[str]:
949958
l: list[str] = []
950959
i: i32

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "f1338c6f6e5f0d9e55addc9bd28498dde023b5870395fd4e097788d2",
9+
"stdout_hash": "ee0a4d84951f6650a05acb092400a9f68bf7141cd2539099696c0379",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)