-
Notifications
You must be signed in to change notification settings - Fork 171
Add join method in str #2354
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
def test_str_join2(): | ||
a: str | ||
a = "**" | ||
p:list[str] = ["a","b"] | ||
res:str = a.join(p) | ||
assert res == "a**b" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... |
||
|
||
|
||
def test_constant_str_subscript(): | ||
assert "abc"[2] == "c" | ||
assert "abc"[:2] == "ab" | ||
|
@@ -55,6 +70,7 @@ def check(): | |
test_str_index() | ||
test_str_slice() | ||
test_str_repeat() | ||
test_str_join() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's test |
||
test_constant_str_subscript() | ||
|
||
check() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
#include <lpython/parser/parser.h> | ||
#include <libasr/serialization.h> | ||
|
||
|
||
namespace LCompilers::LPython { | ||
|
||
namespace CastingUtil { | ||
|
@@ -1251,7 +1252,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)) { | ||
|
@@ -6664,6 +6664,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed this. Here, I think I think you can fix the above in your new PR #2364. You can just make the rename a separate commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go ahead and create an issue, so that we don't forget. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
Uh oh!
There was an error while loading. Please reload this page.