-
Notifications
You must be signed in to change notification settings - Fork 171
C: Fix negative string index #1138
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
Conversation
integration_tests/test_str_03.py
Outdated
# runtime tests | ||
k: i32 = len(a); i: i32 | ||
for i in range(k): | ||
assert a[-i-1] == a[k-i-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the same reason as discussed in the PR for negative list indices, we should not allow runtime negative string indices, because they can't be implemented in LLVM/C with high performance (that is, without an extra check for every index access).
We can however allow compile time negative value, because there is no runtime penalty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh okay, get it. I'll revert the last two commits.
src/libasr/codegen/asr_to_c.cpp
Outdated
@@ -806,8 +806,9 @@ R"( | |||
std::string idx = std::move(src); | |||
this->visit_expr(*x.m_arg); | |||
std::string str = std::move(src); | |||
idx = "((" + idx + " - 1) >= 0 ? (" + idx + " - 1) : (strlen(" + str + ") + " + idx + " - 1))"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change should not be done, as it slows down every index access, see above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Fixes #1124