Skip to content

Commit 77d2bb3

Browse files
Adding str slicing
1 parent c251f64 commit 77d2bb3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

integration_tests/test_str_01.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ def test_str_index():
2525
a = "012345"
2626
assert a[2] == "2"
2727

28+
def test_str_slice():
29+
a: str
30+
a = "012345"
31+
assert a[2:4] == "234"
32+
2833
def check():
2934
f()
3035
test_str_concat()
3136
test_str_index()
37+
test_str_slice()
3238

3339
check()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,15 +1079,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
10791079
//throw CodeGenError("Only string(a:b) for a,b variables for now.", x.base.base.loc);
10801080
// Use the "right" index for now
10811081
this->visit_expr_wrapper(x.m_args[0].m_right, true);
1082-
llvm::Value *idx = tmp;
1082+
llvm::Value *idx2 = tmp;
1083+
this->visit_expr_wrapper(x.m_args[0].m_left, true);
1084+
llvm::Value *idx1 = tmp;
10831085
// idx = builder->CreateSub(idx, llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
10841086
//std::vector<llvm::Value*> idx_vec = {llvm::ConstantInt::get(context, llvm::APInt(32, 0)), idx};
10851087
// std::vector<llvm::Value*> idx_vec = {idx};
10861088
llvm::Value *str = CreateLoad(array);
10871089
// llvm::Value *p = CreateGEP(str, idx_vec);
10881090
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
10891091
// We have to allocate a new string, copy it and add null termination.
1090-
llvm::Value *p = lfortran_str_copy(str, idx, idx);
1092+
llvm::Value *p = lfortran_str_copy(str, idx1, idx2);
10911093

10921094
tmp = builder->CreateAlloca(character_type, nullptr);
10931095
builder->CreateStore(p, tmp);

0 commit comments

Comments
 (0)