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 1 commit
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
9 changes: 9 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ 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_constant_str_subscript():
assert "abc"[2] == "c"
assert "abc"[:2] == "ab"
Expand All @@ -55,6 +63,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 @@ -764,6 +764,14 @@ def _lpython_str_upper(x: str) -> str:
return res


def _lpython_str_join(s:str, lis:list[str]) -> str:
res:str = ""
i:i32
for i in range(len(lis)):
Copy link
Contributor

@OculusMode OculusMode Oct 2, 2023

Choose a reason for hiding this comment

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

Can be directly iterated on the list itself, this way it can be worked on iterables easily(in future). Every iterable can be called using next so I think that would be more better.

something like this can be more cleaner I think.

for ele in range(list_):
    res+=ele

Copy link
Contributor

Choose a reason for hiding this comment

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

If it works, we can use the simpler syntax. LPython doesn't yet work for user defined iterables, but eventually it will I think.

res+=s
res+=lis[i]
return res[1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

thats not reliable. You are always assuming your str len will be 1 here, It should fail when str has len more than 1. You can prob use res[len(str):]

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! I think even better would be something like:

if len(lis) == 0: return ""
res = lis[0]
for i in range(1, len(lis)):
    res += s + lis[i]
return res

Copy link
Contributor

Choose a reason for hiding this comment

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

That works too, altho as long as there plans of iterables, part of len will probably be removed regardless.


@overload
def _lpython_str_find(s: str, sub: str) -> i32:
s_len :i32; sub_len :i32; flag: bool; _len: i32;
Expand Down