Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement str.replace() and str.index() #2447

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ def test_str_split():
assert res5 == ["123"]
# assert res6 == [""]

def test_str_replace():
a: str = "abracadabra"
res: str = a.replace("a","b")
res1: str = a.replace("a","")
res2: str = a.replace("e","a")
res3: str = a.replace("ab","ba")
res4: str = a.replace("","")
assert res == "bbrbcbdbbrb"
assert res1 == "brcdbr"
assert res2 == "abracadabra"
assert res3 == "baracadbara"
assert res4 == "abracadabra"

def check():
f()
test_str_concat()
Expand All @@ -160,5 +173,6 @@ def check():
test_str_istitle()
test_str_isalpha()
test_str_split()
test_str_replace()

check()
47 changes: 47 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6789,6 +6789,53 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
} else {
fn_args.push_back(al, str);
}
} else if(attr_name == "replace") {
if(args.size() != 2) {
throw SemanticError("str.replace() takes two argument for now.", loc);
}
ASR::expr_t *arg_value = args[0].m_value;
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value);
if (!ASRUtils::is_character(*arg_value_type)) {
throw SemanticError("str.replace() argument 1 must be str", loc);
}
arg_value = args[1].m_value;
arg_value_type = ASRUtils::expr_type(arg_value);
if (!ASRUtils::is_character(*arg_value_type)) {
throw SemanticError("str.replace() argument 2 must be str", loc);
}
fn_call_name = "_lpython_str_replace";
ASR::call_arg_t str;
str.loc = loc;
str.m_value = s_var;

ASR::call_arg_t value;
value.loc = loc;
value.m_value = args[0].m_value;

fn_args.push_back(al, str);
fn_args.push_back(al, value);
value.m_value = args[1].m_value;
fn_args.push_back(al, value);
} else if(attr_name == "index") {
if (args.size() != 1) {
throw SemanticError("str.index() takes one argument",
loc);
}
ASR::expr_t *arg_sub = args[0].m_value;
ASR::ttype_t *arg_sub_type = ASRUtils::expr_type(arg_sub);
if (!ASRUtils::is_character(*arg_sub_type)) {
throw SemanticError("str.index() takes one argument of type: str",
loc);
}
fn_call_name = "_lpython_str_index";
ASR::call_arg_t str;
str.loc = loc;
str.m_value = s_var;
ASR::call_arg_t sub;
sub.loc = loc;
sub.m_value = args[0].m_value;
fn_args.push_back(al, str);
fn_args.push_back(al, sub);
} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
/*
String Validation Methods i.e all "is" based functions are handled here
Expand Down
2 changes: 2 additions & 0 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ struct PythonIntrinsicProcedures {
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
{"_lpython_str_strip", {m_builtin, &not_implemented}},
{"_lpython_str_split", {m_builtin, &not_implemented}},
{"_lpython_str_replace", {m_builtin, &not_implemented}},
{"_lpython_str_index", {m_builtin, &not_implemented}},
{"_lpython_str_swapcase", {m_builtin, &not_implemented}},
{"_lpython_str_startswith", {m_builtin, &not_implemented}},
{"_lpython_str_endswith", {m_builtin, &not_implemented}},
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,23 @@ def _lpython_str_split(x: str, sep:str) -> list[str]:
start += ind + len(sep)
return res

@overload
def _lpython_str_replace(x: str, old:str, new:str) -> str:
if (old == ""):
res: str = ""
i: str
for i in x:
res += new + i
return res
return _lpython_str_join(new, _lpython_str_split(x,old))

@overload
def _lpython_str_index(s: str, sub: str) -> i32:
ind: i32 = _lpython_str_find(s, sub);
if (ind == -1):
raise ValueError("substring not found")
return ind

@overload
def _lpython_str_swapcase(s: str) -> str:
res :str = ""
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "5d4751789e2ddcd882c4d6026f801ba32cfc227fafff7395a788bdd9",
"stdout_hash": "74bc84a1e1097e1d450910b0122bfd6fddff20d3d5df5ba282f0b2bb",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading
Loading