Skip to content

Added str.replace() #2587

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

Merged
merged 9 commits into from
Mar 10, 2024
Merged
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
50 changes: 50 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,55 @@ def test_str_split():
assert res5 == ["123"]
# assert res6 == [""]

def test_str_replace():
x: str = "abc"
a: str = "zzaaabracadabra"
print(a.replace("a",""))
print(a.replace("",""))
print(a.replace("a","b"))
print(a.replace("e","a"))
print(a.replace("ab","ba"))
print(a.replace("c","z"))
print(a.replace("zza","yo"))
print(a.replace("a","b",0))
print(a.replace("a","b",1))
print(a.replace("a","b",2))
print(a.replace("a","b",2))
print(a.replace("a","b",3))
print(a.replace("a","b",4))
print(a.replace("a","b",5))
print(a.replace("a","b",6))
print(a.replace("a","b",7))
print(a.replace("a","b",8))
print(a.replace("a","b",9))
print(a.replace("b","k",1))
print(a.replace("b","k",2))
print(a.replace("zza","yo",2))
print(x.replace("", ","))
assert a.replace("a","") == "zzbrcdbr"
assert a.replace("","") == "zzaaabracadabra"
assert a.replace("a","b") == "zzbbbbrbcbdbbrb"
assert a.replace("e","a") == "zzaaabracadabra"
assert a.replace("ab","ba") == "zzaabaracadbara"
assert a.replace("c","z") == "zzaaabrazadabra"
assert a.replace("zza","yo") == "yoaabracadabra"
assert a.replace("a","b",0) == "zzaaabracadabra"
assert a.replace("a","b",1) == "zzbaabracadabra"
assert a.replace("a","b",2) == "zzbbabracadabra"
assert a.replace("a","b",2) == "zzbbabracadabra"
assert a.replace("a","b",3) == "zzbbbbracadabra"
assert a.replace("a","b",4) == "zzbbbbrbcadabra"
assert a.replace("a","b",5) == "zzbbbbrbcbdabra"
assert a.replace("a","b",6) == "zzbbbbrbcbdbbra"
assert a.replace("a","b",7) == "zzbbbbrbcbdbbrb"
assert a.replace("a","b",8) == "zzbbbbrbcbdbbrb"
assert a.replace("a","b",9) == "zzbbbbrbcbdbbrb"
assert a.replace("b","k",1) == "zzaaakracadabra"
assert a.replace("b","k",2) == "zzaaakracadakra"
assert a.replace("zza","yo",2) == "yoaabracadabra"
assert x.replace("", ",") == ",a,b,c,"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We next need support for compile-time strings in replace. For example,

% cat examples/expr2.py 
def main0():
    print("abc".replace("", ","))

main0()
% python examples/expr2.py 
,a,b,c,
% lpython examples/expr2.py
semantic error: 'str' object has no attribute 'replace'
 --> examples/expr2.py:2:11
  |
2 |     print("abc".replace("", ","))
  |           ^^^^^^^^^^^^^^^^^^^^^^ 


Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

I think this can be tackled in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Please let me know how to implement this. Also, is this done for all other functions?

Copy link
Collaborator

@ubaidsk ubaidsk Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search for "upper" in python_ast_to_asr.cpp and you will find the place(s) for compile-time implementation.

I would suggest supporting it in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks.



def check():
f()
test_str_concat()
Expand All @@ -127,5 +176,6 @@ def check():
test_constant_str_subscript()
test_str_title()
test_str_split()
test_str_replace()

check()
35 changes: 35 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,41 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
} else {
fn_args.push_back(al, str);
}
} else if(attr_name == "replace") {
if(!(args.size() == 2 || args.size()==3)) {
throw SemanticError("str.replace() takes two or three arguments.", 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);
if(args.size()==3){
ASR::expr_t *arg_value = args[2].m_value;
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value);
if (!ASRUtils::is_integer(*arg_value_type)) {
throw SemanticError("str.replace() argument 3 must be int", loc);
}
value.m_value = args[2].m_value;
fn_args.push_back(al, value);
}
} 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
1 change: 1 addition & 0 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ 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_swapcase", {m_builtin, &not_implemented}},
{"_lpython_str_startswith", {m_builtin, &not_implemented}},
{"_lpython_str_endswith", {m_builtin, &not_implemented}},
Expand Down
33 changes: 33 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,39 @@ 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:
return _lpython_str_replace(x, old, new, len(x))


@overload
def _lpython_str_replace(x: str, old:str, new:str, count: i32) -> str:
if (old == ""):
res1: str = ""
s: str
for s in x:
res1 += new + s
return res1 + new
res: str = ""
i: i32 = 0
ind: i32 = -1
l: i32 = len(new)
lo: i32 = len(old)
lx: i32 = len(x)
c: i32 = 0
t: i32 = -1

while(c<count):
t = _lpython_str_find(x[i:lx], old)
if(t==-1):
break
ind = i + t
res = res + x[i:ind] + new
i = ind + lo
c = c + 1
res = res + x[i:lx]
return res

@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": "7e3c68aa6acba27674e544f894bb141357db82f8840c756af448f5bb",
"stdout_hash": "f14010ffcf7d42b89e54c88ebecc7ca51d67b204825e07f4f708875e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading