Skip to content

Commit 31628a8

Browse files
committed
Fix merge conflict
1 parent 670b12f commit 31628a8

File tree

66 files changed

+1842
-1539
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

+1842
-1539
lines changed

integration_tests/test_str_01.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ def test_str_slice():
3737
# TODO:
3838
# assert a[0:5:-1] == ""
3939

40+
def test_str_isalpha():
41+
a:str = "helloworld"
42+
b:str = "hj kl"
43+
res: bool = a.isalpha()
44+
res2: bool = b.isalpha()
45+
assert res == True
46+
assert res2 == False
47+
48+
def test_str_title():
49+
a:str = "hello world"
50+
b:str = "hj'kl"
51+
res: str = a.title()
52+
res2: str = b.title()
53+
assert res == "Hello World"
54+
assert res2 == "Hj'Kl"
55+
56+
def test_str_istitle():
57+
a:str = "Hello World"
58+
b:str = "Hj'kl"
59+
res: bool = a.istitle()
60+
res2: bool = b.istitle()
61+
assert res == True
62+
assert res2 == False
63+
4064
def test_str_repeat():
4165
a: str
4266
a = "Xyz"
@@ -88,5 +112,7 @@ def check():
88112
test_str_join_empty_str()
89113
test_str_join_empty_list()
90114
test_constant_str_subscript()
91-
115+
test_str_title()
116+
test_str_istitle()
117+
test_str_isalpha()
92118
check()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6468,6 +6468,36 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
64686468
arg.loc = loc;
64696469
arg.m_value = s_var;
64706470
fn_args.push_back(al, arg);
6471+
} else if (attr_name == "isalpha") {
6472+
if (args.size() != 0) {
6473+
throw SemanticError("str.isalpha() takes no arguments",
6474+
loc);
6475+
}
6476+
fn_call_name = "_lpython_str_isalpha";
6477+
ASR::call_arg_t arg;
6478+
arg.loc = loc;
6479+
arg.m_value = s_var;
6480+
fn_args.push_back(al, arg);
6481+
} else if (attr_name == "istitle") {
6482+
if (args.size() != 0) {
6483+
throw SemanticError("str.istitle() takes no arguments",
6484+
loc);
6485+
}
6486+
fn_call_name = "_lpython_str_istitle";
6487+
ASR::call_arg_t arg;
6488+
arg.loc = loc;
6489+
arg.m_value = s_var;
6490+
fn_args.push_back(al, arg);
6491+
} else if (attr_name == "title") {
6492+
if (args.size() != 0) {
6493+
throw SemanticError("str.title() takes no arguments",
6494+
loc);
6495+
}
6496+
fn_call_name = "_lpython_str_title";
6497+
ASR::call_arg_t arg;
6498+
arg.loc = loc;
6499+
arg.m_value = s_var;
6500+
fn_args.push_back(al, arg);
64716501
} else if (attr_name == "upper") {
64726502
if (args.size() != 0) {
64736503
throw SemanticError("str.upper() takes no arguments",

src/lpython/semantics/python_comptime_eval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ struct PythonIntrinsicProcedures {
8282
{"_lpython_str_upper", {m_builtin, &not_implemented}},
8383
{"_lpython_str_join", {m_builtin, &not_implemented}},
8484
{"_lpython_str_find", {m_builtin, &not_implemented}},
85+
{"_lpython_str_isalpha", {m_builtin, &not_implemented}},
86+
{"_lpython_str_title", {m_builtin, &not_implemented}},
87+
{"_lpython_str_istitle", {m_builtin, &not_implemented}},
8588
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
8689
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
8790
{"_lpython_str_strip", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,72 @@ def _lpython_str_join(s:str, lis:list[str]) -> str:
685685
res += s + lis[i]
686686
return res
687687

688+
@overload
689+
def _lpython_str_isalpha(s: str) -> bool:
690+
ch: str
691+
for ch in s:
692+
ch_ord: i32 = ord(ch)
693+
if 65 <= ch_ord and ch_ord <= 90:
694+
continue
695+
if 97 <= ch_ord and ch_ord <= 122:
696+
continue
697+
return False
698+
return True
699+
700+
@overload
701+
def _lpython_str_title(s: str) -> str:
702+
result: str = ""
703+
capitalize_next: bool = True
704+
ch: str
705+
for ch in s:
706+
ch_ord: i32 = ord(ch)
707+
if ch_ord >= 97 and ch_ord <= 122:
708+
if capitalize_next:
709+
result += chr(ord(ch) - ord('a') + ord('A'))
710+
capitalize_next = False
711+
else:
712+
result += ch
713+
elif ch_ord >= 65 and ch_ord <= 90:
714+
if capitalize_next:
715+
result += ch
716+
capitalize_next = False
717+
else:
718+
result += chr(ord(ch) + ord('a') - ord('A'))
719+
else:
720+
result += ch
721+
capitalize_next = True
722+
723+
return result
724+
725+
@overload
726+
def _lpython_str_istitle(s: str) -> bool:
727+
length: i32 = len(s)
728+
729+
if length == 0:
730+
return False # Empty string is not in title case
731+
732+
word_start: bool = True # Flag to track the start of a word
733+
ch: str
734+
735+
for ch in s:
736+
if (ch == ' ' or ch == '\t' or ch == '\n') and word_start:
737+
return False # Found a space character at the start of a word
738+
elif ch.isalpha() and ch.isupper():
739+
if word_start:
740+
word_start = False
741+
else:
742+
return False # Found an uppercase character in the middle of a word
743+
elif ch.isalpha() and ch.islower():
744+
if word_start:
745+
return False # Found a lowercase character in the middle of a word
746+
word_start = False
747+
else:
748+
word_start = True
749+
750+
return True
751+
752+
753+
688754
@overload
689755
def _lpython_str_find(s: str, sub: str) -> i32:
690756
s_len :i32; sub_len :i32; flag: bool; _len: 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": "24b1d1f4774489a87a82c51c4f4a797ca363f7efdb011e42936fc6b9",
9+
"stdout_hash": "061e9b8367c4ec2f73abaff839ab1e38dbd4d1dac3e89c29be9d5051",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)