Skip to content

Commit 825364d

Browse files
authored
Added built in method upper() for string (#1714)
1 parent ab38df1 commit 825364d

File tree

64 files changed

+102
-61
lines changed

Some content is hidden

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

64 files changed

+102
-61
lines changed

integration_tests/test_str_attributes.py

100644100755
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def lower():
2121
assert "DDd12Vv" .lower() == "ddd12vv"
2222
assert "".lower() == ""
2323

24+
def upper():
25+
s: str
26+
s = "AaaaAABBbbbbBB!@12223BN"
27+
assert s.upper() == "AAAAAABBBBBBBB!@12223BN"
28+
assert "DDd12Vv".upper() == "DDD12VV"
29+
assert "".upper() == ""
2430

2531
def strip():
2632
s: str
@@ -179,6 +185,7 @@ def partition():
179185
def check():
180186
capitalize()
181187
lower()
188+
upper()
182189
strip()
183190
swapcase()
184191
find()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,6 +5773,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
57735773
arg.loc = loc;
57745774
arg.m_value = s_var;
57755775
fn_args.push_back(al, arg);
5776+
} else if (attr_name == "upper") {
5777+
if (args.size() != 0) {
5778+
throw SemanticError("str.upper() takes no arguments",
5779+
loc);
5780+
}
5781+
fn_call_name = "_lpython_str_upper";
5782+
ASR::call_arg_t arg;
5783+
arg.loc = loc;
5784+
arg.m_value = s_var;
5785+
fn_args.push_back(al, arg);
57765786
} else if (attr_name == "find") {
57775787
if (args.size() != 1) {
57785788
throw SemanticError("str.find() takes one argument",
@@ -6062,6 +6072,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
60626072
i = tolower(i);
60636073
}
60646074
}
6075+
} else if (attr_name == "upper") {
6076+
if (args.size() != 0) {
6077+
throw SemanticError("str.upper() takes no arguments",
6078+
loc);
6079+
}
6080+
for (auto &i : s_var) {
6081+
if (i >= 'a' && i<= 'z') {
6082+
i = toupper(i);
6083+
}
6084+
}
60656085
} else if (attr_name == "find") {
60666086
if (args.size() != 1) {
60676087
throw SemanticError("str.find() takes one arguments",

src/lpython/semantics/python_comptime_eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct PythonIntrinsicProcedures {
7272
// for evaluation.
7373
{"_lpython_str_capitalize", {m_builtin, &not_implemented}},
7474
{"_lpython_str_lower", {m_builtin, &not_implemented}},
75+
{"_lpython_str_upper", {m_builtin, &not_implemented}},
7576
{"_lpython_str_find", {m_builtin, &not_implemented}},
7677
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
7778
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,12 +689,25 @@ def _lpython_str_lower(x: str) -> str:
689689
res = ""
690690
i:str
691691
for i in x:
692-
if ord('A') <= ord(i) and ord('Z') >= ord(i):
692+
if ord('A') <= ord(i) and ord(i) <= ord('Z'):
693693
res += chr(ord(i) +32)
694694
else:
695695
res += i
696696
return res
697697

698+
@overload
699+
def _lpython_str_upper(x: str) -> str:
700+
res: str
701+
res = ""
702+
i:str
703+
for i in x:
704+
if ord('a') <= ord(i) and ord(i) <= ord('z'):
705+
res += chr(ord(i) -32)
706+
else:
707+
res += i
708+
return res
709+
710+
698711
@overload
699712
def _lpython_str_find(s: str, sub: str) -> i32:
700713
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": "b2131e7fac5ffaeae5379d22cbe0362cc22d8b6ca62e9f49e7d4f780",
9+
"stdout_hash": "57442cc5bdf2788dfde4f41fa74c886757651c0dc6a62b4f3e0ec745",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

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

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-array_02_decl-e8f6874.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_02_decl-e8f6874.stdout",
9-
"stdout_hash": "2adb7f466d09d9c6911977465fb6736be6b54a076074d9827688c7da",
9+
"stdout_hash": "0744133e16cf5b9274d2e3172c2cd9efb0f25962023b7bb240f2e753",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-array_02_decl-e8f6874.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/reference/asr-bindc_02-bc1a7ea.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-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "90ed89ce4e51d48a2f3676b335b22ed329b262136a0988ecd3f6d4b2",
9+
"stdout_hash": "253e33fc1172553b2973f9298c8db9d7d1cca284e77c37cb48ef1cbd",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_02-bc1a7ea.stdout

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)