Skip to content

Fix string indexing #273

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

Closed
certik opened this issue Mar 22, 2022 · 4 comments · Fixed by #423
Closed

Fix string indexing #273

certik opened this issue Mar 22, 2022 · 4 comments · Fixed by #423

Comments

@certik
Copy link
Contributor

certik commented Mar 22, 2022

The following code:

def f():
    s: str
    s = "abcdefg"
    print(s[1]) # prints "bcdefg", but should print "b"
    print(s[1:2]) # prints "cdefg", but should print "b"
    print(s[1:3]) # prints "defg", but should print "bc"

f()

Prints (correctly) using CPython:

$ python a.py 
b
b
bc

But with LPython it prints:

$ lpython a.py && ./a.out 
bcdefg
cdefg
defg

We have to fix it.

@namannimmo10
Copy link
Collaborator

C++ code for reference:

#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <cmath>
#include <Kokkos_Core.hpp>
#include <lfortran_intrinsics.h>

template <typename T>
Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
{
    Kokkos::View<T*> r("r", v.size());
    for (size_t i=0; i < v.size(); i++) {
        r(i) = v[i];
    }
    return r;
}

void _lpython_main_program()
{
    f();
}

void f()
{
    std::string s;
    s = "abcdefg";
    std::cout << s[1 + 1-1] << std::endl;
    std::cout << s[2 + 1-1] << std::endl;
    std::cout << s[3 + 1-1] << std::endl;
}

namespace {

void main2() {
        _lpython_main_program();
}

}
int main(int argc, char* argv[])
{
    Kokkos::initialize(argc, argv);
    main2();
    Kokkos::finalize();
    return 0;
}

@namannimmo10
Copy link
Collaborator

We should improve it as well.

@certik certik mentioned this issue Mar 23, 2022
9 tasks
@namannimmo10
Copy link
Collaborator

I guess the problem is here:

if (l == r) {
this->visit_expr_wrapper(x.m_args[0].m_left, true);
llvm::Value *idx = tmp;
idx = builder->CreateSub(idx, llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
//std::vector<llvm::Value*> idx_vec = {llvm::ConstantInt::get(context, llvm::APInt(32, 0)), idx};
std::vector<llvm::Value*> idx_vec = {idx};
llvm::Value *str = builder->CreateLoad(array);
llvm::Value *p = builder->CreateGEP(str, idx_vec);
// TODO: Currently the string starts at the right location, but goes to the end of the original string.
// We have to allocate a new string, copy it and add null termination.
tmp = builder->CreateAlloca(character_type, nullptr);
builder->CreateStore(p, tmp);
//tmp = p;

@Smit-create
Copy link
Collaborator

This issue might be easier to handle once we move forward with: https://gitlab.com/lfortran/lfortran/-/merge_requests/1700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants