Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit c10d31d

Browse files
author
Ehsan Totoni
committed
string getitem char
1 parent ecb1cf6 commit c10d31d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

hpat/_str_ext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const char* get_c_str(std::string* s);
99
void* str_concat(std::string* s1, std::string* s2);
1010
bool str_equal(std::string* s1, std::string* s2);
1111
void* str_split(std::string* str, std::string* sep, int64_t *size);
12+
void* str_substr_int(std::string* str, int64_t index);
1213

1314
PyMODINIT_FUNC PyInit_hstr_ext(void) {
1415
PyObject *m;
@@ -30,6 +31,8 @@ PyMODINIT_FUNC PyInit_hstr_ext(void) {
3031
PyLong_FromVoidPtr((void*)(&str_equal)));
3132
PyObject_SetAttrString(m, "str_split",
3233
PyLong_FromVoidPtr((void*)(&str_split)));
34+
PyObject_SetAttrString(m, "str_substr_int",
35+
PyLong_FromVoidPtr((void*)(&str_substr_int)));
3336
return m;
3437
}
3538

@@ -87,3 +90,8 @@ void* str_split(std::string* str, std::string* sep, int64_t *size)
8790
// std::cout<< *(((std::string**)(out))[1])<<std::endl;
8891
return out;
8992
}
93+
94+
void* str_substr_int(std::string* str, int64_t index)
95+
{
96+
return new std::string(*str, index, 1);
97+
}

hpat/str_ext.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@ def resolve_split(self, dict, args, kws):
4949
assert len(args) == 1
5050
return signature(types.List(string_type), *args)
5151

52+
@infer
53+
class GetItemString(AbstractTemplate):
54+
key = "getitem"
55+
56+
def generic(self, args, kws):
57+
assert not kws
58+
if (len(args) == 2 and isinstance(args[0], StringType)
59+
and isinstance(args[1], types.Integer)):
60+
return signature(args[0], *args)
61+
5262
import hstr_ext
5363
ll.add_symbol('init_string', hstr_ext.init_string)
5464
ll.add_symbol('init_string_const', hstr_ext.init_string_const)
5565
ll.add_symbol('get_c_str', hstr_ext.get_c_str)
5666
ll.add_symbol('str_concat', hstr_ext.str_concat)
5767
ll.add_symbol('str_equal', hstr_ext.str_equal)
5868
ll.add_symbol('str_split', hstr_ext.str_split)
69+
ll.add_symbol('str_substr_int', hstr_ext.str_substr_int)
5970

6071
@unbox(StringType)
6172
def unbox_string(typ, obj, c):
@@ -131,3 +142,12 @@ def string_split_impl(context, builder, sig, args):
131142
value = builder.load(cgutils.gep_inbounds(builder, ptr, loop.index))
132143
_list.setitem(loop.index, value)
133144
return impl_ret_new_ref(context, builder, sig.return_type, _list.value)
145+
146+
@lower_builtin('getitem', StringType, types.Integer)
147+
def getitem_string(context, builder, sig, args):
148+
fnty = lir.FunctionType(lir.IntType(8).as_pointer(),
149+
[lir.IntType(8).as_pointer(), lir.IntType(64)])
150+
fn = builder.module.get_or_insert_function(fnty, name="str_substr_int")
151+
# TODO: handle reference counting
152+
#return impl_ret_new_ref(builder.call(fn, args))
153+
return (builder.call(fn, args))

0 commit comments

Comments
 (0)