Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98e53f4

Browse files
authored
Merge pull request #2354 from Agent-Hellboy/add_join_in_str
Add join method in str
2 parents d78559b + a4183b3 commit 98e53f4

File tree

66 files changed

+1300
-1261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1300
-1261
lines changed

integration_tests/test_str_01.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ def test_str_repeat():
4545
assert 3*a*3 == "XyzXyzXyzXyzXyzXyzXyzXyzXyz"
4646
assert a*-1 == ""
4747

48+
def test_str_join():
49+
a: str
50+
a = ","
51+
p:list[str] = ["a","b"]
52+
res:str = a.join(p)
53+
assert res == "a,b"
54+
55+
def test_str_join2():
56+
a: str
57+
a = "**"
58+
p:list[str] = ["a","b"]
59+
res:str = a.join(p)
60+
assert res == "a**b"
61+
62+
4863
def test_constant_str_subscript():
4964
assert "abc"[2] == "c"
5065
assert "abc"[:2] == "ab"
@@ -55,6 +70,7 @@ def check():
5570
test_str_index()
5671
test_str_slice()
5772
test_str_repeat()
73+
test_str_join()
5874
test_constant_str_subscript()
5975

6076
check()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <lpython/parser/parser.h>
3535
#include <libasr/serialization.h>
3636

37+
3738
namespace LCompilers::LPython {
3839

3940
namespace CastingUtil {
@@ -1251,7 +1252,6 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
12511252
visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs,
12521253
args, rt_subs, func, loc);
12531254
}
1254-
12551255
if (ASRUtils::get_FunctionType(func)->m_is_restriction) {
12561256
rt_vec.push_back(s);
12571257
} else if (ASRUtils::is_generic_function(s)) {
@@ -6664,6 +6664,20 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
66646664
arg.loc = loc;
66656665
arg.m_value = s_var;
66666666
fn_args.push_back(al, arg);
6667+
} else if (attr_name == "join") {
6668+
if (args.size() != 1) {
6669+
throw SemanticError("str.join() takes one argument",
6670+
loc);
6671+
}
6672+
fn_call_name = "_lpython_str_join";
6673+
ASR::call_arg_t str_var;
6674+
str_var.loc = loc;
6675+
str_var.m_value = s_var;
6676+
ASR::call_arg_t passed_int;
6677+
passed_int.loc = loc;
6678+
passed_int.m_value = args[0].m_value;
6679+
fn_args.push_back(al, str_var);
6680+
fn_args.push_back(al, passed_int);
66676681
} else if (attr_name == "find") {
66686682
if (args.size() != 1) {
66696683
throw SemanticError("str.find() takes one argument",

src/lpython/semantics/python_comptime_eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct PythonIntrinsicProcedures {
8080
{"_lpython_str_count", {m_builtin, &not_implemented}},
8181
{"_lpython_str_lower", {m_builtin, &not_implemented}},
8282
{"_lpython_str_upper", {m_builtin, &not_implemented}},
83+
{"_lpython_str_join", {m_builtin, &not_implemented}},
8384
{"_lpython_str_find", {m_builtin, &not_implemented}},
8485
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
8586
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},

src/runtime/lpython_builtin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,14 @@ def _lpython_str_upper(x: str) -> str:
763763
res += i
764764
return res
765765

766+
@overload
767+
def _lpython_str_join(s:str, lis:list[str]) -> str:
768+
if len(lis) == 0: return ""
769+
res:str = lis[0]
770+
i:i32
771+
for i in range(1, len(lis)):
772+
res += s + lis[i]
773+
return res
766774

767775
@overload
768776
def _lpython_str_find(s: str, sub: str) -> i32:

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "960bc68922ceaabf957cbdc2cfc76c1f4a4758856072d6be3ae9ed18",
9+
"stdout_hash": "f1338c6f6e5f0d9e55addc9bd28498dde023b5870395fd4e097788d2",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)