Skip to content

Commit 229832f

Browse files
committed
Fix conflicts
1 parent 87e7caa commit 229832f

33 files changed

+1350
-1193
lines changed

integration_tests/test_str_01.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,43 @@ 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+
c: str = "a12(){}A"
44+
res: bool = a.isalpha()
45+
res2: bool = b.isalpha()
46+
res3: bool = c.isalpha()
47+
assert res == True
48+
assert res2 == False
49+
assert res3 == False
50+
51+
52+
def test_str_title():
53+
a: str = "hello world"
54+
b: str = "hj'kl"
55+
c: str = "hELlo wOrlD"
56+
d: str = "{Hel1o}world"
57+
res: str = a.title()
58+
res2: str = b.title()
59+
res3: str = c.title()
60+
res4: str = d.title()
61+
assert res == "Hello World"
62+
assert res2 == "Hj'Kl"
63+
assert res3 == "Hello World"
64+
assert res4 == "{Hel1O}World"
65+
66+
def test_str_istitle():
67+
a: str = "Hello World"
68+
b: str = "Hj'kl"
69+
c: str = "hELlo wOrlD"
70+
res: bool = a.istitle()
71+
res2: bool = b.istitle()
72+
res3: bool = c.istitle()
73+
assert res == True
74+
assert res2 == False
75+
assert res3 == False
76+
4077
def test_str_repeat():
4178
a: str
4279
a = "Xyz"
@@ -88,5 +125,7 @@ def check():
88125
test_str_join_empty_str()
89126
test_str_join_empty_list()
90127
test_constant_str_subscript()
91-
128+
test_str_title()
129+
test_str_istitle()
130+
test_str_isalpha()
92131
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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,91 @@ def _lpython_str_join(s:str, lis:list[str]) -> str:
685685
res += s + lis[i]
686686
return res
687687

688+
def _lpython_str_isalpha(s: str) -> bool:
689+
ch: str
690+
for ch in s:
691+
ch_ord: i32 = ord(ch)
692+
if 65 <= ch_ord and ch_ord <= 90:
693+
continue
694+
if 97 <= ch_ord and ch_ord <= 122:
695+
continue
696+
return False
697+
return True
698+
699+
def _lpython_str_title(s: str) -> str:
700+
result: str = ""
701+
capitalize_next: bool = True
702+
ch: str
703+
for ch in s:
704+
ch_ord: i32 = ord(ch)
705+
if ch_ord >= 97 and ch_ord <= 122:
706+
if capitalize_next:
707+
result += chr(ord(ch) - ord('a') + ord('A'))
708+
capitalize_next = False
709+
else:
710+
result += ch
711+
elif ch_ord >= 65 and ch_ord <= 90:
712+
if capitalize_next:
713+
result += ch
714+
capitalize_next = False
715+
else:
716+
result += chr(ord(ch) + ord('a') - ord('A'))
717+
else:
718+
result += ch
719+
capitalize_next = True
720+
721+
return result
722+
723+
def _lpython_str_istitle(s: str) -> bool:
724+
length: i32 = len(s)
725+
726+
if length == 0:
727+
return False # Empty string is not in title case
728+
729+
word_start: bool = True # Flag to track the start of a word
730+
ch: str
731+
732+
for ch in s:
733+
if (ch == ' ' or ch == '\t' or ch == '\n') and word_start:
734+
return False # Found a space character at the start of a word
735+
<<<<<<< HEAD
736+
<<<<<<< HEAD
737+
elif ch.isalpha() and (ord('A') <= ord(ch) and ord(ch) <= ord('Z')):
738+
=======
739+
elif ch.isalpha() and ch.isupper():
740+
>>>>>>> 5c5bcc620 (Fix merge conflict)
741+
=======
742+
elif ch.isalpha() and (ord('A') <= ord(ch) and ord(ch) <= ord('Z')):
743+
>>>>>>> f7fcb9835 (Fix test references)
744+
if word_start:
745+
word_start = False
746+
else:
747+
return False # Found an uppercase character in the middle of a word
748+
<<<<<<< HEAD
749+
<<<<<<< HEAD
750+
elif ch.isalpha() and (ord('a') <= ord(ch) and ord(ch) <= ord('z')):
751+
=======
752+
elif ch.isalpha() and ch.islower():
753+
>>>>>>> 5c5bcc620 (Fix merge conflict)
754+
=======
755+
elif ch.isalpha() and (ord('a') <= ord(ch) and ord(ch) <= ord('z')):
756+
>>>>>>> f7fcb9835 (Fix test references)
757+
if word_start:
758+
return False # Found a lowercase character in the middle of a word
759+
word_start = False
760+
else:
761+
word_start = True
762+
763+
return True
764+
765+
766+
<<<<<<< HEAD
767+
<<<<<<< HEAD
768+
=======
769+
770+
>>>>>>> 5c5bcc620 (Fix merge conflict)
771+
=======
772+
>>>>>>> f7fcb9835 (Fix test references)
688773
@overload
689774
def _lpython_str_find(s: str, sub: str) -> i32:
690775
s_len :i32; sub_len :i32; flag: bool; _len: i32;

0 commit comments

Comments
 (0)