forked from synapticarbors/rust-cython-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cytest.pyx
71 lines (59 loc) · 2.01 KB
/
cytest.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from cpython cimport PyUnicode_AsUTF8String, PyUnicode_InternFromString
cdef extern from "string.h":
void *memcpy(void *dest, void *src, size_t n)
cdef extern from "stdio.h":
int toupper(int c)
import numpy as np
cimport numpy as np
import cython
cdef extern:
char *my_upper(char *b)
int init()
def call_init():
return bool(init())
def call_rust_upper(unicode s):
cdef unicode out
return PyUnicode_InternFromString(my_upper(PyUnicode_AsUTF8String(s)))
@cython.boundscheck(False)
@cython.wraparound(False)
cdef do_array_upper(np.ndarray[np.int64_t, ndim=1] starts,
np.ndarray[np.int64_t, ndim=1] stops,
np.ndarray[np.uint8_t, ndim=1] content,
np.ndarray[np.uint8_t, ndim=1] out):
cdef char* word
cdef char* uword
cdef int start, stop
assert content.flags['C_CONTIGUOUS']
assert out.flags['C_CONTIGUOUS']
l = len(starts)
cdef int i = 0
while i < l:
i += 1
start = starts[i]
stop = stops[i]
word = <char*> (<void*> (content.data + start))
uword = my_upper(word)
memcpy((<void*> out.data) + start, <void *> uword, stop - start)
@cython.boundscheck(False)
@cython.wraparound(False)
cdef do_carray_upper(np.ndarray[np.int64_t, ndim=1] starts,
np.ndarray[np.int64_t, ndim=1] stops,
np.ndarray[np.uint8_t, ndim=1] content,
np.ndarray[np.uint8_t, ndim=1] out):
cdef char* word
cdef int start, stop
cdef int l = starts.shape[0]
cdef int i, j = 0
while i < l:
start = starts[i]
stop = stops[i]
word = <char*> (<void*> (content.data + start))
while start <= stop:
out.data[start] = <char>toupper(<int>content.data[start])
start += 1
i += 1
def array_upper(arr):
out = arr.empty_like()
#do_array_upper(arr.starts, arr.stops, arr.content, out.content)
do_carray_upper(arr.starts, arr.stops, arr.content, out.content)
return out