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

Skip to content

Commit 605e1d0

Browse files
authored
Merge pull request #2582 from Kishan-Ved/isalnum2
Added APIs isalnum() and isnumeric() in string object
2 parents 0bcc640 + a528d41 commit 605e1d0

File tree

62 files changed

+1353
-1231
lines changed

Some content is hidden

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

62 files changed

+1353
-1231
lines changed

integration_tests/test_str_attributes.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,69 @@ def is_space():
366366
s = ""
367367
assert s.isspace() == False
368368

369+
def is_alnum():
370+
a: str = "helloworld"
371+
b: str = "hj kl"
372+
c: str = "a12(){}A"
373+
d: str = " "
374+
e: str = ""
375+
f: str = "ab23"
376+
g: str = "ab2%3"
377+
res: bool = a.isalnum()
378+
res2: bool = b.isalnum()
379+
res3: bool = c.isalnum()
380+
res4: bool = d.isalnum()
381+
res5: bool = e.isalnum()
382+
res6: bool = f.isalnum()
383+
res7: bool = g.isalnum()
384+
385+
assert res == True
386+
assert res2 == False
387+
assert res3 == False
388+
assert res4 == False
389+
assert res5 == False
390+
assert res6 == True
391+
assert res7 == False
392+
393+
assert "helloworld".isalnum() == True
394+
assert "hj kl".isalnum() == False
395+
assert "a12(){}A".isalnum() == False
396+
assert " ".isalnum() == False
397+
assert "".isalnum() == False
398+
assert "ab23".isalnum() == True
399+
assert "ab2%3".isalnum() == False
400+
401+
def is_numeric():
402+
a: str = "123"
403+
b: str = "12 34"
404+
c: str = "-123"
405+
d: str = "12.3"
406+
e: str = " "
407+
f: str = ""
408+
g: str = "ab2%3"
409+
res: bool = a.isnumeric()
410+
res2: bool = b.isnumeric()
411+
res3: bool = c.isnumeric()
412+
res4: bool = d.isnumeric()
413+
res5: bool = e.isnumeric()
414+
res6: bool = f.isnumeric()
415+
res7: bool = g.isnumeric()
416+
417+
assert res == True
418+
assert res2 == False
419+
assert res3 == False
420+
assert res4 == False
421+
assert res5 == False
422+
assert res6 == False
423+
assert res7 == False
369424

425+
assert "123".isnumeric() == True
426+
assert "12 34".isnumeric() == False
427+
assert "-123".isnumeric() == False
428+
assert "12.3".isnumeric() == False
429+
assert " ".isnumeric() == False
430+
assert "".isnumeric() == False
431+
assert "ab2%3".isnumeric() == False
370432

371433
def check():
372434
capitalize()
@@ -386,6 +448,8 @@ def check():
386448
is_alpha()
387449
is_title()
388450
is_space()
451+
is_alnum()
452+
is_numeric()
389453

390454

391455
check()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6793,7 +6793,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
67936793
/*
67946794
String Validation Methods i.e all "is" based functions are handled here
67956795
*/
6796-
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title"}; // Database of validation methods supported
6796+
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title", "alnum", "numeric"}; // Database of validation methods supported
67976797
std::string method_name = attr_name.substr(2);
67986798

67996799
if(std::find(validation_methods.begin(),validation_methods.end(), method_name) == validation_methods.end()) {
@@ -7096,7 +7096,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
70967096
* islower() method is limited to English Alphabets currently
70977097
* TODO: We can support other characters from Unicode Library
70987098
*/
7099-
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title"}; // Database of validation methods supported
7099+
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii", "space", "alpha", "title", "alnum", "numeric"}; // Database of validation methods supported
71007100
std::string method_name = attr_name.substr(2);
71017101
if(std::find(validation_methods.begin(),validation_methods.end(), method_name) == validation_methods.end()) {
71027102
throw SemanticError("String method not implemented: " + attr_name, loc);
@@ -7210,6 +7210,38 @@ we will have to use something else.
72107210
tmp = ASR::make_LogicalConstant_t(al, loc, is_alpha,
72117211
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
72127212
return;
7213+
} else if (attr_name == "isalnum") {
7214+
/*
7215+
* Specification -
7216+
Return True if all characters in the string are alphabets or numbers,
7217+
and there is at least one character in the string.
7218+
*/
7219+
bool is_alnum = (s_var.size() != 0);
7220+
for (auto &i : s_var) {
7221+
if (!((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9'))) {
7222+
is_alnum = false;
7223+
break;
7224+
}
7225+
}
7226+
tmp = ASR::make_LogicalConstant_t(al, loc, is_alnum,
7227+
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
7228+
return;
7229+
} else if (attr_name == "isnumeric") {
7230+
/*
7231+
* Specification -
7232+
Return True if all characters in the string are numbers,
7233+
and there is at least one character in the string.
7234+
*/
7235+
bool is_numeric = (s_var.size() != 0);
7236+
for (auto &i : s_var) {
7237+
if (!(i >= '0' && i <= '9')) {
7238+
is_numeric = false;
7239+
break;
7240+
}
7241+
}
7242+
tmp = ASR::make_LogicalConstant_t(al, loc, is_numeric,
7243+
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)));
7244+
return;
72137245
} else if (attr_name == "istitle") {
72147246
/*
72157247
* Specification -

src/lpython/semantics/python_comptime_eval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ struct PythonIntrinsicProcedures {
8383
{"_lpython_str_join", {m_builtin, &not_implemented}},
8484
{"_lpython_str_find", {m_builtin, &not_implemented}},
8585
{"_lpython_str_isalpha", {m_builtin, &not_implemented}},
86+
{"_lpython_str_isalnum", {m_builtin, &not_implemented}},
87+
{"_lpython_str_isnumeric", {m_builtin, &not_implemented}},
8688
{"_lpython_str_title", {m_builtin, &not_implemented}},
8789
{"_lpython_str_istitle", {m_builtin, &not_implemented}},
8890
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,30 @@ def _lpython_str_isalpha(s: str) -> bool:
730730
return False
731731
return True
732732

733+
def _lpython_str_isalnum(s: str) -> bool:
734+
ch: str
735+
if len(s) == 0: return False
736+
for ch in s:
737+
ch_ord: i32 = ord(ch)
738+
if 65 <= ch_ord and ch_ord <= 90:
739+
continue
740+
if 97 <= ch_ord and ch_ord <= 122:
741+
continue
742+
if 48 <= ch_ord and ch_ord <= 57:
743+
continue
744+
return False
745+
return True
746+
747+
def _lpython_str_isnumeric(s: str) -> bool:
748+
ch: str
749+
if len(s) == 0: return False
750+
for ch in s:
751+
ch_ord: i32 = ord(ch)
752+
if 48 <= ch_ord and ch_ord <= 57:
753+
continue
754+
return False
755+
return True
756+
733757
def _lpython_str_title(s: str) -> str:
734758
result: str = ""
735759
capitalize_next: bool = True

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": "d167f932ab4a4bb559ae3b4bb3b983cd6153a105cfb6180474e64ae2",
9+
"stdout_hash": "7e3c68aa6acba27674e544f894bb141357db82f8840c756af448f5bb",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)