-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
stringdtype_ufuncs.cpp
2814 lines (2424 loc) · 91.3 KB
/
stringdtype_ufuncs.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Ufunc implementations for the StringDType class */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#define _UMATHMODULE
#include "numpy/arrayobject.h"
#include "numpy/ndarraytypes.h"
#include "numpy/npy_math.h"
#include "numpy/ufuncobject.h"
#include "numpyos.h"
#include "gil_utils.h"
#include "dtypemeta.h"
#include "abstractdtypes.h"
#include "dispatching.h"
#include "string_ufuncs.h"
#include "stringdtype_ufuncs.h"
#include "string_buffer.h"
#include "string_fastsearch.h"
#include "templ_common.h" /* for npy_mul_size_with_overflow_size_t */
#include "stringdtype/static_string.h"
#include "stringdtype/dtype.h"
#include "stringdtype/utf8_utils.h"
#define LOAD_TWO_INPUT_STRINGS(CONTEXT) \
const npy_packed_static_string *ps1 = (npy_packed_static_string *)in1; \
npy_static_string s1 = {0, NULL}; \
int s1_isnull = NpyString_load(s1allocator, ps1, &s1); \
const npy_packed_static_string *ps2 = (npy_packed_static_string *)in2; \
npy_static_string s2 = {0, NULL}; \
int s2_isnull = NpyString_load(s2allocator, ps2, &s2); \
if (s1_isnull == -1 || s2_isnull == -1) { \
npy_gil_error(PyExc_MemoryError, "Failed to load string in %s", \
CONTEXT); \
goto fail; \
} \
static NPY_CASTING
multiply_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const dtypes[], PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
{
PyArray_Descr *ldescr = given_descrs[0];
PyArray_Descr *rdescr = given_descrs[1];
PyArray_StringDTypeObject *odescr = NULL;
PyArray_Descr *out_descr = NULL;
if (dtypes[0] == &PyArray_StringDType) {
odescr = (PyArray_StringDTypeObject *)ldescr;
}
else {
odescr = (PyArray_StringDTypeObject *)rdescr;
}
if (given_descrs[2] == NULL) {
out_descr = (PyArray_Descr *)new_stringdtype_instance(
odescr->na_object, odescr->coerce);
if (out_descr == NULL) {
return (NPY_CASTING)-1;
}
}
else {
Py_INCREF(given_descrs[2]);
out_descr = given_descrs[2];
}
Py_INCREF(ldescr);
loop_descrs[0] = ldescr;
Py_INCREF(rdescr);
loop_descrs[1] = rdescr;
loop_descrs[2] = out_descr;
return NPY_NO_CASTING;
}
template <typename T>
static int multiply_loop_core(
npy_intp N, char *sin, char *iin, char *out,
npy_intp s_stride, npy_intp i_stride, npy_intp o_stride,
PyArray_StringDTypeObject *idescr, PyArray_StringDTypeObject *odescr)
{
PyArray_Descr *descrs[2] =
{(PyArray_Descr *)idescr, (PyArray_Descr *)odescr};
npy_string_allocator *allocators[2] = {};
NpyString_acquire_allocators(2, descrs, allocators);
npy_string_allocator *iallocator = allocators[0];
npy_string_allocator *oallocator = allocators[1];
int has_null = idescr->na_object != NULL;
int has_nan_na = idescr->has_nan_na;
int has_string_na = idescr->has_string_na;
const npy_static_string *default_string = &idescr->default_string;
while (N--) {
const npy_packed_static_string *ips =
(npy_packed_static_string *)sin;
npy_static_string is = {0, NULL};
npy_packed_static_string *ops = (npy_packed_static_string *)out;
int is_isnull = NpyString_load(iallocator, ips, &is);
if (is_isnull == -1) {
npy_gil_error(PyExc_MemoryError,
"Failed to load string in multiply");
goto fail;
}
else if (is_isnull) {
if (has_nan_na) {
if (NpyString_pack_null(oallocator, ops) < 0) {
npy_gil_error(PyExc_MemoryError,
"Failed to deallocate string in multiply");
goto fail;
}
sin += s_stride;
iin += i_stride;
out += o_stride;
continue;
}
else if (has_string_na || !has_null) {
is = *(npy_static_string *)default_string;
}
else {
npy_gil_error(PyExc_TypeError,
"Cannot multiply null that is not a nan-like "
"value");
goto fail;
}
}
T factor = *(T *)iin;
size_t cursize = is.size;
size_t newsize;
int overflowed = npy_mul_with_overflow_size_t(
&newsize, cursize, factor);
if (overflowed) {
npy_gil_error(PyExc_MemoryError,
"Failed to allocate string in string multiply");
goto fail;
}
char *buf = NULL;
npy_static_string os = {0, NULL};
// in-place
if (descrs[0] == descrs[1]) {
buf = (char *)PyMem_RawMalloc(newsize);
if (buf == NULL) {
npy_gil_error(PyExc_MemoryError,
"Failed to allocate string in multiply");
goto fail;
}
}
else {
if (load_new_string(
ops, &os, newsize,
oallocator, "multiply") == -1) {
goto fail;
}
/* explicitly discard const; initializing new buffer */
buf = (char *)os.buf;
}
for (size_t i = 0; i < (size_t)factor; i++) {
/* multiply can't overflow because cursize * factor */
/* has already been checked and doesn't overflow */
memcpy((char *)buf + i * cursize, is.buf, cursize);
}
// clean up temp buffer for in-place operations
if (descrs[0] == descrs[1]) {
if (NpyString_pack(oallocator, ops, buf, newsize) < 0) {
npy_gil_error(PyExc_MemoryError,
"Failed to pack string in multiply");
goto fail;
}
PyMem_RawFree(buf);
}
sin += s_stride;
iin += i_stride;
out += o_stride;
}
NpyString_release_allocators(2, allocators);
return 0;
fail:
NpyString_release_allocators(2, allocators);
return -1;
}
template <typename T>
static int multiply_right_strided_loop(
PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_StringDTypeObject *idescr =
(PyArray_StringDTypeObject *)context->descriptors[0];
PyArray_StringDTypeObject *odescr =
(PyArray_StringDTypeObject *)context->descriptors[2];
npy_intp N = dimensions[0];
char *sin = data[0];
char *iin = data[1];
char *out = data[2];
npy_intp in1_stride = strides[0];
npy_intp in2_stride = strides[1];
npy_intp out_stride = strides[2];
return multiply_loop_core<T>(
N, sin, iin, out, in1_stride, in2_stride, out_stride,
idescr, odescr);
}
template <typename T>
static int multiply_left_strided_loop(
PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_StringDTypeObject *idescr =
(PyArray_StringDTypeObject *)context->descriptors[1];
PyArray_StringDTypeObject *odescr =
(PyArray_StringDTypeObject *)context->descriptors[2];
npy_intp N = dimensions[0];
char *iin = data[0];
char *sin = data[1];
char *out = data[2];
npy_intp in1_stride = strides[0];
npy_intp in2_stride = strides[1];
npy_intp out_stride = strides[2];
return multiply_loop_core<T>(
N, sin, iin, out, in2_stride, in1_stride, out_stride,
idescr, odescr);
}
static NPY_CASTING
binary_resolve_descriptors(struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[],
npy_intp *NPY_UNUSED(view_offset))
{
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0];
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1];
int out_coerce = descr1->coerce && descr1->coerce;
PyObject *out_na_object = NULL;
if (stringdtype_compatible_na(
descr1->na_object, descr2->na_object, &out_na_object) == -1) {
return (NPY_CASTING)-1;
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
PyArray_Descr *out_descr = NULL;
if (given_descrs[2] == NULL) {
out_descr = (PyArray_Descr *)new_stringdtype_instance(
out_na_object, out_coerce);
if (out_descr == NULL) {
return (NPY_CASTING)-1;
}
}
else {
Py_INCREF(given_descrs[2]);
out_descr = given_descrs[2];
}
loop_descrs[2] = out_descr;
return NPY_NO_CASTING;
}
static int
add_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_StringDTypeObject *s1descr = (PyArray_StringDTypeObject *)context->descriptors[0];
PyArray_StringDTypeObject *s2descr = (PyArray_StringDTypeObject *)context->descriptors[1];
PyArray_StringDTypeObject *odescr = (PyArray_StringDTypeObject *)context->descriptors[2];
int has_null = s1descr->na_object != NULL;
int has_nan_na = s1descr->has_nan_na;
int has_string_na = s1descr->has_string_na;
const npy_static_string *default_string = &s1descr->default_string;
npy_intp N = dimensions[0];
char *in1 = data[0];
char *in2 = data[1];
char *out = data[2];
npy_intp in1_stride = strides[0];
npy_intp in2_stride = strides[1];
npy_intp out_stride = strides[2];
npy_string_allocator *allocators[3] = {};
NpyString_acquire_allocators(3, context->descriptors, allocators);
npy_string_allocator *s1allocator = allocators[0];
npy_string_allocator *s2allocator = allocators[1];
npy_string_allocator *oallocator = allocators[2];
while (N--) {
LOAD_TWO_INPUT_STRINGS("add")
char *buf = NULL;
npy_static_string os = {0, NULL};
size_t newsize = 0;
npy_packed_static_string *ops = (npy_packed_static_string *)out;
if (NPY_UNLIKELY(s1_isnull || s2_isnull)) {
if (has_nan_na) {
if (NpyString_pack_null(oallocator, ops) < 0) {
npy_gil_error(PyExc_MemoryError,
"Failed to deallocate string in add");
goto fail;
}
goto next_step;
}
else if (has_string_na || !has_null) {
if (s1_isnull) {
s1 = *default_string;
}
if (s2_isnull) {
s2 = *default_string;
}
}
else {
npy_gil_error(PyExc_ValueError,
"Cannot add null that is not a nan-like value");
goto fail;
}
}
// check for overflow
newsize = s1.size + s2.size;
if (newsize < s1.size) {
npy_gil_error(PyExc_MemoryError, "Failed to allocate string in add");
goto fail;
}
// in-place
if (s1descr == odescr || s2descr == odescr) {
buf = (char *)PyMem_RawMalloc(newsize);
if (buf == NULL) {
npy_gil_error(PyExc_MemoryError,
"Failed to allocate string in add");
goto fail;
}
}
else {
if (load_new_string(ops, &os, newsize, oallocator, "add") == -1) {
goto fail;
}
// explicitly discard const; initializing new buffer
buf = (char *)os.buf;
}
memcpy(buf, s1.buf, s1.size);
memcpy(buf + s1.size, s2.buf, s2.size);
// clean up temporary in-place buffer
if (s1descr == odescr || s2descr == odescr) {
if (NpyString_pack(oallocator, ops, buf, newsize) < 0) {
npy_gil_error(PyExc_MemoryError,
"Failed to pack output string in add");
goto fail;
}
PyMem_RawFree(buf);
}
next_step:
in1 += in1_stride;
in2 += in2_stride;
out += out_stride;
}
NpyString_release_allocators(3, allocators);
return 0;
fail:
NpyString_release_allocators(3, allocators);
return -1;
}
static int
minimum_maximum_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
const char *ufunc_name = ((PyUFuncObject *)context->caller)->name;
npy_bool invert = *(npy_bool *)context->method->static_data; // true for maximum
PyArray_StringDTypeObject *in1_descr =
((PyArray_StringDTypeObject *)context->descriptors[0]);
PyArray_StringDTypeObject *in2_descr =
((PyArray_StringDTypeObject *)context->descriptors[1]);
npy_intp N = dimensions[0];
char *in1 = data[0];
char *in2 = data[1];
char *out = data[2];
npy_intp in1_stride = strides[0];
npy_intp in2_stride = strides[1];
npy_intp out_stride = strides[2];
npy_string_allocator *allocators[3] = {};
NpyString_acquire_allocators(3, context->descriptors, allocators);
npy_string_allocator *in1_allocator = allocators[0];
npy_string_allocator *in2_allocator = allocators[1];
npy_string_allocator *out_allocator = allocators[2];
while (N--) {
const npy_packed_static_string *sin1 = (npy_packed_static_string *)in1;
const npy_packed_static_string *sin2 = (npy_packed_static_string *)in2;
npy_packed_static_string *sout = (npy_packed_static_string *)out;
int cmp = _compare(in1, in2, in1_descr, in2_descr);
if (cmp == 0 && (in1 == out || in2 == out)) {
goto next_step;
}
if ((cmp < 0) ^ invert) {
// if in and out are the same address, do nothing to avoid a
// use-after-free
if (in1 != out) {
if (free_and_copy(in1_allocator, out_allocator, sin1, sout,
ufunc_name) == -1) {
goto fail;
}
}
}
else {
if (in2 != out) {
if (free_and_copy(in2_allocator, out_allocator, sin2, sout,
ufunc_name) == -1) {
goto fail;
}
}
}
next_step:
in1 += in1_stride;
in2 += in2_stride;
out += out_stride;
}
NpyString_release_allocators(3, allocators);
return 0;
fail:
NpyString_release_allocators(3, allocators);
return -1;
}
static int
string_comparison_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
const char *ufunc_name = ((PyUFuncObject *)context->caller)->name;
npy_bool res_for_eq = ((npy_bool *)context->method->static_data)[0];
npy_bool res_for_lt = ((npy_bool *)context->method->static_data)[1];
npy_bool res_for_gt = ((npy_bool *)context->method->static_data)[2];
npy_bool res_for_ne = !res_for_eq;
npy_bool eq_or_ne = res_for_lt == res_for_gt;
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)context->descriptors[0];
int has_null = descr1->na_object != NULL;
int has_nan_na = descr1->has_nan_na;
int has_string_na = descr1->has_string_na;
const npy_static_string *default_string = &descr1->default_string;
npy_intp N = dimensions[0];
char *in1 = data[0];
char *in2 = data[1];
npy_bool *out = (npy_bool *)data[2];
npy_intp in1_stride = strides[0];
npy_intp in2_stride = strides[1];
npy_intp out_stride = strides[2];
npy_string_allocator *allocators[2] = {};
NpyString_acquire_allocators(2, context->descriptors, allocators);
npy_string_allocator *s1allocator = allocators[0];
npy_string_allocator *s2allocator = allocators[1];
while (N--) {
int cmp;
LOAD_TWO_INPUT_STRINGS(ufunc_name);
if (NPY_UNLIKELY(s1_isnull || s2_isnull)) {
if (has_nan_na) {
// s1 or s2 is NA
*out = NPY_FALSE;
goto next_step;
}
else if (has_null && !has_string_na) {
if (eq_or_ne) {
if (s1_isnull && s2_isnull) {
*out = res_for_eq;
}
else {
*out = res_for_ne;
}
}
else {
npy_gil_error(PyExc_ValueError,
"'%s' not supported for null values that are not "
"nan-like or strings.", ufunc_name);
goto fail;
}
}
else {
if (s1_isnull) {
s1 = *default_string;
}
if (s2_isnull) {
s2 = *default_string;
}
}
}
cmp = NpyString_cmp(&s1, &s2);
if (cmp == 0) {
*out = res_for_eq;
}
else if (cmp < 0) {
*out = res_for_lt;
}
else {
*out = res_for_gt;
}
next_step:
in1 += in1_stride;
in2 += in2_stride;
out += out_stride;
}
NpyString_release_allocators(2, allocators);
return 0;
fail:
NpyString_release_allocators(2, allocators);
return -1;
}
static NPY_CASTING
string_comparison_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
{
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0];
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1];
if (stringdtype_compatible_na(descr1->na_object, descr2->na_object, NULL) == -1) {
return (NPY_CASTING)-1;
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
loop_descrs[2] = PyArray_DescrFromType(NPY_BOOL); // cannot fail
return NPY_NO_CASTING;
}
static int
string_isnan_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)context->descriptors[0];
int has_nan_na = descr->has_nan_na;
npy_intp N = dimensions[0];
char *in = data[0];
npy_bool *out = (npy_bool *)data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
while (N--) {
const npy_packed_static_string *s = (npy_packed_static_string *)in;
if (has_nan_na && NpyString_isnull(s)) {
*out = NPY_TRUE;
}
else {
*out = NPY_FALSE;
}
in += in_stride;
out += out_stride;
}
return 0;
}
static NPY_CASTING
string_bool_output_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
{
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
loop_descrs[1] = PyArray_DescrFromType(NPY_BOOL); // cannot fail
return NPY_NO_CASTING;
}
static NPY_CASTING
string_intp_output_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[], npy_intp *NPY_UNUSED(view_offset))
{
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
loop_descrs[1] = PyArray_DescrFromType(NPY_INTP); // cannot fail
return NPY_NO_CASTING;
}
using utf8_buffer_method = bool (Buffer<ENCODING::UTF8>::*)();
static int
string_bool_output_unary_strided_loop(
PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
const char *ufunc_name = ((PyUFuncObject *)context->caller)->name;
utf8_buffer_method is_it = *(utf8_buffer_method *)(context->method->static_data);
PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)context->descriptors[0];
npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
int has_string_na = descr->has_string_na;
int has_nan_na = descr->has_nan_na;
const npy_static_string *default_string = &descr->default_string;
npy_intp N = dimensions[0];
char *in = data[0];
char *out = data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
while (N--) {
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
npy_static_string s = {0, NULL};
const char *buffer = NULL;
size_t size = 0;
Buffer<ENCODING::UTF8> buf;
int is_null = NpyString_load(allocator, ps, &s);
if (is_null == -1) {
npy_gil_error(PyExc_MemoryError, "Failed to load string in %s", ufunc_name);
goto fail;
}
if (is_null) {
if (has_nan_na) {
// NA values are always falsy
*out = NPY_FALSE;
goto next_step;
}
else if (!has_string_na) {
npy_gil_error(PyExc_ValueError,
"Cannot use the %s function with a null that is "
"not a nan-like value", ufunc_name);
goto fail;
}
buffer = default_string->buf;
size = default_string->size;
}
else {
buffer = s.buf;
size = s.size;
}
buf = Buffer<ENCODING::UTF8>((char *)buffer, size);
*(npy_bool *)out = (buf.*is_it)();
next_step:
in += in_stride;
out += out_stride;
}
NpyString_release_allocator(allocator);
return 0;
fail:
NpyString_release_allocator(allocator);
return -1;
}
static int
string_strlen_strided_loop(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *auxdata)
{
PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)context->descriptors[0];
npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
int has_string_na = descr->has_string_na;
const npy_static_string *default_string = &descr->default_string;
npy_intp N = dimensions[0];
char *in = data[0];
char *out = data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
while (N--) {
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
npy_static_string s = {0, NULL};
const char *buffer = NULL;
size_t size = 0;
Buffer<ENCODING::UTF8> buf;
int is_null = NpyString_load(allocator, ps, &s);
if (is_null == -1) {
npy_gil_error(PyExc_MemoryError, "Failed to load string in str_len");
goto fail;
}
if (is_null) {
if (!has_string_na) {
npy_gil_error(PyExc_ValueError,
"The length of a null string is undefined");
goto next_step;
}
buffer = default_string->buf;
size = default_string->size;
}
else {
buffer = s.buf;
size = s.size;
}
buf = Buffer<ENCODING::UTF8>((char *)buffer, size);
*(npy_intp *)out = buf.num_codepoints();
next_step:
in += in_stride;
out += out_stride;
}
NpyString_release_allocator(allocator);
return 0;
fail:
NpyString_release_allocator(allocator);
return -1;
}
static int
string_findlike_promoter(PyObject *NPY_UNUSED(ufunc),
PyArray_DTypeMeta *const op_dtypes[],
PyArray_DTypeMeta *const signature[],
PyArray_DTypeMeta *new_op_dtypes[])
{
new_op_dtypes[0] = NPY_DT_NewRef(&PyArray_StringDType);
new_op_dtypes[1] = NPY_DT_NewRef(&PyArray_StringDType);
new_op_dtypes[2] = NPY_DT_NewRef(&PyArray_Int64DType);
new_op_dtypes[3] = NPY_DT_NewRef(&PyArray_Int64DType);
new_op_dtypes[4] = PyArray_DTypeFromTypeNum(NPY_DEFAULT_INT);
return 0;
}
static NPY_CASTING
string_findlike_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[],
npy_intp *NPY_UNUSED(view_offset))
{
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0];
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1];
if (stringdtype_compatible_na(descr1->na_object, descr2->na_object, NULL) == -1) {
return (NPY_CASTING)-1;
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
Py_INCREF(given_descrs[2]);
loop_descrs[2] = given_descrs[2];
Py_INCREF(given_descrs[3]);
loop_descrs[3] = given_descrs[3];
if (given_descrs[4] == NULL) {
loop_descrs[4] = PyArray_DescrFromType(NPY_DEFAULT_INT);
}
else {
Py_INCREF(given_descrs[4]);
loop_descrs[4] = given_descrs[4];
}
return NPY_NO_CASTING;
}
static int
string_startswith_endswith_promoter(
PyObject *NPY_UNUSED(ufunc),
PyArray_DTypeMeta *const op_dtypes[],
PyArray_DTypeMeta *const signature[],
PyArray_DTypeMeta *new_op_dtypes[])
{
new_op_dtypes[0] = NPY_DT_NewRef(&PyArray_StringDType);
new_op_dtypes[1] = NPY_DT_NewRef(&PyArray_StringDType);
new_op_dtypes[2] = NPY_DT_NewRef(&PyArray_Int64DType);
new_op_dtypes[3] = NPY_DT_NewRef(&PyArray_Int64DType);
new_op_dtypes[4] = PyArray_DTypeFromTypeNum(NPY_BOOL);
return 0;
}
static NPY_CASTING
string_startswith_endswith_resolve_descriptors(
struct PyArrayMethodObject_tag *NPY_UNUSED(method),
PyArray_DTypeMeta *const NPY_UNUSED(dtypes[]),
PyArray_Descr *const given_descrs[],
PyArray_Descr *loop_descrs[],
npy_intp *NPY_UNUSED(view_offset))
{
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)given_descrs[0];
PyArray_StringDTypeObject *descr2 = (PyArray_StringDTypeObject *)given_descrs[1];
if (stringdtype_compatible_na(descr1->na_object, descr2->na_object, NULL) == -1) {
return (NPY_CASTING)-1;
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
Py_INCREF(given_descrs[2]);
loop_descrs[2] = given_descrs[2];
Py_INCREF(given_descrs[3]);
loop_descrs[3] = given_descrs[3];
if (given_descrs[4] == NULL) {
loop_descrs[4] = PyArray_DescrFromType(NPY_BOOL);
}
else {
Py_INCREF(given_descrs[4]);
loop_descrs[4] = given_descrs[4];
}
return NPY_NO_CASTING;
}
typedef npy_intp find_like_function(Buffer<ENCODING::UTF8>, Buffer<ENCODING::UTF8>,
npy_int64, npy_int64);
static int
string_findlike_strided_loop(PyArrayMethod_Context *context,
char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *auxdata)
{
const char *ufunc_name = ((PyUFuncObject *)context->caller)->name;
find_like_function *function = *(find_like_function *)(context->method->static_data);
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)context->descriptors[0];
int has_null = descr1->na_object != NULL;
int has_string_na = descr1->has_string_na;
const npy_static_string *default_string = &descr1->default_string;
npy_string_allocator *allocators[2] = {};
NpyString_acquire_allocators(2, context->descriptors, allocators);
npy_string_allocator *s1allocator = allocators[0];
npy_string_allocator *s2allocator = allocators[1];
char *in1 = data[0];
char *in2 = data[1];
char *in3 = data[2];
char *in4 = data[3];
char *out = data[4];
npy_intp N = dimensions[0];
while (N--) {
LOAD_TWO_INPUT_STRINGS(ufunc_name);
if (NPY_UNLIKELY(s1_isnull || s2_isnull)) {
if (has_null && !has_string_na) {
npy_gil_error(PyExc_ValueError,
"'%s' not supported for null values that are not "
"strings.", ufunc_name);
goto fail;
}
else {
if (s1_isnull) {
s1 = *default_string;
}
if (s2_isnull) {
s2 = *default_string;
}
}
}
npy_int64 start = *(npy_int64 *)in3;
npy_int64 end = *(npy_int64 *)in4;
Buffer<ENCODING::UTF8> buf1((char *)s1.buf, s1.size);
Buffer<ENCODING::UTF8> buf2((char *)s2.buf, s2.size);
npy_intp pos = function(buf1, buf2, start, end);
if (pos == -2) {
goto fail;
}
*(npy_intp *)out = pos;
in1 += strides[0];
in2 += strides[1];
in3 += strides[2];
in4 += strides[3];
out += strides[4];
}
NpyString_release_allocators(2, allocators);
return 0;
fail:
NpyString_release_allocators(2, allocators);
return -1;
}
static int
string_startswith_endswith_strided_loop(PyArrayMethod_Context *context,
char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *auxdata)
{
const char *ufunc_name = ((PyUFuncObject *)context->caller)->name;
STARTPOSITION startposition = *(STARTPOSITION *)context->method->static_data;
PyArray_StringDTypeObject *descr1 = (PyArray_StringDTypeObject *)context->descriptors[0];
int has_null = descr1->na_object != NULL;
int has_string_na = descr1->has_string_na;
int has_nan_na = descr1->has_nan_na;
const npy_static_string *default_string = &descr1->default_string;
npy_string_allocator *allocators[2] = {};
NpyString_acquire_allocators(2, context->descriptors, allocators);
npy_string_allocator *s1allocator = allocators[0];
npy_string_allocator *s2allocator = allocators[1];
char *in1 = data[0];
char *in2 = data[1];
char *in3 = data[2];
char *in4 = data[3];
char *out = data[4];
npy_intp N = dimensions[0];
while (N--) {
LOAD_TWO_INPUT_STRINGS(ufunc_name);
if (NPY_UNLIKELY(s1_isnull || s2_isnull)) {
if (has_null && !has_string_na) {
if (has_nan_na) {
// nulls are always falsey for this operation.
*(npy_bool *)out = 0;
goto next_step;
}
else {
npy_gil_error(PyExc_ValueError,
"'%s' not supported for null values that "
"are not nan-like or strings.", ufunc_name);
goto fail;
}
}
else {
if (s1_isnull) {
s1 = *default_string;
}
if (s2_isnull) {
s2 = *default_string;
}
}
}
{
npy_int64 start = *(npy_int64 *)in3;
npy_int64 end = *(npy_int64 *)in4;
Buffer<ENCODING::UTF8> buf1((char *)s1.buf, s1.size);
Buffer<ENCODING::UTF8> buf2((char *)s2.buf, s2.size);