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

Commit c1c9170

Browse files
author
Ehsan Totoni
committed
support string to int
1 parent e6abfa9 commit c1c9170

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

hpat/_str_ext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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);
1212
void* str_substr_int(std::string* str, int64_t index);
13+
int64_t str_to_int64(std::string* str);
1314

1415
PyMODINIT_FUNC PyInit_hstr_ext(void) {
1516
PyObject *m;
@@ -33,6 +34,8 @@ PyMODINIT_FUNC PyInit_hstr_ext(void) {
3334
PyLong_FromVoidPtr((void*)(&str_split)));
3435
PyObject_SetAttrString(m, "str_substr_int",
3536
PyLong_FromVoidPtr((void*)(&str_substr_int)));
37+
PyObject_SetAttrString(m, "str_to_int64",
38+
PyLong_FromVoidPtr((void*)(&str_to_int64)));
3639
return m;
3740
}
3841

@@ -95,3 +98,8 @@ void* str_substr_int(std::string* str, int64_t index)
9598
{
9699
return new std::string(*str, index, 1);
97100
}
101+
102+
int64_t str_to_int64(std::string* str)
103+
{
104+
return std::stoll(*str);
105+
}

hpat/str_ext.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numba
22
from numba.extending import (box, unbox, typeof_impl, register_model, models,
3-
NativeValue, lower_builtin)
3+
NativeValue, lower_builtin, lower_cast)
44
from numba.targets.imputils import lower_constant, impl_ret_new_ref
55
from numba import types, typing
66
from numba.typing.templates import (signature, AbstractTemplate, infer, infer_getattr,
@@ -59,6 +59,14 @@ def generic(self, args, kws):
5959
and isinstance(args[1], types.Integer)):
6060
return signature(args[0], *args)
6161

62+
@infer_global(int)
63+
class StrToInt(AbstractTemplate):
64+
def generic(self, args, kws):
65+
assert not kws
66+
[arg] = args
67+
if isinstance(arg, StringType):
68+
return signature(types.intp, arg)
69+
6270
import hstr_ext
6371
ll.add_symbol('init_string', hstr_ext.init_string)
6472
ll.add_symbol('init_string_const', hstr_ext.init_string_const)
@@ -67,6 +75,7 @@ def generic(self, args, kws):
6775
ll.add_symbol('str_equal', hstr_ext.str_equal)
6876
ll.add_symbol('str_split', hstr_ext.str_split)
6977
ll.add_symbol('str_substr_int', hstr_ext.str_substr_int)
78+
ll.add_symbol('str_to_int64', hstr_ext.str_to_int64)
7079

7180
@unbox(StringType)
7281
def unbox_string(typ, obj, c):
@@ -151,3 +160,9 @@ def getitem_string(context, builder, sig, args):
151160
# TODO: handle reference counting
152161
#return impl_ret_new_ref(builder.call(fn, args))
153162
return (builder.call(fn, args))
163+
164+
@lower_cast(StringType, types.int64)
165+
def dict_empty(context, builder, fromty, toty, val):
166+
fnty = lir.FunctionType(lir.IntType(64), [lir.IntType(8).as_pointer()])
167+
fn = builder.module.get_or_insert_function(fnty, name="str_to_int64")
168+
return builder.call(fn, (val,))

0 commit comments

Comments
 (0)