Skip to content

Commit 2811722

Browse files
Merge pull request #434 from Abdullahjavednesar/str_neg_idx
Implementation for negative indexing in str
2 parents a1d726f + fe3844f commit 2811722

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

integration_tests/test_str_01.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def test_str_index():
2424
a: str
2525
a = "012345"
2626
assert a[2] == "2"
27+
assert a[-1] == "5"
28+
assert a[-6] == "0"
2729

2830
def check():
2931
f()

src/runtime/impure/lfortran_intrinsics.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ LFORTRAN_API void _lfortran_strcat(char** s1, char** s2, char** dest)
616616

617617
// idx1 and idx2 both start from 1
618618
LFORTRAN_API char* _lfortran_str_copy(char* s, int32_t idx1, int32_t idx2) {
619+
int s_len = strlen(s);
620+
if(idx1 <= 0) {
621+
idx1 = s_len + idx1;
622+
}
623+
if(idx2 <= 0) {
624+
idx2 = s_len + idx2;
625+
}
619626
char* dest_char = (char*)malloc(idx2-idx1+2);
620627
for (int i=idx1; i <= idx2; i++)
621628
{

0 commit comments

Comments
 (0)