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

Add join method in str #2354

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 16 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def test_str_repeat():
assert 3*a*3 == "XyzXyzXyzXyzXyzXyzXyzXyzXyz"
assert a*-1 == ""

def test_str_join():
a: str
a = ","
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a,b"
certik marked this conversation as resolved.
Show resolved Hide resolved

def test_str_join2():
a: str
a = "**"
p:list[str] = ["a","b"]
res:str = a.join(p)
assert res == "a**b"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can test more cases like empty strings, more items in the list (more than 3), and so on...
This can be done in the subsequent PRs.



def test_constant_str_subscript():
assert "abc"[2] == "c"
assert "abc"[:2] == "ab"
Expand All @@ -55,6 +70,7 @@ def check():
test_str_index()
test_str_slice()
test_str_repeat()
test_str_join()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's test test_str_join2

test_constant_str_subscript()

check()
17 changes: 16 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <lpython/semantics/python_intrinsic_eval.h>
#include <lpython/parser/parser.h>
#include <libasr/serialization.h>
#include <lpython/pickle.cpp>

Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved

namespace LCompilers::LPython {

Expand Down Expand Up @@ -1251,7 +1253,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs,
args, rt_subs, func, loc);
}

if (ASRUtils::get_FunctionType(func)->m_is_restriction) {
rt_vec.push_back(s);
} else if (ASRUtils::is_generic_function(s)) {
Expand Down Expand Up @@ -6664,6 +6665,20 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
arg.loc = loc;
arg.m_value = s_var;
fn_args.push_back(al, arg);
} else if (attr_name == "join") {
if (args.size() != 1) {
throw SemanticError("str.join() takes one argument",
loc);
}
fn_call_name = "_lpython_str_join";
ASR::call_arg_t str_var;
str_var.loc = loc;
str_var.m_value = s_var;
ASR::call_arg_t passed_int;
passed_int.loc = loc;
passed_int.m_value = args[0].m_value;
Comment on lines +6676 to +6678
Copy link
Collaborator

Choose a reason for hiding this comment

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

I just noticed this. Here, I think passed_int should be named list_of_str (or something similar). (@Agent-Hellboy I remember we were experimenting with it by passing an integer.)

I think you can fix the above in your new PR #2364. You can just make the rename a separate commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Go ahead and create an issue, so that we don't forget.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created #2365

fn_args.push_back(al, str_var);
fn_args.push_back(al, passed_int);
} else if (attr_name == "find") {
if (args.size() != 1) {
throw SemanticError("str.find() takes one argument",
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 @@ -80,6 +80,7 @@ struct PythonIntrinsicProcedures {
{"_lpython_str_count", {m_builtin, &not_implemented}},
{"_lpython_str_lower", {m_builtin, &not_implemented}},
{"_lpython_str_upper", {m_builtin, &not_implemented}},
{"_lpython_str_join", {m_builtin, &not_implemented}},
{"_lpython_str_find", {m_builtin, &not_implemented}},
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,14 @@ def _lpython_str_upper(x: str) -> str:
res += i
return res

@overload
def _lpython_str_join(s:str, lis:list[str]) -> str:
if len(lis) == 0: return ""
res:str = lis[0]
i:i32
for i in range(1, len(lis)):
res += s + lis[i]
return res

@overload
def _lpython_str_find(s: str, sub: str) -> i32:
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": "960bc68922ceaabf957cbdc2cfc76c1f4a4758856072d6be3ae9ed18",
"stdout_hash": "f1338c6f6e5f0d9e55addc9bd28498dde023b5870395fd4e097788d2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading