rsarray 1.2.10__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rsarray/__init__.py +12028 -0
- rsarray/_core/__init__.py +213 -0
- rsarray/_core/_exceptions.py +83 -0
- rsarray/_core/_internal.py +331 -0
- rsarray/_core/_multiarray_tests.py +129 -0
- rsarray/_core/_multiarray_umath.py +121 -0
- rsarray/_core/_rational_tests.py +52 -0
- rsarray/_core/arrayprint.py +36 -0
- rsarray/_core/fromnumeric.py +31 -0
- rsarray/_core/function_base.py +84 -0
- rsarray/_core/multiarray.py +385 -0
- rsarray/_core/numeric.py +28 -0
- rsarray/_core/numerictypes.py +233 -0
- rsarray/_core/records.py +14 -0
- rsarray/_core/shape_base.py +23 -0
- rsarray/_core/strings.py +78 -0
- rsarray/_core/umath.py +176 -0
- rsarray/_dtypes.py +1229 -0
- rsarray/_extra.py +6621 -0
- rsarray/_typing.py +24 -0
- rsarray/array_methods.py +1224 -0
- rsarray/array_ops.py +1585 -0
- rsarray/char.py +1590 -0
- rsarray/exceptions.py +45 -0
- rsarray/fft/__init__.py +231 -0
- rsarray/io.py +453 -0
- rsarray/lib/__init__.py +102 -0
- rsarray/lib/_function_base_impl.py +37 -0
- rsarray/lib/_index_tricks_impl.py +98 -0
- rsarray/lib/_nanfunctions_impl.py +1637 -0
- rsarray/lib/_stride_tricks_impl.py +110 -0
- rsarray/lib/recfunctions.py +1222 -0
- rsarray/lib/stride_tricks.py +106 -0
- rsarray/linalg/__init__.py +2603 -0
- rsarray/linalg/_linalg.py +91 -0
- rsarray/linalg/_pylinalg.py +1008 -0
- rsarray/linalg/lapack_lite.py +13 -0
- rsarray/ma/__init__.py +92 -0
- rsarray/ma/core.py +1385 -0
- rsarray/ma/extras.py +711 -0
- rsarray/ma/mrecords.py +144 -0
- rsarray/math_functions.py +965 -0
- rsarray/matlib.py +67 -0
- rsarray/num_core.cp315-win_amd64.pyd +0 -0
- rsarray/polynomial/__init__.py +148 -0
- rsarray/polynomial/_npcompat.py +721 -0
- rsarray/polynomial/polynomial.py +1082 -0
- rsarray/random/__init__.py +2266 -0
- rsarray/random/_distributions.py +773 -0
- rsarray/rec.py +207 -0
- rsarray/statistics.py +1360 -0
- rsarray/strings.py +349 -0
- rsarray/typing/__init__.py +26 -0
- rsarray-1.2.10.dist-info/METADATA +280 -0
- rsarray-1.2.10.dist-info/RECORD +59 -0
- rsarray-1.2.10.dist-info/WHEEL +4 -0
- rsarray-1.2.10.dist-info/licenses/LICENSE +21 -0
- rsarray-1.2.10.dist-info/licenses/NOTICE +7 -0
- rsarray-1.2.10.dist-info/sboms/rsarray.cyclonedx.json +2789 -0
rsarray/char.py
ADDED
|
@@ -0,0 +1,1590 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
"""rsarray.char - 向量化字符串操作模块(Rust 层实现,本模块为薄包装)。
|
|
3
|
+
|
|
4
|
+
所有元素级字符串运算由 Rust 端 ``num_core.char_str_op/char_int_op/
|
|
5
|
+
char_bool_op/char_add/char_multiply`` 一次跨边界批量完成。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import builtins
|
|
9
|
+
|
|
10
|
+
import rsarray.num_core as _core
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _np_mod():
|
|
14
|
+
import rsarray
|
|
15
|
+
return rsarray
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _get_strings(a):
|
|
19
|
+
"""从 ndarray 或列表中提取字符串列表(bytes 解码为 utf-8 str)。"""
|
|
20
|
+
if isinstance(a, (str, bytes)):
|
|
21
|
+
return [a]
|
|
22
|
+
rd = getattr(a, '_raw_data', None)
|
|
23
|
+
if rd is None and hasattr(a, '_inst'):
|
|
24
|
+
rd = getattr(a._inst, '_raw_data', None)
|
|
25
|
+
if rd is not None:
|
|
26
|
+
return builtins.list(rd)
|
|
27
|
+
if hasattr(a, 'tolist'):
|
|
28
|
+
return _flatten(a.tolist(), [])
|
|
29
|
+
if isinstance(a, (builtins.list, builtins.tuple)):
|
|
30
|
+
return _flatten(a, [])
|
|
31
|
+
return _flatten([a], [])
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _flatten(x, out):
|
|
35
|
+
for v in x:
|
|
36
|
+
if isinstance(v, (builtins.list, builtins.tuple)):
|
|
37
|
+
_flatten(v, out)
|
|
38
|
+
else:
|
|
39
|
+
out.append(v)
|
|
40
|
+
return out
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _decode(vals):
|
|
44
|
+
"""bytes 元素解码(surrogateescape 往返无损)。"""
|
|
45
|
+
return [v.decode('utf-8', 'surrogateescape') if isinstance(v, bytes) else v
|
|
46
|
+
for v in vals]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _is_bytes(vals):
|
|
50
|
+
return builtins.any(isinstance(v, bytes) for v in vals)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _a_was_bytes(a):
|
|
54
|
+
"""数组输入:以 dtype.kind 为准(tolist/_raw_data 元素类型不可信)。"""
|
|
55
|
+
try:
|
|
56
|
+
k = a.dtype.kind
|
|
57
|
+
if k in 'SU':
|
|
58
|
+
return k == 'S'
|
|
59
|
+
except Exception:
|
|
60
|
+
pass
|
|
61
|
+
return _is_bytes(_get_strings(a))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _shape_of(a):
|
|
65
|
+
try:
|
|
66
|
+
return builtins.tuple(a.shape)
|
|
67
|
+
except Exception:
|
|
68
|
+
return ()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _nest(flat, dims):
|
|
72
|
+
"""flat → 嵌套列表(最外层长度 = dims[0],1 维即 flat 本身)。"""
|
|
73
|
+
nested = builtins.list(flat)
|
|
74
|
+
for dim in builtins.reversed(dims[1:]):
|
|
75
|
+
nested = [nested[i:i + dim]
|
|
76
|
+
for i in range(0, builtins.len(nested), dim)]
|
|
77
|
+
return nested
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _bc_shapes(shapes):
|
|
81
|
+
"""式形状广播(尾对齐,1 可扩,短形状前导补 1)。"""
|
|
82
|
+
ss = [builtins.tuple(reversed(s)) for s in shapes if s]
|
|
83
|
+
out = []
|
|
84
|
+
nd = builtins.max((builtins.len(s) for s in ss), default=0)
|
|
85
|
+
for i in builtins.range(nd):
|
|
86
|
+
ds = [s[i] for s in ss if i < builtins.len(s) and s[i] != 1]
|
|
87
|
+
if builtins.len(set(ds)) > 1:
|
|
88
|
+
raise ValueError('could not broadcast input shapes')
|
|
89
|
+
out.append(ds[0] if ds else 1)
|
|
90
|
+
return builtins.tuple(reversed(out))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _bc_linear(shape, idx):
|
|
94
|
+
"""广播形状 → 扁平索引(尾对齐,dim=1 → 0)。"""
|
|
95
|
+
if not shape:
|
|
96
|
+
return 0
|
|
97
|
+
pad = builtins.max(0, builtins.len(idx) - builtins.len(shape))
|
|
98
|
+
padded = (1,) * pad + builtins.tuple(shape)
|
|
99
|
+
off = 0
|
|
100
|
+
for d, i in builtins.zip(padded, idx):
|
|
101
|
+
if d != 1:
|
|
102
|
+
off = off * d + i
|
|
103
|
+
return off
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _bc_get(flat, shape, idx):
|
|
107
|
+
if builtins.len(flat) <= 1 or not shape:
|
|
108
|
+
return flat[0]
|
|
109
|
+
return flat[_bc_linear(shape, idx)]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _iter_indices(shape):
|
|
113
|
+
if not shape:
|
|
114
|
+
yield ()
|
|
115
|
+
return
|
|
116
|
+
total = 1
|
|
117
|
+
for d in shape:
|
|
118
|
+
total *= d
|
|
119
|
+
for n in builtins.range(total):
|
|
120
|
+
idx = []
|
|
121
|
+
m = n
|
|
122
|
+
for d in reversed(shape):
|
|
123
|
+
idx.append(m % d)
|
|
124
|
+
m //= d
|
|
125
|
+
yield builtins.tuple(reversed(idx))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _str_input_kind(x):
|
|
129
|
+
"""字符串输入种类('S'/'U'/'T');非字符串输入返回 None(—no-loop)。"""
|
|
130
|
+
if isinstance(x, bytes):
|
|
131
|
+
return 'S'
|
|
132
|
+
if isinstance(x, str):
|
|
133
|
+
return 'U'
|
|
134
|
+
if isinstance(x, (builtins.list, builtins.tuple)):
|
|
135
|
+
flat = _flatten(builtins.list(x), [])
|
|
136
|
+
if not flat:
|
|
137
|
+
return None
|
|
138
|
+
if builtins.all(isinstance(v, bytes) for v in flat):
|
|
139
|
+
return 'S'
|
|
140
|
+
if builtins.all(isinstance(v, str) for v in flat):
|
|
141
|
+
return 'U'
|
|
142
|
+
return None
|
|
143
|
+
k = getattr(getattr(x, 'dtype', None), 'kind', None)
|
|
144
|
+
return k if k in ('S', 'U', 'T') else None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _operand(x):
|
|
148
|
+
"""把 values 输入转成 (flat 列表, shape);str 标量 → 单元素。
|
|
149
|
+
|
|
150
|
+
数组语义:数值标量/数组元素提升为 rsarray 标量(repr 为
|
|
151
|
+
'np.int64(1)' 等),保证 '%r' 格式化与标准语义一致。"""
|
|
152
|
+
if x is None:
|
|
153
|
+
return None, ()
|
|
154
|
+
if isinstance(x, (str, bytes)):
|
|
155
|
+
return [_decode([x])[0]], ()
|
|
156
|
+
if isinstance(x, bool):
|
|
157
|
+
return [builtins.int(x)], ()
|
|
158
|
+
if isinstance(x, (int, float)):
|
|
159
|
+
from .__init__ import int64 as _i64, float64 as _f64
|
|
160
|
+
return [(_f64(x) if isinstance(x, float) else _i64(x))], ()
|
|
161
|
+
flat = _decode(_get_strings(x))
|
|
162
|
+
if isinstance(x, (builtins.list, builtins.tuple)):
|
|
163
|
+
shape = _nested_shape(x)
|
|
164
|
+
else:
|
|
165
|
+
shape = _shape_of(x)
|
|
166
|
+
# 数值输入(标量已处理):逐元素包装 rsarray 标量类('%r' 语义)。
|
|
167
|
+
if flat and all(isinstance(v, (int, float)) for v in flat):
|
|
168
|
+
from .__init__ import _SCALAR_CLASS_BY_DTYPE, int64 as _i64
|
|
169
|
+
from .__init__ import float64 as _f64
|
|
170
|
+
if isinstance(x, (builtins.list, builtins.tuple)):
|
|
171
|
+
_cls = _f64 if isinstance(flat[0], float) else _i64
|
|
172
|
+
else:
|
|
173
|
+
_cls = _SCALAR_CLASS_BY_DTYPE.get(
|
|
174
|
+
str(getattr(x, '_dtype', '')))
|
|
175
|
+
if _cls is None:
|
|
176
|
+
_cls = _f64 if isinstance(flat[0], float) else _i64
|
|
177
|
+
flat = [_cls(v) for v in flat]
|
|
178
|
+
return flat, shape
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _wrap_str(vals, shape, was_bytes=False):
|
|
182
|
+
"""把扁平结果列表按 shape 包装为字符串数组。
|
|
183
|
+
|
|
184
|
+
裸 'S'/'U' dtype 按内容推断最小宽度(保留 bytes/str 类别:S 输入→
|
|
185
|
+
S 结果,U 输入→U 结果),宽度随内容增长(如 replace 变宽)。
|
|
186
|
+
"""
|
|
187
|
+
from .__init__ import array as _array
|
|
188
|
+
kind = 'S' if was_bytes else 'U'
|
|
189
|
+
if not shape:
|
|
190
|
+
return _array([vals[0] if vals else ''], dtype=kind)
|
|
191
|
+
nested = vals
|
|
192
|
+
for dim in builtins.reversed(shape[1:]):
|
|
193
|
+
nested = [nested[i:i + dim] for i in range(0, builtins.len(nested), dim)]
|
|
194
|
+
return _array(nested, dtype=kind)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _str_op(op, a, arg1=None, arg2=None, arg3=None):
|
|
198
|
+
"""字符串算子薄包装:解码 → Rust → 编码 → 包装。
|
|
199
|
+
|
|
200
|
+
S 输入结果保持 S dtype(was_bytes 传递,S→S 语义)。"""
|
|
201
|
+
vals = _decode(_get_strings(a))
|
|
202
|
+
out = _core.char_str_op(op, vals,
|
|
203
|
+
None if arg1 is None else _as_str_arg(arg1),
|
|
204
|
+
None if arg2 is None else _as_str_arg(arg2),
|
|
205
|
+
None if arg3 is None else _as_str_arg(arg3))
|
|
206
|
+
_wb = _a_was_bytes(a)
|
|
207
|
+
if _wb:
|
|
208
|
+
out = [v.encode('utf-8', 'surrogateescape') for v in out]
|
|
209
|
+
shape = _shape_of(a)
|
|
210
|
+
if isinstance(a, (str, bytes)):
|
|
211
|
+
return out[0] if out else ''
|
|
212
|
+
if shape == ():
|
|
213
|
+
return out[0] if out else ''
|
|
214
|
+
return _wrap_str(out, shape, was_bytes=_wb)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _as_str_arg(x):
|
|
218
|
+
"""算子参数转字符串(宽度/填充符等)。"""
|
|
219
|
+
if isinstance(x, (str, bytes)):
|
|
220
|
+
return x.decode('utf-8', 'surrogateescape') if isinstance(x, bytes) else x
|
|
221
|
+
return builtins.str(x)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _int_op(op, a, arg1=None, arg2=None, arg3=None):
|
|
225
|
+
vals = _decode(_get_strings(a))
|
|
226
|
+
out = _core.char_int_op(op, vals,
|
|
227
|
+
None if arg1 is None else _as_str_arg(arg1),
|
|
228
|
+
None if arg2 is None else _as_str_arg(arg2),
|
|
229
|
+
None if arg3 is None else _as_str_arg(arg3))
|
|
230
|
+
shape = _shape_of(a)
|
|
231
|
+
from .__init__ import ndarray
|
|
232
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
233
|
+
# 数组语义:标量输入 → 0-d int 数组。
|
|
234
|
+
res = ndarray([builtins.float(out[0]) if out else -1], _dtype='int64')
|
|
235
|
+
return res.reshape(())
|
|
236
|
+
res = ndarray([builtins.float(v) for v in out], _dtype='int64')
|
|
237
|
+
return res.reshape(shape)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _bool_op(op, a, arg1=None, other=None):
|
|
241
|
+
vals = _decode(_get_strings(a))
|
|
242
|
+
other_vals = None
|
|
243
|
+
if other is not None:
|
|
244
|
+
other_vals = _decode(_get_strings(other))
|
|
245
|
+
out = _core.char_bool_op(op, vals,
|
|
246
|
+
None if arg1 is None else _as_str_arg(arg1),
|
|
247
|
+
other_vals)
|
|
248
|
+
shape = _shape_of(a)
|
|
249
|
+
from .__init__ import ndarray
|
|
250
|
+
if isinstance(a, (str, bytes)) or shape == ():
|
|
251
|
+
# 数组语义:标量比较返回 0-d bool 数组(isinstance ndarray)。
|
|
252
|
+
val = builtins.bool(out[0]) if out else False
|
|
253
|
+
res = ndarray([1.0 if val else 0.0])
|
|
254
|
+
res._dtype = 'bool'
|
|
255
|
+
return res.reshape(())
|
|
256
|
+
res = ndarray([1.0 if v else 0.0 for v in out])
|
|
257
|
+
res._dtype = 'bool'
|
|
258
|
+
return res.reshape(shape)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
# ==================== 模块级函数(np.char.*) ====================
|
|
262
|
+
|
|
263
|
+
def add(x1, x2):
|
|
264
|
+
"""逐元素字符串连接(np.char.add / np.strings.add / np.add 字符串语义)。"""
|
|
265
|
+
v1 = _decode(_get_strings(x1))
|
|
266
|
+
v2 = _decode(_get_strings(x2))
|
|
267
|
+
out = _core.char_add(v1, v2)
|
|
268
|
+
# 数组语义:结果总字节数不得超 intc max(超 → TypeError,
|
|
269
|
+
# large_string_addition 回归)
|
|
270
|
+
if out:
|
|
271
|
+
_w = 1 if _a_was_bytes(x1) else 4
|
|
272
|
+
_total = builtins.max(builtins.len(s) for s in out) * _w \
|
|
273
|
+
* builtins.len(out)
|
|
274
|
+
if _total > 2147483647:
|
|
275
|
+
raise TypeError(
|
|
276
|
+
f"string result of size {_total} bytes exceeds the "
|
|
277
|
+
"maximum representable size (intc max)")
|
|
278
|
+
if _a_was_bytes(x1):
|
|
279
|
+
out = [v.encode('utf-8', 'surrogateescape') for v in out]
|
|
280
|
+
shape = _shape_of(x1)
|
|
281
|
+
if shape == () and _shape_of(x2) == ():
|
|
282
|
+
return out[0] if out else ''
|
|
283
|
+
if shape == ():
|
|
284
|
+
shape = _shape_of(x2)
|
|
285
|
+
res = _wrap_str(out, shape, was_bytes=_a_was_bytes(x1))
|
|
286
|
+
if isinstance(x1, chararray) or isinstance(x2, chararray):
|
|
287
|
+
return _as_chararray(res)
|
|
288
|
+
return res
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def multiply(a, i):
|
|
292
|
+
"""逐元素重复(np.char.multiply / np.strings.multiply 语义)。
|
|
293
|
+
|
|
294
|
+
数组语义:i 必须整型(float/bool → TypeError "unsupported type ...
|
|
295
|
+
for operand 'i'");0-d 整数数组按标量处理;重复后总长超 intp 上限
|
|
296
|
+
→ OverflowError;i≤0 → 空串。"""
|
|
297
|
+
vals = _decode(_get_strings(a))
|
|
298
|
+
if hasattr(i, 'tolist'):
|
|
299
|
+
_t = i.tolist()
|
|
300
|
+
fl = _flatten([_t] if not isinstance(_t, list) else _t, [])
|
|
301
|
+
if fl and hasattr(i, 'dtype') and \
|
|
302
|
+
getattr(getattr(i, 'dtype', None), 'kind', None) not in 'iub':
|
|
303
|
+
raise TypeError(
|
|
304
|
+
f"unsupported type {i.dtype} for operand 'i'")
|
|
305
|
+
elif isinstance(i, (builtins.list, builtins.tuple)):
|
|
306
|
+
fl = _flatten(builtins.list(i), [])
|
|
307
|
+
else:
|
|
308
|
+
if not isinstance(i, int) or isinstance(i, bool):
|
|
309
|
+
_tname = getattr(type(i), '__name__', 'float')
|
|
310
|
+
raise TypeError(f"unsupported type {_tname} for operand 'i'")
|
|
311
|
+
fl = [i]
|
|
312
|
+
ns = [builtins.int(v) for v in fl]
|
|
313
|
+
_max_intp = 2 ** 63 - 1
|
|
314
|
+
if ns and builtins.max(len(v) for v in vals) * builtins.max(ns) > _max_intp:
|
|
315
|
+
raise OverflowError(
|
|
316
|
+
"repeated string would be larger than max size")
|
|
317
|
+
if builtins.len(ns) == 1:
|
|
318
|
+
ns = ns * builtins.max(builtins.len(vals), 1)
|
|
319
|
+
out = _core.char_multiply(vals, ns)
|
|
320
|
+
_wb = _is_bytes(_get_strings(a))
|
|
321
|
+
if _wb:
|
|
322
|
+
out = [v.encode('utf-8', 'surrogateescape') for v in out]
|
|
323
|
+
shape = _shape_of(a)
|
|
324
|
+
if shape == ():
|
|
325
|
+
return out[0] if out else ''
|
|
326
|
+
return _wrap_str(out, shape, was_bytes=_wb)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def capitalize(a):
|
|
330
|
+
"""首字母大写,其余小写。"""
|
|
331
|
+
return _str_op('capitalize', a)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def title(a):
|
|
335
|
+
"""title 语义(非字母后首字母大写)。"""
|
|
336
|
+
return _str_op('title', a)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def lower(a):
|
|
340
|
+
"""全部小写。"""
|
|
341
|
+
return _str_op('lower', a)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def upper(a):
|
|
345
|
+
"""全部大写。"""
|
|
346
|
+
return _str_op('upper', a)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def swapcase(a):
|
|
350
|
+
"""大小写互换。"""
|
|
351
|
+
return _str_op('swapcase', a)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def strip(a, chars=None):
|
|
355
|
+
"""两侧去字符。"""
|
|
356
|
+
return _str_op('strip', a, chars)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def lstrip(a, chars=None):
|
|
360
|
+
return _str_op('lstrip', a, chars)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def rstrip(a, chars=None):
|
|
364
|
+
return _str_op('rstrip', a, chars)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _pad_op(op, a, width, fillchar=None):
|
|
368
|
+
"""center/ljust/rjust/zfill:width/fillchar 支持标量/数组(广播)。
|
|
369
|
+
|
|
370
|
+
数组语义:fillchar 非单字符 → TypeError("The fill character must be
|
|
371
|
+
exactly one character long")。"""
|
|
372
|
+
vals = _decode(_get_strings(a))
|
|
373
|
+
a_shape = _shape_of(a)
|
|
374
|
+
was_bytes = _a_was_bytes(a)
|
|
375
|
+
w_flat, w_shape = _operand(width)
|
|
376
|
+
bshape = _bc_shapes([a_shape, w_shape]) if w_shape else a_shape
|
|
377
|
+
if fillchar is None:
|
|
378
|
+
fc_flat, fc_shape = [' '], ()
|
|
379
|
+
else:
|
|
380
|
+
fc_flat = _decode(_get_strings(fillchar))
|
|
381
|
+
fc_shape = _shape_of(fillchar)
|
|
382
|
+
if fc_flat and builtins.len(fc_flat[0]) != 1:
|
|
383
|
+
raise TypeError(
|
|
384
|
+
'The fill character must be exactly one character long')
|
|
385
|
+
out = []
|
|
386
|
+
for idx in _iter_indices(bshape):
|
|
387
|
+
v = _bc_get(vals, a_shape, idx)
|
|
388
|
+
w = _bc_get(w_flat, w_shape, idx)
|
|
389
|
+
if op == 'zfill':
|
|
390
|
+
out.append(v.zfill(w))
|
|
391
|
+
else:
|
|
392
|
+
fc = _bc_get(fc_flat, fc_shape, idx)
|
|
393
|
+
out.append(getattr(v, op)(w, fc))
|
|
394
|
+
if was_bytes:
|
|
395
|
+
# S (bytes) dtype 仅接受 ASCII:非 ASCII → ValueError
|
|
396
|
+
# ("'ascii' codec can't encode ...")
|
|
397
|
+
out = [v.encode('ascii').decode('ascii') for v in out]
|
|
398
|
+
res = _wrap_str(out, bshape, was_bytes=was_bytes)
|
|
399
|
+
if isinstance(a, chararray):
|
|
400
|
+
return _as_chararray(res)
|
|
401
|
+
return res
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def center(a, width, fillchar=' '):
|
|
405
|
+
"""居中填充(width 支持数组广播)。"""
|
|
406
|
+
return _pad_op('center', a, width, fillchar)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def ljust(a, width, fillchar=' '):
|
|
410
|
+
return _pad_op('ljust', a, width, fillchar)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def rjust(a, width, fillchar=' '):
|
|
414
|
+
return _pad_op('rjust', a, width, fillchar)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def zfill(a, width):
|
|
418
|
+
return _pad_op('zfill', a, width)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def replace(a, old, new, count=None):
|
|
422
|
+
"""逐元素替换(old/new/count 支持数组广播,数组语义)。
|
|
423
|
+
|
|
424
|
+
ArrayLike 输入经 __array__ 转换(恰好一次,数组语义)。"""
|
|
425
|
+
from rsarray import asarray as _asarray
|
|
426
|
+
if not hasattr(a, '_raw_data'):
|
|
427
|
+
a = _asarray(a)
|
|
428
|
+
if not hasattr(old, '_raw_data'):
|
|
429
|
+
old = _asarray(old)
|
|
430
|
+
if not hasattr(new, '_raw_data'):
|
|
431
|
+
new = _asarray(new)
|
|
432
|
+
vals = _decode(_get_strings(a))
|
|
433
|
+
a_shape = _shape_of(a)
|
|
434
|
+
was_bytes = _a_was_bytes(a)
|
|
435
|
+
o_flat, o_shape = _operand(old)
|
|
436
|
+
n_flat, n_shape = _operand(new)
|
|
437
|
+
c_flat, c_shape = _operand(count)
|
|
438
|
+
bshape = a_shape
|
|
439
|
+
for sh in (o_shape, n_shape, c_shape):
|
|
440
|
+
bshape = _bc_shapes([bshape, sh]) if sh else bshape
|
|
441
|
+
out = []
|
|
442
|
+
for idx in _iter_indices(bshape):
|
|
443
|
+
v = _bc_get(vals, a_shape, idx)
|
|
444
|
+
o = _bc_get(o_flat, o_shape, idx)
|
|
445
|
+
n = _bc_get(n_flat, n_shape, idx)
|
|
446
|
+
c = -1 if c_flat is None else _bc_get(c_flat, c_shape, idx)
|
|
447
|
+
out.append(v.replace(o, n, c))
|
|
448
|
+
res = _wrap_str(out, bshape, was_bytes=was_bytes)
|
|
449
|
+
# T(StringDType 变长)标记传播:输入为 T 时结果保持 T dtype
|
|
450
|
+
# (内部存储仍为 U,语义经 _strdtype_T 表达)
|
|
451
|
+
if getattr(a, '_strdtype_T', False) and hasattr(res, '_str_dtype'):
|
|
452
|
+
res._strdtype_T = True
|
|
453
|
+
if isinstance(a, chararray):
|
|
454
|
+
return _as_chararray(res)
|
|
455
|
+
return res
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def expandtabs(a, tabsize=8):
|
|
459
|
+
"""逐元素 str.expandtabs(数组语义:tab 对齐到下一个 tab stop,
|
|
460
|
+
\\n/\\r 后列号归零)。
|
|
461
|
+
|
|
462
|
+
Python 层实现:按 ``incr = tabsize - (line_pos % tabsize)`` 对齐
|
|
463
|
+
tab stop,而 Rust ``char_str_op`` 每个 tab 固定插 ``tabsize`` 个空格
|
|
464
|
+
不对齐;且结果超限时 Rust 层 abort 而非抛异常。故在此一次完成展开与
|
|
465
|
+
溢出检查(展开后长度 > INT_MAX → OverflowError,见 PR #26589)。
|
|
466
|
+
"""
|
|
467
|
+
# 非字符串输入 → ufunc no-loop 错误(TypeError 子类)
|
|
468
|
+
if not _str_input_kind(a):
|
|
469
|
+
from rsarray._core._exceptions import _UFuncNoLoopError
|
|
470
|
+
raise _UFuncNoLoopError(
|
|
471
|
+
"'_expandtabs' ufunc did not contain a loop with signature "
|
|
472
|
+
"matching types")
|
|
473
|
+
ts = int(tabsize)
|
|
474
|
+
vals = _decode(_get_strings(a))
|
|
475
|
+
LIM = 2147483647 # INT_MAX
|
|
476
|
+
# 第一遍(对齐 string_expandtabs_length):仅计长度,超限即抛
|
|
477
|
+
# OverflowError——不能先构建展开串(' ' * 2**63 会先 MemoryError)
|
|
478
|
+
for v in vals:
|
|
479
|
+
new_len = 0
|
|
480
|
+
line_pos = 0
|
|
481
|
+
for ch in v:
|
|
482
|
+
if ch == '\t':
|
|
483
|
+
if ts > 0:
|
|
484
|
+
new_len += ts - (line_pos % ts)
|
|
485
|
+
line_pos += ts - (line_pos % ts)
|
|
486
|
+
else:
|
|
487
|
+
line_pos += 1
|
|
488
|
+
new_len += 1
|
|
489
|
+
if ch in '\n\r':
|
|
490
|
+
line_pos = 0
|
|
491
|
+
if new_len > LIM:
|
|
492
|
+
raise OverflowError("new string is too long")
|
|
493
|
+
# 第二遍:长度已保证 ≤ INT_MAX,构建展开串
|
|
494
|
+
out = []
|
|
495
|
+
for v in vals:
|
|
496
|
+
line_pos = 0
|
|
497
|
+
parts = []
|
|
498
|
+
for ch in v:
|
|
499
|
+
if ch == '\t':
|
|
500
|
+
if ts > 0:
|
|
501
|
+
incr = ts - (line_pos % ts)
|
|
502
|
+
line_pos += incr
|
|
503
|
+
parts.append(' ' * incr)
|
|
504
|
+
else:
|
|
505
|
+
line_pos += 1
|
|
506
|
+
parts.append(ch)
|
|
507
|
+
if ch in '\n\r':
|
|
508
|
+
line_pos = 0
|
|
509
|
+
out.append(''.join(parts))
|
|
510
|
+
# 输出约定与 _str_op 一致(bytes 回转 / 标量 / shape 包装);
|
|
511
|
+
# 保留输入 dtype 种类(S→S、U→U,见 strings 模块 expandtabs)
|
|
512
|
+
_wb = _a_was_bytes(a)
|
|
513
|
+
if _wb:
|
|
514
|
+
out = [v.encode('utf-8', 'surrogateescape') for v in out]
|
|
515
|
+
shape = _shape_of(a)
|
|
516
|
+
if isinstance(a, (str, bytes)):
|
|
517
|
+
return out[0] if out else ''
|
|
518
|
+
# 0-d 数组输入:返回数组(含 .dtype),非裸标量
|
|
519
|
+
if shape == ():
|
|
520
|
+
return _wrap_str(out, (), was_bytes=_wb)
|
|
521
|
+
return _wrap_str(out, shape, was_bytes=_wb)
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def join(sep, a):
|
|
525
|
+
"""以 sep 连接序列元素(sep 支持数组逐元素,广播)。"""
|
|
526
|
+
s = _decode(_get_strings(a))
|
|
527
|
+
a_shape = _shape_of(a)
|
|
528
|
+
if isinstance(sep, (str, bytes)):
|
|
529
|
+
seps = [_decode([sep])[0]]
|
|
530
|
+
sep_shape = ()
|
|
531
|
+
else:
|
|
532
|
+
seps = _decode(_get_strings(sep))
|
|
533
|
+
sep_shape = (_nested_shape(sep)
|
|
534
|
+
if isinstance(sep, (builtins.list, builtins.tuple))
|
|
535
|
+
else _shape_of(sep))
|
|
536
|
+
bshape = _bc_shapes([a_shape, sep_shape]) if sep_shape else a_shape
|
|
537
|
+
out = []
|
|
538
|
+
for idx in _iter_indices(bshape):
|
|
539
|
+
v = _bc_get(s, a_shape, idx)
|
|
540
|
+
sp = _bc_get(seps, sep_shape, idx)
|
|
541
|
+
out.append(sp.join(v))
|
|
542
|
+
if isinstance(a, (str, bytes)) and not sep_shape:
|
|
543
|
+
return out[0] if out else ''
|
|
544
|
+
res = _wrap_str(out, bshape, was_bytes=False)
|
|
545
|
+
if isinstance(a, chararray):
|
|
546
|
+
return _as_chararray(res)
|
|
547
|
+
return res
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def _bc_int_arg(x):
|
|
551
|
+
"""start/end 整型参数 → (flat int 列表, shape);None → (None, ())。"""
|
|
552
|
+
if x is None:
|
|
553
|
+
return None, ()
|
|
554
|
+
if isinstance(x, (builtins.list, builtins.tuple)):
|
|
555
|
+
flat = _flatten(builtins.list(x), [])
|
|
556
|
+
shape = _nested_shape(x)
|
|
557
|
+
elif hasattr(x, 'tolist') and hasattr(x, 'shape'):
|
|
558
|
+
_t = x.tolist()
|
|
559
|
+
flat = _flatten([_t] if not isinstance(_t, list) else _t, [])
|
|
560
|
+
shape = builtins.tuple(x.shape)
|
|
561
|
+
else:
|
|
562
|
+
flat = [x]
|
|
563
|
+
shape = ()
|
|
564
|
+
flat = [builtins.int(v) for v in flat]
|
|
565
|
+
return flat, shape
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
def _range_search(a, sub, start, end, py_fn, kind):
|
|
569
|
+
"""带 start/end 范围的逐元素搜索(广播语义,Python 实现)。
|
|
570
|
+
|
|
571
|
+
数组语义:end=None → intp.max;start/end 按 slice 语义作用于
|
|
572
|
+
每个元素。kind='int' → int64 数组(find/rfind/count/index/rindex);
|
|
573
|
+
kind='bool' → bool 数组(startswith/endswith)。"""
|
|
574
|
+
vals = _decode(_get_strings(a))
|
|
575
|
+
a_shape = _shape_of(a)
|
|
576
|
+
subs = _decode(_get_strings(sub))
|
|
577
|
+
s_shape = _shape_of(sub)
|
|
578
|
+
bshape = _bc_shapes([a_shape, s_shape])
|
|
579
|
+
st, st_shape = _bc_int_arg(start)
|
|
580
|
+
ep, ep_shape = _bc_int_arg(end)
|
|
581
|
+
_max = 2 ** 63 - 1
|
|
582
|
+
out = []
|
|
583
|
+
for _idx in _iter_indices(bshape):
|
|
584
|
+
_v = _bc_get(vals, a_shape, _idx)
|
|
585
|
+
_s = _bc_get(st, st_shape, _idx) if st is not None else 0
|
|
586
|
+
_e = _bc_get(ep, ep_shape, _idx) if ep is not None else _max
|
|
587
|
+
out.append(py_fn(_v, _bc_get(subs, s_shape, _idx), _s, _e))
|
|
588
|
+
from .__init__ import ndarray
|
|
589
|
+
if isinstance(a, (str, bytes)) or bshape == ():
|
|
590
|
+
_v0 = out[0] if out else (0 if kind == 'int' else False)
|
|
591
|
+
if kind == 'int':
|
|
592
|
+
res = ndarray([builtins.float(_v0)], _dtype='int64')
|
|
593
|
+
return res.reshape(())
|
|
594
|
+
res = ndarray([1.0 if _v0 else 0.0])
|
|
595
|
+
res._dtype = 'bool'
|
|
596
|
+
return res.reshape(())
|
|
597
|
+
if kind == 'int':
|
|
598
|
+
res = ndarray([builtins.float(v) for v in out], _dtype='int64')
|
|
599
|
+
return res.reshape(bshape)
|
|
600
|
+
res = ndarray([1.0 if v else 0.0 for v in out])
|
|
601
|
+
res._dtype = 'bool'
|
|
602
|
+
return res.reshape(bshape)
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
def find(a, sub, start=0, end=None):
|
|
606
|
+
"""逐元素 str.find(未找到返回 -1);start/end 支持标量/数组。"""
|
|
607
|
+
return _range_search(a, sub, start, end,
|
|
608
|
+
lambda v, s_, st_, e_: v.find(s_, st_, e_), 'int')
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
def rfind(a, sub, start=0, end=None):
|
|
612
|
+
"""逐元素 str.rfind(未找到返回 -1);start/end 支持标量/数组。"""
|
|
613
|
+
return _range_search(a, sub, start, end,
|
|
614
|
+
lambda v, s_, st_, e_: v.rfind(s_, st_, e_), 'int')
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
def index(a, sub, start=0, end=None):
|
|
618
|
+
"""逐元素 str.index(未找到抛 ValueError)。"""
|
|
619
|
+
def _f(v, s_, st_, e_):
|
|
620
|
+
_r = v.find(s_, st_, e_)
|
|
621
|
+
if _r < 0:
|
|
622
|
+
raise ValueError('substring not found')
|
|
623
|
+
return _r
|
|
624
|
+
return _range_search(a, sub, start, end, _f, 'int')
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def rindex(a, sub, start=0, end=None):
|
|
628
|
+
"""逐元素 str.rindex(未找到抛 ValueError)。"""
|
|
629
|
+
def _f(v, s_, st_, e_):
|
|
630
|
+
_r = v.rfind(s_, st_, e_)
|
|
631
|
+
if _r < 0:
|
|
632
|
+
raise ValueError('substring not found')
|
|
633
|
+
return _r
|
|
634
|
+
return _range_search(a, sub, start, end, _f, 'int')
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
def count(a, sub, start=0, end=None):
|
|
638
|
+
"""逐元素 str.count;start/end 支持标量/数组。"""
|
|
639
|
+
return _range_search(a, sub, start, end,
|
|
640
|
+
lambda v, s_, st_, e_: v.count(s_, st_, e_), 'int')
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
def _range_valid(st_, e_, ln):
|
|
644
|
+
"""数组语义的区间有效性:负索引归一(不归零),start > end → 无效。
|
|
645
|
+
|
|
646
|
+
与 C 层 string_startswith/endswith 一致:('','',1,0)→False 而
|
|
647
|
+
('','',0,0)→True;('hello','',-3,-3)→True。"""
|
|
648
|
+
_s = st_ + ln if st_ < 0 else st_
|
|
649
|
+
_e = e_ + ln if e_ < 0 else e_
|
|
650
|
+
return _s <= _e
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
def startswith(a, prefix, start=0, end=None):
|
|
654
|
+
"""逐元素 startswith(检查 a[start:end] 前缀);start/end 支持标量/数组。
|
|
655
|
+
|
|
656
|
+
数组语义:负索引先加长度归一,start > end → False(Python 原生
|
|
657
|
+
切片在此处不一致,故不走 v[st_:e_].startswith)。"""
|
|
658
|
+
def _sw(v, s_, st_, e_):
|
|
659
|
+
if not _range_valid(st_, e_, builtins.len(v)):
|
|
660
|
+
return False
|
|
661
|
+
return v[st_:e_].startswith(s_)
|
|
662
|
+
return _range_search(a, prefix, start, end, _sw, 'bool')
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
def endswith(a, suffix, start=0, end=None):
|
|
666
|
+
"""逐元素 endswith(检查 a[start:end] 后缀);start/end 支持标量/数组。
|
|
667
|
+
|
|
668
|
+
数组语义同 startswith(区间无效 → False)。"""
|
|
669
|
+
def _ew(v, s_, st_, e_):
|
|
670
|
+
if not _range_valid(st_, e_, builtins.len(v)):
|
|
671
|
+
return False
|
|
672
|
+
return v[st_:e_].endswith(s_)
|
|
673
|
+
return _range_search(a, suffix, start, end, _ew, 'bool')
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
def equal(x1, x2):
|
|
677
|
+
"""逐元素 ==。"""
|
|
678
|
+
return _bool_op('equal', x1, other=x2)
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
def not_equal(x1, x2):
|
|
682
|
+
return _bool_op('not_equal', x1, other=x2)
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
def greater(x1, x2):
|
|
686
|
+
return _bool_op('greater', x1, other=x2)
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
def greater_equal(x1, x2):
|
|
690
|
+
return _bool_op('greater_equal', x1, other=x2)
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
def less(x1, x2):
|
|
694
|
+
return _bool_op('less', x1, other=x2)
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
def less_equal(x1, x2):
|
|
698
|
+
return _bool_op('less_equal', x1, other=x2)
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
def _is_method(a, op):
|
|
702
|
+
return _bool_op(op, a)
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
def isalnum(a):
|
|
706
|
+
return _is_method(a, 'isalnum')
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
def isalpha(a):
|
|
710
|
+
return _is_method(a, 'isalpha')
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
def _unicode_only_guard(a, name):
|
|
714
|
+
"""unicode-only strings 函数无 bytes loop:S 输入 → no-loop
|
|
715
|
+
错误(_UFuncNoLoopError 是 TypeError 子类)。"""
|
|
716
|
+
if _a_was_bytes(a):
|
|
717
|
+
from rsarray._core._exceptions import _UFuncNoLoopError
|
|
718
|
+
raise _UFuncNoLoopError(
|
|
719
|
+
f"'{name}' ufunc did not contain a loop with signature "
|
|
720
|
+
"matching types")
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
def isdecimal(a):
|
|
724
|
+
_unicode_only_guard(a, 'isdecimal')
|
|
725
|
+
return _is_method(a, 'isdecimal')
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
def isdigit(a):
|
|
729
|
+
return _is_method(a, 'isdigit')
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
def islower(a):
|
|
733
|
+
return _is_method(a, 'islower')
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
def isnumeric(a):
|
|
737
|
+
_unicode_only_guard(a, 'isnumeric')
|
|
738
|
+
return _is_method(a, 'isnumeric')
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
def isspace(a):
|
|
742
|
+
return _is_method(a, 'isspace')
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
def istitle(a):
|
|
746
|
+
return _is_method(a, 'istitle')
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
def isupper(a):
|
|
750
|
+
return _is_method(a, 'isupper')
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def split(a, sep=None, maxsplit=None):
|
|
754
|
+
"""逐元素分割(Rust 层暂不支持,回退 Python)。
|
|
755
|
+
|
|
756
|
+
数组的 split 返回 ragged object 数组,无法映射为规则字符串算子。
|
|
757
|
+
"""
|
|
758
|
+
s = _get_strings(a)
|
|
759
|
+
result = [v.split(sep, maxsplit) if maxsplit is not None
|
|
760
|
+
else v.split(sep) for v in s]
|
|
761
|
+
if isinstance(a, (str, bytes)):
|
|
762
|
+
return result[0]
|
|
763
|
+
return _make_result(result)
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
def rsplit(a, sep=None, maxsplit=None):
|
|
767
|
+
s = _get_strings(a)
|
|
768
|
+
result = [v.rsplit(sep, maxsplit) if maxsplit is not None
|
|
769
|
+
else v.rsplit(sep) for v in s]
|
|
770
|
+
if isinstance(a, (str, bytes)):
|
|
771
|
+
return result[0]
|
|
772
|
+
return _make_result(result)
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
def splitlines(a, keepends=False):
|
|
776
|
+
s = _get_strings(a)
|
|
777
|
+
result = [v.splitlines(keepends) for v in s]
|
|
778
|
+
if isinstance(a, (str, bytes)):
|
|
779
|
+
return result[0]
|
|
780
|
+
return _make_result(result)
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
def decode(a, encoding='utf-8', errors='strict'):
|
|
784
|
+
"""数组元素依次调用 bytes.decode(返回 U 数组)。"""
|
|
785
|
+
s = _get_strings(a)
|
|
786
|
+
result = [x.decode(encoding, errors) if isinstance(x, bytes) else x
|
|
787
|
+
for x in s]
|
|
788
|
+
if isinstance(a, (str, bytes)):
|
|
789
|
+
return result[0] if result else ''
|
|
790
|
+
shape = _shape_of(a)
|
|
791
|
+
if not shape:
|
|
792
|
+
return result[0] if result else ''
|
|
793
|
+
from .__init__ import array as _array
|
|
794
|
+
if builtins.len(result) == 0:
|
|
795
|
+
res = _array([], dtype='U')
|
|
796
|
+
if builtins.len(shape) > 1:
|
|
797
|
+
res = res.reshape(shape)
|
|
798
|
+
else:
|
|
799
|
+
res = _array(_nest(result, shape), dtype='U')
|
|
800
|
+
if isinstance(a, chararray):
|
|
801
|
+
return _as_chararray(res)
|
|
802
|
+
return res
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
def encode(a, encoding='utf-8', errors='strict'):
|
|
806
|
+
"""数组元素依次调用 str.encode(返回 S 数组)。"""
|
|
807
|
+
s = _get_strings(a)
|
|
808
|
+
result = [x.encode(encoding, errors) if isinstance(x, str) else x
|
|
809
|
+
for x in s]
|
|
810
|
+
if isinstance(a, (str, bytes)):
|
|
811
|
+
return result[0] if result else b''
|
|
812
|
+
shape = _shape_of(a)
|
|
813
|
+
if not shape:
|
|
814
|
+
return result[0] if result else b''
|
|
815
|
+
from .__init__ import array as _array
|
|
816
|
+
if builtins.len(result) == 0:
|
|
817
|
+
res = _array([], dtype='S')
|
|
818
|
+
if builtins.len(shape) > 1:
|
|
819
|
+
res = res.reshape(shape)
|
|
820
|
+
else:
|
|
821
|
+
res = _array(_nest(result, shape), dtype='S')
|
|
822
|
+
if isinstance(a, chararray):
|
|
823
|
+
return _as_chararray(res)
|
|
824
|
+
return res
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
def partition(a, sep):
|
|
828
|
+
"""逐元素 partition(结果 shape+(3,);标量输入返回 3 元列表)。"""
|
|
829
|
+
s = _decode(_get_strings(a))
|
|
830
|
+
a_shape = _shape_of(a)
|
|
831
|
+
if isinstance(a, (str, bytes)):
|
|
832
|
+
seps = _decode(_get_strings(sep))
|
|
833
|
+
r = s[0].partition(seps[0] if seps else '')
|
|
834
|
+
return [r[0], r[1], r[2]]
|
|
835
|
+
seps = _decode(_get_strings(sep))
|
|
836
|
+
sep_shape = () if isinstance(sep, (str, bytes)) else (
|
|
837
|
+
_nested_shape(sep) if isinstance(sep, (builtins.list, builtins.tuple))
|
|
838
|
+
else _shape_of(sep))
|
|
839
|
+
was_bytes = _a_was_bytes(a)
|
|
840
|
+
bshape = _bc_shapes([a_shape, sep_shape]) if sep_shape else a_shape
|
|
841
|
+
out = []
|
|
842
|
+
for idx in _iter_indices(bshape):
|
|
843
|
+
v = _bc_get(s, a_shape, idx)
|
|
844
|
+
sp = _bc_get(seps, sep_shape, idx)
|
|
845
|
+
out.extend(builtins.list(v.partition(sp)))
|
|
846
|
+
shape = bshape + (3,)
|
|
847
|
+
from .__init__ import array as _array
|
|
848
|
+
res = _array(_nest(out, shape), dtype='S' if was_bytes else 'U')
|
|
849
|
+
if isinstance(a, chararray):
|
|
850
|
+
return _as_chararray(res)
|
|
851
|
+
return res
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
def rpartition(a, sep):
|
|
855
|
+
"""逐元素 rpartition(结果 shape+(3,);标量输入返回 3 元列表)。"""
|
|
856
|
+
s = _decode(_get_strings(a))
|
|
857
|
+
a_shape = _shape_of(a)
|
|
858
|
+
if isinstance(a, (str, bytes)):
|
|
859
|
+
seps = _decode(_get_strings(sep))
|
|
860
|
+
r = s[0].rpartition(seps[0] if seps else '')
|
|
861
|
+
return [r[0], r[1], r[2]]
|
|
862
|
+
seps = _decode(_get_strings(sep))
|
|
863
|
+
sep_shape = () if isinstance(sep, (str, bytes)) else (
|
|
864
|
+
_nested_shape(sep) if isinstance(sep, (builtins.list, builtins.tuple))
|
|
865
|
+
else _shape_of(sep))
|
|
866
|
+
was_bytes = _a_was_bytes(a)
|
|
867
|
+
bshape = _bc_shapes([a_shape, sep_shape]) if sep_shape else a_shape
|
|
868
|
+
out = []
|
|
869
|
+
for idx in _iter_indices(bshape):
|
|
870
|
+
v = _bc_get(s, a_shape, idx)
|
|
871
|
+
sp = _bc_get(seps, sep_shape, idx)
|
|
872
|
+
out.extend(builtins.list(v.rpartition(sp)))
|
|
873
|
+
shape = bshape + (3,)
|
|
874
|
+
from .__init__ import array as _array
|
|
875
|
+
res = _array(_nest(out, shape), dtype='S' if was_bytes else 'U')
|
|
876
|
+
if isinstance(a, chararray):
|
|
877
|
+
return _as_chararray(res)
|
|
878
|
+
return res
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
def mod(a, values):
|
|
882
|
+
"""逐元素 % 格式化(char.mod,支持广播)。"""
|
|
883
|
+
s = _get_strings(a)
|
|
884
|
+
a_shape = _shape_of(a)
|
|
885
|
+
v_flat, v_shape = _operand(values)
|
|
886
|
+
was_bytes = _a_was_bytes(a)
|
|
887
|
+
bshape = _bc_shapes([a_shape, v_shape]) if v_shape else a_shape
|
|
888
|
+
out = []
|
|
889
|
+
for idx in _iter_indices(bshape):
|
|
890
|
+
v = _bc_get(s, a_shape, idx)
|
|
891
|
+
x = _bc_get(v_flat, v_shape, idx)
|
|
892
|
+
if isinstance(v, bytes) and isinstance(x, str):
|
|
893
|
+
x = x.encode('utf-8', 'surrogateescape')
|
|
894
|
+
out.append(v % x)
|
|
895
|
+
if isinstance(a, (str, bytes)) and not v_shape:
|
|
896
|
+
return out[0]
|
|
897
|
+
res = _wrap_str(out, bshape, was_bytes=was_bytes)
|
|
898
|
+
if isinstance(a, chararray):
|
|
899
|
+
return _as_chararray(res)
|
|
900
|
+
return res
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def _make_result(strings):
|
|
904
|
+
"""将字符串列表包装为 ndarray(ragged 场景 object 数组)。"""
|
|
905
|
+
from .__init__ import ndarray
|
|
906
|
+
return ndarray._wrap_raw(strings)
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
def str_len(a, axis=None):
|
|
910
|
+
"""逐元素字符串长度(np.char.str_len)→ 整数数组。"""
|
|
911
|
+
s = _decode(_get_strings(a))
|
|
912
|
+
if isinstance(a, (str, bytes)):
|
|
913
|
+
return builtins.len(s[0])
|
|
914
|
+
flat = [builtins.len(v) for v in s]
|
|
915
|
+
shape = _shape_of(a)
|
|
916
|
+
from .__init__ import array as _array
|
|
917
|
+
if not shape:
|
|
918
|
+
return flat[0] if flat else 0
|
|
919
|
+
nested = flat
|
|
920
|
+
for dim in builtins.reversed(shape[1:]):
|
|
921
|
+
nested = [nested[i:i + dim]
|
|
922
|
+
for i in range(0, builtins.len(nested), dim)]
|
|
923
|
+
return _array(nested, dtype='int64')
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
# ==================== chararray(组合代理,方法委托 Rust) ====================
|
|
927
|
+
|
|
928
|
+
_RUST_STR_OPS = {
|
|
929
|
+
'capitalize': ('capitalize',), 'lower': ('lower',), 'upper': ('upper',),
|
|
930
|
+
'title': ('title',), 'swapcase': ('swapcase',), 'strip': ('strip',),
|
|
931
|
+
'lstrip': ('lstrip',), 'rstrip': ('rstrip',), 'expandtabs': ('expandtabs',),
|
|
932
|
+
}
|
|
933
|
+
_RUST_ARG_OPS = {
|
|
934
|
+
'center': ('center', 2), 'ljust': ('ljust', 2), 'rjust': ('rjust', 2),
|
|
935
|
+
'zfill': ('zfill', 1), 'replace': ('replace', None),
|
|
936
|
+
}
|
|
937
|
+
_RUST_INT_OPS = {'find': 1, 'rfind': 1, 'index': 1, 'rindex': 1, 'count': 1}
|
|
938
|
+
_RUST_BOOL_OPS = {'startswith': 1, 'endswith': 1, 'isalnum': 0, 'isalpha': 0,
|
|
939
|
+
'isdecimal': 0, 'isdigit': 0, 'islower': 0, 'isnumeric': 0,
|
|
940
|
+
'isspace': 0, 'istitle': 0, 'isupper': 0}
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
def _nested_shape(data):
|
|
944
|
+
shape = []
|
|
945
|
+
while isinstance(data, (builtins.list, builtins.tuple)):
|
|
946
|
+
shape.append(builtins.len(data))
|
|
947
|
+
data = data[0] if data else None
|
|
948
|
+
return builtins.tuple(shape) or (0,)
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
def _rstrip_tree(x):
|
|
952
|
+
"""递归 rstrip str/bytes 元素(char.array 构造语义)。"""
|
|
953
|
+
if isinstance(x, (str, bytes)):
|
|
954
|
+
return x.rstrip()
|
|
955
|
+
if isinstance(x, (builtins.list, builtins.tuple)):
|
|
956
|
+
return [_rstrip_tree(v) for v in x]
|
|
957
|
+
return x
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
class chararray:
|
|
961
|
+
"""字符串/字节 ndarray(chararray 语义,方法委托 Rust 算子)。
|
|
962
|
+
|
|
963
|
+
组合代理:内部持有真实 ndarray(_inst),ndarray 协议经 __getattr__
|
|
964
|
+
代理,元素级字符串方法经 _core.char_* 算子在 Rust 层完成。
|
|
965
|
+
"""
|
|
966
|
+
|
|
967
|
+
def __new__(cls, obj, itemsize=None, copy=True, unicode=None, order=None):
|
|
968
|
+
from .__init__ import ndarray, array as _array
|
|
969
|
+
_ = order
|
|
970
|
+
if isinstance(obj, chararray):
|
|
971
|
+
if not copy:
|
|
972
|
+
return obj
|
|
973
|
+
inst = ndarray(obj._inst._array.copy())
|
|
974
|
+
inst._dtype = getattr(obj._inst, '_dtype', 'string_')
|
|
975
|
+
inst._raw_data = builtins.list(
|
|
976
|
+
getattr(obj._inst, '_raw_data', None) or [])
|
|
977
|
+
inst._str_dtype = getattr(obj._inst, '_str_dtype', None)
|
|
978
|
+
return _as_chararray(inst)
|
|
979
|
+
if hasattr(obj, '_array'):
|
|
980
|
+
if unicode is False:
|
|
981
|
+
# 数组语义:U 数组 → S 需 ascii 编码,非 ascii 抛错。
|
|
982
|
+
_rd = list(getattr(obj, '_raw_data', None) or [])
|
|
983
|
+
for _v in _rd:
|
|
984
|
+
if isinstance(_v, str):
|
|
985
|
+
_v.encode('ascii')
|
|
986
|
+
shape = _shape_of(obj)
|
|
987
|
+
inst = _array(list(_rd), dtype='S')
|
|
988
|
+
if builtins.len(shape) > 1:
|
|
989
|
+
inst = inst.reshape(shape)
|
|
990
|
+
elif unicode is True:
|
|
991
|
+
_rd = list(getattr(obj, '_raw_data', None) or [])
|
|
992
|
+
if getattr(getattr(obj, 'dtype', None), 'kind', 'U') == 'S':
|
|
993
|
+
_dec = [(_v.decode('utf-8', 'surrogateescape')
|
|
994
|
+
if isinstance(_v, (bytes, bytearray))
|
|
995
|
+
else str(_v)) for _v in _rd]
|
|
996
|
+
shape = _shape_of(obj)
|
|
997
|
+
inst = _array(list(_dec), dtype='U')
|
|
998
|
+
if builtins.len(shape) > 1:
|
|
999
|
+
inst = inst.reshape(shape)
|
|
1000
|
+
else:
|
|
1001
|
+
inst = obj if not copy else obj.copy()
|
|
1002
|
+
else:
|
|
1003
|
+
inst = obj if not copy else obj.copy()
|
|
1004
|
+
else:
|
|
1005
|
+
# char.array:Python 输入构造期元素 rstrip
|
|
1006
|
+
# (全空白元素 → 空串)。ndarray 输入保持原样。
|
|
1007
|
+
obj = _rstrip_tree(obj)
|
|
1008
|
+
if isinstance(obj, (str, bytes)):
|
|
1009
|
+
# 数组语义:单个 str/bytes 是 1 维单元素数组(|S3 / <U1),
|
|
1010
|
+
# 元素本身为完整字符串(len(A)==1, len(A[0])==len(输入))。
|
|
1011
|
+
inst = _array([obj], dtype='S' if isinstance(obj, bytes) else 'U')
|
|
1012
|
+
else:
|
|
1013
|
+
flat0 = None
|
|
1014
|
+
if isinstance(obj, (builtins.list, builtins.tuple)) and obj:
|
|
1015
|
+
fl = []
|
|
1016
|
+
_flatten(builtins.list(obj), fl)
|
|
1017
|
+
flat0 = fl[0] if fl else None
|
|
1018
|
+
if unicode is True or (unicode is None and
|
|
1019
|
+
isinstance(flat0, str)):
|
|
1020
|
+
inst = _array(obj, dtype='U')
|
|
1021
|
+
elif unicode is False:
|
|
1022
|
+
# 数组语义:强制 ascii 编码,非 ascii 抛 UnicodeEncodeError。
|
|
1023
|
+
for _v in _flatten(builtins.list(obj), []):
|
|
1024
|
+
if isinstance(_v, str):
|
|
1025
|
+
_v.encode('ascii')
|
|
1026
|
+
inst = _array(obj, dtype='S')
|
|
1027
|
+
else:
|
|
1028
|
+
inst = _array(obj)
|
|
1029
|
+
return _as_chararray(inst)
|
|
1030
|
+
|
|
1031
|
+
def __getattr__(self, name):
|
|
1032
|
+
# ndarray 协议代理:shape/dtype/tolist/ndim/size/reshape/astype/...
|
|
1033
|
+
return getattr(self._inst, name)
|
|
1034
|
+
|
|
1035
|
+
def astype(self, dtype, *a, **k):
|
|
1036
|
+
# 数组语义:S/U 结果保持 chararray,其余返回普通 ndarray。
|
|
1037
|
+
res = self._inst.astype(dtype, *a, **k)
|
|
1038
|
+
from .__init__ import ndarray as _ndarray
|
|
1039
|
+
if isinstance(res, _ndarray) and res.dtype.kind in 'SU':
|
|
1040
|
+
return _as_chararray(res)
|
|
1041
|
+
return res
|
|
1042
|
+
|
|
1043
|
+
def _vals(self):
|
|
1044
|
+
rd = getattr(self._inst, '_raw_data', None)
|
|
1045
|
+
if rd is not None:
|
|
1046
|
+
return _flatten(builtins.list(rd), [])
|
|
1047
|
+
return _flatten(self._inst.tolist(), [])
|
|
1048
|
+
|
|
1049
|
+
def _apply(self, op, args):
|
|
1050
|
+
"""元素级方法委托 Rust 算子。"""
|
|
1051
|
+
vals = _decode(self._vals())
|
|
1052
|
+
# S dtype 数组 tolist() 返回 str(字节边界丢失),以 dtype kind 为准。
|
|
1053
|
+
try:
|
|
1054
|
+
was_bytes = self._inst.dtype.kind == 'S'
|
|
1055
|
+
except Exception:
|
|
1056
|
+
was_bytes = _is_bytes(vals)
|
|
1057
|
+
shape = _shape_of(self._inst)
|
|
1058
|
+
# None 参数保持 None(stringify 成 'None' 会改变 strip 等语义);
|
|
1059
|
+
# 数组参数传 None(Rust 端按"未指定"处理)。
|
|
1060
|
+
str_args = []
|
|
1061
|
+
for x in args:
|
|
1062
|
+
if x is None:
|
|
1063
|
+
str_args.append(None)
|
|
1064
|
+
elif (isinstance(x, (builtins.list, builtins.tuple))
|
|
1065
|
+
or hasattr(x, 'tolist')):
|
|
1066
|
+
str_args.append(None)
|
|
1067
|
+
else:
|
|
1068
|
+
str_args.append(_as_str_arg(x))
|
|
1069
|
+
if op in _RUST_STR_OPS or op in _RUST_ARG_OPS:
|
|
1070
|
+
a1 = str_args[0] if builtins.len(str_args) > 0 else None
|
|
1071
|
+
a2 = str_args[1] if builtins.len(str_args) > 1 else None
|
|
1072
|
+
a3 = str_args[2] if builtins.len(str_args) > 2 else None
|
|
1073
|
+
if op == 'replace' and builtins.len(args) < 3:
|
|
1074
|
+
a3 = None
|
|
1075
|
+
out = _core.char_str_op(op, vals, a1, a2, a3)
|
|
1076
|
+
elif op in _RUST_INT_OPS:
|
|
1077
|
+
out = _core.char_int_op(op, vals,
|
|
1078
|
+
str_args[0] if str_args else None)
|
|
1079
|
+
return self._pack(out, shape, as_int=True)
|
|
1080
|
+
elif op in _RUST_BOOL_OPS:
|
|
1081
|
+
out = _core.char_bool_op(op, vals,
|
|
1082
|
+
str_args[0] if str_args else None)
|
|
1083
|
+
return self._pack(out, shape, as_bool=True)
|
|
1084
|
+
else:
|
|
1085
|
+
raise AttributeError(op)
|
|
1086
|
+
return self._pack(out, shape, was_bytes=was_bytes)
|
|
1087
|
+
|
|
1088
|
+
def _pack(self, vals, shape, as_int=False, as_bool=False,
|
|
1089
|
+
was_bytes=False):
|
|
1090
|
+
from .__init__ import ndarray
|
|
1091
|
+
if as_int:
|
|
1092
|
+
res = ndarray([builtins.float(v) for v in vals], _dtype='int64')
|
|
1093
|
+
return res.reshape(shape) if shape else res
|
|
1094
|
+
if as_bool:
|
|
1095
|
+
res = ndarray([1.0 if v else 0.0 for v in vals])
|
|
1096
|
+
res._dtype = 'bool'
|
|
1097
|
+
return res.reshape(shape) if shape else res
|
|
1098
|
+
if was_bytes:
|
|
1099
|
+
vals = [v.encode('utf-8', 'surrogateescape')
|
|
1100
|
+
if isinstance(v, str) else v for v in vals]
|
|
1101
|
+
return _as_chararray(_wrap_str(vals, shape, was_bytes=was_bytes))
|
|
1102
|
+
|
|
1103
|
+
def capitalize(self):
|
|
1104
|
+
return self._apply('capitalize', [])
|
|
1105
|
+
|
|
1106
|
+
def lower(self):
|
|
1107
|
+
return self._apply('lower', [])
|
|
1108
|
+
|
|
1109
|
+
def upper(self):
|
|
1110
|
+
return self._apply('upper', [])
|
|
1111
|
+
|
|
1112
|
+
def title(self):
|
|
1113
|
+
return self._apply('title', [])
|
|
1114
|
+
|
|
1115
|
+
def swapcase(self):
|
|
1116
|
+
return self._apply('swapcase', [])
|
|
1117
|
+
|
|
1118
|
+
def strip(self, chars=None):
|
|
1119
|
+
return self._cstrip('strip', chars)
|
|
1120
|
+
|
|
1121
|
+
def lstrip(self, chars=None):
|
|
1122
|
+
return self._cstrip('lstrip', chars)
|
|
1123
|
+
|
|
1124
|
+
def rstrip(self, chars=None):
|
|
1125
|
+
return self._cstrip('rstrip', chars)
|
|
1126
|
+
|
|
1127
|
+
def _cstrip(self, op, chars):
|
|
1128
|
+
try:
|
|
1129
|
+
kind = self._inst.dtype.kind
|
|
1130
|
+
except Exception:
|
|
1131
|
+
kind = 'U'
|
|
1132
|
+
if (chars is not None
|
|
1133
|
+
and (isinstance(chars, (builtins.list, builtins.tuple))
|
|
1134
|
+
or hasattr(chars, 'tolist'))):
|
|
1135
|
+
# chars 数组:逐元素(广播 i % len)。
|
|
1136
|
+
# strip:chars strip 后 C 截断(含 \0);lstrip:chars lstrip 后
|
|
1137
|
+
# 去尾部空白;rstrip:仅 chars rstrip(C 实现语义)。
|
|
1138
|
+
vals = _decode(self._vals())
|
|
1139
|
+
cs = _decode(_get_strings(chars))
|
|
1140
|
+
|
|
1141
|
+
def _c_trunc(s):
|
|
1142
|
+
parts = s.split('\x00', 1)
|
|
1143
|
+
return parts[0] + '\x00' if builtins.len(parts) > 1 else parts[0]
|
|
1144
|
+
|
|
1145
|
+
if op == 'strip':
|
|
1146
|
+
out = [_c_trunc(getattr(v, op)(cs[i % builtins.len(cs)]))
|
|
1147
|
+
for i, v in builtins.enumerate(vals)]
|
|
1148
|
+
elif op == 'lstrip':
|
|
1149
|
+
out = [getattr(v, op)(cs[i % builtins.len(cs)]).rstrip()
|
|
1150
|
+
for i, v in builtins.enumerate(vals)]
|
|
1151
|
+
else:
|
|
1152
|
+
out = [_c_trunc(getattr(v, op)(cs[i % builtins.len(cs)]))
|
|
1153
|
+
for i, v in builtins.enumerate(vals)]
|
|
1154
|
+
return self._pack(out, _shape_of(self._inst),
|
|
1155
|
+
was_bytes=kind == 'S')
|
|
1156
|
+
if kind in 'SU':
|
|
1157
|
+
if op == 'lstrip':
|
|
1158
|
+
# lstrip 无参:仅空白 lstrip(不 \0 截断,数组语义)。
|
|
1159
|
+
return self._apply(op, [chars])
|
|
1160
|
+
# strip/rstrip 无参:C 字符串截断(除 \0)+ 空白 op。
|
|
1161
|
+
vals = [v.split('\x00', 1)[0] for v in _decode(self._vals())]
|
|
1162
|
+
sub = _as_chararray(_wrap_str(
|
|
1163
|
+
vals, _shape_of(self._inst), was_bytes=kind == 'S'))
|
|
1164
|
+
return sub._apply(op, [chars])
|
|
1165
|
+
return self._apply(op, [chars])
|
|
1166
|
+
|
|
1167
|
+
def center(self, width, fillchar=' '):
|
|
1168
|
+
return _pad_op('center', self, width, fillchar)
|
|
1169
|
+
|
|
1170
|
+
def ljust(self, width, fillchar=' '):
|
|
1171
|
+
return _pad_op('ljust', self, width, fillchar)
|
|
1172
|
+
|
|
1173
|
+
def rjust(self, width, fillchar=' '):
|
|
1174
|
+
return _pad_op('rjust', self, width, fillchar)
|
|
1175
|
+
|
|
1176
|
+
def zfill(self, width):
|
|
1177
|
+
return _pad_op('zfill', self, width)
|
|
1178
|
+
|
|
1179
|
+
def expandtabs(self, tabsize=8):
|
|
1180
|
+
# Python 层实现(Rust 对含 \0 元素的处理与 不一致)。
|
|
1181
|
+
vals = _decode(self._vals())
|
|
1182
|
+
out = [v.expandtabs(tabsize) for v in vals]
|
|
1183
|
+
try:
|
|
1184
|
+
wb = self._inst.dtype.kind == 'S'
|
|
1185
|
+
except Exception:
|
|
1186
|
+
wb = False
|
|
1187
|
+
return self._pack(out, _shape_of(self._inst), was_bytes=wb)
|
|
1188
|
+
|
|
1189
|
+
def replace(self, old, new, count=None):
|
|
1190
|
+
return replace(self, old, new, count)
|
|
1191
|
+
|
|
1192
|
+
def _find_all(self, op, sub, start, end):
|
|
1193
|
+
# 统一 Python 层:支持标量/数组 sub 与 start/end 边界。
|
|
1194
|
+
vals = _decode(self._vals())
|
|
1195
|
+
if isinstance(sub, (str, bytes)):
|
|
1196
|
+
s_ = (sub.decode('utf-8', 'surrogateescape')
|
|
1197
|
+
if isinstance(sub, bytes) else sub)
|
|
1198
|
+
subs = [s_]
|
|
1199
|
+
else:
|
|
1200
|
+
subs = _decode(_get_strings(sub))
|
|
1201
|
+
out = [getattr(v, op)(subs[i % builtins.len(subs)], start, end)
|
|
1202
|
+
for i, v in builtins.enumerate(vals)]
|
|
1203
|
+
return self._pack(out, _shape_of(self._inst), as_int=True)
|
|
1204
|
+
|
|
1205
|
+
def find(self, sub, start=0, end=None):
|
|
1206
|
+
return self._find_all('find', sub, start, end)
|
|
1207
|
+
|
|
1208
|
+
def rfind(self, sub, start=0, end=None):
|
|
1209
|
+
return self._find_all('rfind', sub, start, end)
|
|
1210
|
+
|
|
1211
|
+
def index(self, sub, start=0, end=None):
|
|
1212
|
+
r = self.find(sub, start, end)
|
|
1213
|
+
flat = _flatten(builtins.list(r.tolist()), [])
|
|
1214
|
+
if builtins.any(v == -1 for v in flat):
|
|
1215
|
+
raise ValueError('substring not found')
|
|
1216
|
+
return r
|
|
1217
|
+
|
|
1218
|
+
def rindex(self, sub, start=0, end=None):
|
|
1219
|
+
r = self.rfind(sub, start, end)
|
|
1220
|
+
flat = _flatten(builtins.list(r.tolist()), [])
|
|
1221
|
+
if builtins.any(v == -1 for v in flat):
|
|
1222
|
+
raise ValueError('substring not found')
|
|
1223
|
+
return r
|
|
1224
|
+
|
|
1225
|
+
def count(self, sub, start=0, end=None):
|
|
1226
|
+
return self._find_all('count', sub, start, end)
|
|
1227
|
+
|
|
1228
|
+
def _prefix_py(self, op, prefix, start, end):
|
|
1229
|
+
for s_ in (start, end):
|
|
1230
|
+
if s_ is not None and not isinstance(s_, (int,)):
|
|
1231
|
+
raise TypeError(
|
|
1232
|
+
op + ': start/end must be integers or None')
|
|
1233
|
+
if start is None:
|
|
1234
|
+
start = 0
|
|
1235
|
+
if start != 0 or end is not None:
|
|
1236
|
+
vals = _decode(self._vals())
|
|
1237
|
+
if isinstance(prefix, (str, bytes)):
|
|
1238
|
+
p = (prefix.decode('utf-8', 'surrogateescape')
|
|
1239
|
+
if isinstance(prefix, bytes) else prefix)
|
|
1240
|
+
out = [getattr(v[start:end], op)(p) for v in vals]
|
|
1241
|
+
else:
|
|
1242
|
+
ps = _decode(_get_strings(prefix))
|
|
1243
|
+
out = [getattr(v[start:end], op)(ps[i % builtins.len(ps)])
|
|
1244
|
+
for i, v in builtins.enumerate(vals)]
|
|
1245
|
+
return self._pack(out, _shape_of(self._inst), as_bool=True)
|
|
1246
|
+
return self._apply(op, [prefix])
|
|
1247
|
+
|
|
1248
|
+
def startswith(self, prefix, start=None, end=None):
|
|
1249
|
+
return self._prefix_py('startswith', prefix, start, end)
|
|
1250
|
+
|
|
1251
|
+
def endswith(self, suffix, start=None, end=None):
|
|
1252
|
+
return self._prefix_py('endswith', suffix, start, end)
|
|
1253
|
+
|
|
1254
|
+
def isalnum(self):
|
|
1255
|
+
return self._apply('isalnum', [])
|
|
1256
|
+
|
|
1257
|
+
def isalpha(self):
|
|
1258
|
+
return self._apply('isalpha', [])
|
|
1259
|
+
|
|
1260
|
+
def isdecimal(self):
|
|
1261
|
+
try:
|
|
1262
|
+
if self._inst.dtype.kind == 'S':
|
|
1263
|
+
raise TypeError(
|
|
1264
|
+
'isdecimal() not supported for byte-string arrays')
|
|
1265
|
+
except AttributeError:
|
|
1266
|
+
pass
|
|
1267
|
+
return self._apply('isdecimal', [])
|
|
1268
|
+
|
|
1269
|
+
def isdigit(self):
|
|
1270
|
+
return self._apply('isdigit', [])
|
|
1271
|
+
|
|
1272
|
+
def islower(self):
|
|
1273
|
+
return self._apply('islower', [])
|
|
1274
|
+
|
|
1275
|
+
def isnumeric(self):
|
|
1276
|
+
try:
|
|
1277
|
+
if self._inst.dtype.kind == 'S':
|
|
1278
|
+
raise TypeError(
|
|
1279
|
+
'isnumeric() not supported for byte-string arrays')
|
|
1280
|
+
except AttributeError:
|
|
1281
|
+
pass
|
|
1282
|
+
return self._apply('isnumeric', [])
|
|
1283
|
+
|
|
1284
|
+
def isspace(self):
|
|
1285
|
+
return self._apply('isspace', [])
|
|
1286
|
+
|
|
1287
|
+
def istitle(self):
|
|
1288
|
+
return self._apply('istitle', [])
|
|
1289
|
+
|
|
1290
|
+
def isupper(self):
|
|
1291
|
+
return self._apply('isupper', [])
|
|
1292
|
+
|
|
1293
|
+
def join(self, seq):
|
|
1294
|
+
return join(self, seq)
|
|
1295
|
+
|
|
1296
|
+
def mod(self, values):
|
|
1297
|
+
return mod(self, values)
|
|
1298
|
+
|
|
1299
|
+
def multiply(self, i):
|
|
1300
|
+
return multiply(self, i)
|
|
1301
|
+
|
|
1302
|
+
def split(self, sep=None, maxsplit=None):
|
|
1303
|
+
return split(self, sep, maxsplit)
|
|
1304
|
+
|
|
1305
|
+
def rsplit(self, sep=None, maxsplit=None):
|
|
1306
|
+
return rsplit(self, sep, maxsplit)
|
|
1307
|
+
|
|
1308
|
+
def splitlines(self, keepends=False):
|
|
1309
|
+
return splitlines(self, keepends)
|
|
1310
|
+
|
|
1311
|
+
def decode(self, encoding='utf-8', errors='strict'):
|
|
1312
|
+
return decode(self, encoding, errors)
|
|
1313
|
+
|
|
1314
|
+
def encode(self, encoding='utf-8', errors='strict'):
|
|
1315
|
+
return encode(self, encoding, errors)
|
|
1316
|
+
|
|
1317
|
+
def partition(self, sep):
|
|
1318
|
+
return partition(self, sep)
|
|
1319
|
+
|
|
1320
|
+
def rpartition(self, sep):
|
|
1321
|
+
return rpartition(self, sep)
|
|
1322
|
+
|
|
1323
|
+
def __len__(self):
|
|
1324
|
+
return builtins.len(self._inst)
|
|
1325
|
+
|
|
1326
|
+
def __iter__(self):
|
|
1327
|
+
return iter(self._inst)
|
|
1328
|
+
|
|
1329
|
+
def __getitem__(self, key):
|
|
1330
|
+
inst = self._inst
|
|
1331
|
+
shape = _shape_of(inst)
|
|
1332
|
+
ndim = builtins.len(shape)
|
|
1333
|
+
|
|
1334
|
+
def _trim_tuple(v):
|
|
1335
|
+
# S/U 元组索引 getitem:C 字符串截断(含 \x00)+
|
|
1336
|
+
# 尾部空白去除(在 \x00 处停止)。
|
|
1337
|
+
if isinstance(v, str):
|
|
1338
|
+
parts = v.split('\x00', 1)
|
|
1339
|
+
s = parts[0] + '\x00' if builtins.len(parts) > 1 else parts[0]
|
|
1340
|
+
return s.rstrip()
|
|
1341
|
+
if isinstance(v, bytes):
|
|
1342
|
+
parts = v.split(b'\x00', 1)
|
|
1343
|
+
s = parts[0] + b'\x00' if builtins.len(parts) > 1 else parts[0]
|
|
1344
|
+
return s.rstrip()
|
|
1345
|
+
return v
|
|
1346
|
+
|
|
1347
|
+
if isinstance(key, (int,)) and not isinstance(key, bool):
|
|
1348
|
+
if ndim > 1:
|
|
1349
|
+
# —n 维(n>1)整数索引 → (n-1) 维子数组。
|
|
1350
|
+
# rsarray 的 ndarray [int] 返回标量——此处重建行。
|
|
1351
|
+
flat = self._vals()
|
|
1352
|
+
stride = 1
|
|
1353
|
+
for d in shape[1:]:
|
|
1354
|
+
stride *= d
|
|
1355
|
+
k = key % shape[0]
|
|
1356
|
+
row = flat[k * stride:(k + 1) * stride]
|
|
1357
|
+
kind = getattr(getattr(inst, 'dtype', None), 'kind', 'U')
|
|
1358
|
+
if kind not in ('S', 'U'):
|
|
1359
|
+
kind = 'U'
|
|
1360
|
+
from .__init__ import array as _array
|
|
1361
|
+
sub = _array(_nest(row, shape[1:]), dtype=kind)
|
|
1362
|
+
return _as_chararray(sub)
|
|
1363
|
+
return inst[key]
|
|
1364
|
+
if (isinstance(key, tuple)
|
|
1365
|
+
and builtins.all(
|
|
1366
|
+
isinstance(k, (int,)) and not isinstance(k, bool)
|
|
1367
|
+
for k in key)):
|
|
1368
|
+
# S/U 元组整数 getitem 可能返回占位 0-d 数组——从 _vals 取标量。
|
|
1369
|
+
flat = self._vals()
|
|
1370
|
+
if builtins.len(key) == ndim:
|
|
1371
|
+
idx = 0
|
|
1372
|
+
for k, d in builtins.zip(key, shape):
|
|
1373
|
+
idx = idx * d + (k % d)
|
|
1374
|
+
if idx < builtins.len(flat):
|
|
1375
|
+
return _trim_tuple(flat[idx])
|
|
1376
|
+
return _trim_tuple(inst[key])
|
|
1377
|
+
# S/U 一维切片:从 _vals 重建(Rust 切片路径可能返回占位值)。
|
|
1378
|
+
_k1 = getattr(getattr(inst, 'dtype', None), 'kind', '')
|
|
1379
|
+
if isinstance(key, slice) and ndim == 1 and _k1 in ('S', 'U'):
|
|
1380
|
+
flat = self._vals()
|
|
1381
|
+
sel = flat[key]
|
|
1382
|
+
from .__init__ import array as _array
|
|
1383
|
+
sub = _array(sel, dtype=_k1)
|
|
1384
|
+
sub.base = self
|
|
1385
|
+
return sub
|
|
1386
|
+
# 全切片:从 _vals 重建 S/U 原始数据(Rust 切片路径返回占位值)。
|
|
1387
|
+
is_full = False
|
|
1388
|
+
if isinstance(key, slice):
|
|
1389
|
+
# 任意维度的 arr[:](省略维度视为整维)都是全切片。
|
|
1390
|
+
is_full = (key.start is None and key.stop is None
|
|
1391
|
+
and key.step in (None, 1))
|
|
1392
|
+
elif isinstance(key, tuple):
|
|
1393
|
+
is_full = builtins.all(
|
|
1394
|
+
isinstance(k, slice) and k.start is None and k.stop is None
|
|
1395
|
+
and k.step in (None, 1) for k in key)
|
|
1396
|
+
if is_full:
|
|
1397
|
+
kind = getattr(getattr(inst, 'dtype', None), 'kind', 'U')
|
|
1398
|
+
if kind in ('S', 'U'):
|
|
1399
|
+
flat = self._vals()
|
|
1400
|
+
from .__init__ import array as _array
|
|
1401
|
+
sub = _array(_nest(flat, shape), dtype=kind)
|
|
1402
|
+
sub.base = self
|
|
1403
|
+
return sub
|
|
1404
|
+
v = inst[key]
|
|
1405
|
+
if hasattr(v, 'base'):
|
|
1406
|
+
try:
|
|
1407
|
+
v.base = self
|
|
1408
|
+
except Exception:
|
|
1409
|
+
pass
|
|
1410
|
+
return v
|
|
1411
|
+
|
|
1412
|
+
def __setitem__(self, key, value):
|
|
1413
|
+
try:
|
|
1414
|
+
kind = self._inst.dtype.kind
|
|
1415
|
+
except Exception:
|
|
1416
|
+
kind = 'U'
|
|
1417
|
+
if kind in 'SU':
|
|
1418
|
+
shape = _shape_of(self._inst)
|
|
1419
|
+
ndim = builtins.len(shape)
|
|
1420
|
+
idxs = None
|
|
1421
|
+
if isinstance(key, (int,)) and not isinstance(key, bool):
|
|
1422
|
+
if ndim == 1:
|
|
1423
|
+
idxs = [key % shape[0]]
|
|
1424
|
+
elif ndim > 1:
|
|
1425
|
+
stride = 1
|
|
1426
|
+
for d in shape[1:]:
|
|
1427
|
+
stride *= d
|
|
1428
|
+
k = key % shape[0]
|
|
1429
|
+
idxs = list(builtins.range(k * stride, (k + 1) * stride))
|
|
1430
|
+
elif (isinstance(key, tuple)
|
|
1431
|
+
and builtins.len(key) == ndim
|
|
1432
|
+
and builtins.all(
|
|
1433
|
+
isinstance(k, (int,)) and not isinstance(k, bool)
|
|
1434
|
+
for k in key)):
|
|
1435
|
+
idx = 0
|
|
1436
|
+
for k, d in builtins.zip(key, shape):
|
|
1437
|
+
idx = idx * d + (k % d)
|
|
1438
|
+
idxs = [idx]
|
|
1439
|
+
if idxs is not None:
|
|
1440
|
+
flat = self._vals()
|
|
1441
|
+
for i in idxs:
|
|
1442
|
+
if i < builtins.len(flat):
|
|
1443
|
+
if kind == 'S':
|
|
1444
|
+
flat[i] = (value if isinstance(value, bytes)
|
|
1445
|
+
else str(value).encode(
|
|
1446
|
+
'utf-8', 'surrogateescape'))
|
|
1447
|
+
else:
|
|
1448
|
+
flat[i] = (value if isinstance(value, str)
|
|
1449
|
+
else (value.decode(
|
|
1450
|
+
'utf-8', 'surrogateescape')
|
|
1451
|
+
if isinstance(
|
|
1452
|
+
value, (bytes, bytearray))
|
|
1453
|
+
else str(value)))
|
|
1454
|
+
self._inst._raw_data = list(flat)
|
|
1455
|
+
sd = getattr(self._inst, '_str_dtype', None)
|
|
1456
|
+
w = builtins.max((builtins.len(v) for v in flat), default=0)
|
|
1457
|
+
if sd is not None:
|
|
1458
|
+
self._inst._str_dtype = (
|
|
1459
|
+
sd[0], builtins.max(sd[1], w))
|
|
1460
|
+
return
|
|
1461
|
+
self._inst[key] = value
|
|
1462
|
+
|
|
1463
|
+
def __add__(self, other):
|
|
1464
|
+
return add(self, other)
|
|
1465
|
+
|
|
1466
|
+
def __radd__(self, other):
|
|
1467
|
+
return add(other, self)
|
|
1468
|
+
|
|
1469
|
+
def __mul__(self, other):
|
|
1470
|
+
# chararray 语义:仅整数倍重复,非整数 → ValueError
|
|
1471
|
+
if not isinstance(other, int) or isinstance(other, bool):
|
|
1472
|
+
raise ValueError("Can only multiply by integers")
|
|
1473
|
+
return multiply(self, other)
|
|
1474
|
+
|
|
1475
|
+
def __rmul__(self, other):
|
|
1476
|
+
if not isinstance(other, int) or isinstance(other, bool):
|
|
1477
|
+
raise ValueError("Can only multiply by integers")
|
|
1478
|
+
return multiply(self, other)
|
|
1479
|
+
|
|
1480
|
+
def __mod__(self, other):
|
|
1481
|
+
return mod(self, other)
|
|
1482
|
+
|
|
1483
|
+
def _cmp(self, other, op):
|
|
1484
|
+
# chararray 语义:比较前对两侧值 strip 尾部空格
|
|
1485
|
+
# ('123' == '123 ' 为 True,'123' < '123 ' 为 False)
|
|
1486
|
+
a_vals = _decode(self._vals())
|
|
1487
|
+
if hasattr(other, '_vals'):
|
|
1488
|
+
b_vals = _decode(other._vals())
|
|
1489
|
+
elif hasattr(other, 'tolist'):
|
|
1490
|
+
b_vals = _decode(_flatten(other.tolist(), []))
|
|
1491
|
+
else:
|
|
1492
|
+
b_vals = [_as_str_arg(other)] * builtins.len(a_vals)
|
|
1493
|
+
ops = {'equal': lambda x, y: x == y,
|
|
1494
|
+
'not_equal': lambda x, y: x != y,
|
|
1495
|
+
'greater': lambda x, y: x > y,
|
|
1496
|
+
'less': lambda x, y: x < y,
|
|
1497
|
+
'greater_equal': lambda x, y: x >= y,
|
|
1498
|
+
'less_equal': lambda x, y: x <= y}
|
|
1499
|
+
_fn = ops.get(op)
|
|
1500
|
+
if _fn is None:
|
|
1501
|
+
return _bool_op(op, self, other=other)
|
|
1502
|
+
shape = _shape_of(self._inst)
|
|
1503
|
+
a_t = [v.rstrip(' ') if isinstance(v, str) else v for v in a_vals]
|
|
1504
|
+
b_t = [v.rstrip(' ') if isinstance(v, str) else v for v in b_vals]
|
|
1505
|
+
n = builtins.len(a_t)
|
|
1506
|
+
if builtins.len(b_t) == 1 and n != 1:
|
|
1507
|
+
b_t = b_t * n
|
|
1508
|
+
res = [_fn(x, y) for x, y in builtins.zip(a_t, b_t)]
|
|
1509
|
+
np = _np_mod()
|
|
1510
|
+
out = np.array(res, dtype='bool')
|
|
1511
|
+
return out.reshape(shape) if shape else out
|
|
1512
|
+
|
|
1513
|
+
def __eq__(self, other):
|
|
1514
|
+
return self._cmp(other, 'equal')
|
|
1515
|
+
|
|
1516
|
+
def __ne__(self, other):
|
|
1517
|
+
return self._cmp(other, 'not_equal')
|
|
1518
|
+
|
|
1519
|
+
def __gt__(self, other):
|
|
1520
|
+
return self._cmp(other, 'greater')
|
|
1521
|
+
|
|
1522
|
+
def __lt__(self, other):
|
|
1523
|
+
return self._cmp(other, 'less')
|
|
1524
|
+
|
|
1525
|
+
def __ge__(self, other):
|
|
1526
|
+
return self._cmp(other, 'greater_equal')
|
|
1527
|
+
|
|
1528
|
+
def __le__(self, other):
|
|
1529
|
+
return self._cmp(other, 'less_equal')
|
|
1530
|
+
|
|
1531
|
+
def __bool__(self):
|
|
1532
|
+
v = self._vals()
|
|
1533
|
+
return builtins.bool(v[0]) if builtins.len(v) == 1 else builtins.bool(self._inst.size)
|
|
1534
|
+
|
|
1535
|
+
|
|
1536
|
+
def _as_chararray(inst):
|
|
1537
|
+
"""把 ndarray 实例包装为 chararray(组合代理)。"""
|
|
1538
|
+
wrapper = object.__new__(chararray)
|
|
1539
|
+
wrapper._inst = inst
|
|
1540
|
+
return wrapper
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
def array(obj, itemsize=None, copy=True, unicode=None, order=None):
|
|
1544
|
+
"""创建 chararray(np.char.array)。"""
|
|
1545
|
+
return chararray(obj, itemsize=itemsize, copy=copy, unicode=unicode,
|
|
1546
|
+
order=order)
|
|
1547
|
+
|
|
1548
|
+
|
|
1549
|
+
def asarray(obj, itemsize=None, unicode=None):
|
|
1550
|
+
"""创建 chararray(np.char.asarray,视图语义——共享数据)。"""
|
|
1551
|
+
return chararray(obj, itemsize=itemsize, copy=False, unicode=unicode)
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
def compare_chararrays(a1, a2, cmp='asciibetical', rstrip=False):
|
|
1555
|
+
"""逐元素比较字符数组(char.compare_chararrays 兼容)。
|
|
1556
|
+
|
|
1557
|
+
cmp 接受比较符号('=='/'!='/'<'/'<='/'>'/>=',2.x ufunc
|
|
1558
|
+
语义)或 'asciibetical'/'lexicographic'(legacy,均按码点比较,
|
|
1559
|
+
默认语义为 a > b)。
|
|
1560
|
+
"""
|
|
1561
|
+
import operator as _pyop
|
|
1562
|
+
import rsarray as np
|
|
1563
|
+
_ops = {'==': _pyop.eq, '!=': _pyop.ne, '<': _pyop.lt,
|
|
1564
|
+
'<=': _pyop.le, '>': _pyop.gt, '>=': _pyop.ge}
|
|
1565
|
+
if cmp in _ops:
|
|
1566
|
+
op = _ops[cmp]
|
|
1567
|
+
elif cmp in ('asciibetical', 'lexicographic'):
|
|
1568
|
+
op = _pyop.gt
|
|
1569
|
+
else:
|
|
1570
|
+
raise ValueError("cmp must be 'asciibetical' or 'lexicographic'")
|
|
1571
|
+
arr1 = np.asarray(a1)
|
|
1572
|
+
arr2 = np.asarray(a2)
|
|
1573
|
+
x1 = _decode(_get_strings(arr1))
|
|
1574
|
+
x2 = _decode(_get_strings(arr2))
|
|
1575
|
+
if rstrip:
|
|
1576
|
+
x1 = [v.rstrip() for v in x1]
|
|
1577
|
+
x2 = [v.rstrip() for v in x2]
|
|
1578
|
+
sh1 = _shape_of(arr1)
|
|
1579
|
+
sh2 = _shape_of(arr2)
|
|
1580
|
+
bshape = _bc_shapes([sh1, sh2])
|
|
1581
|
+
res = [op(_bc_get(x1, sh1, idx), _bc_get(x2, sh2, idx))
|
|
1582
|
+
for idx in _iter_indices(bshape)]
|
|
1583
|
+
out = np.asarray(res, dtype='bool')
|
|
1584
|
+
if bshape:
|
|
1585
|
+
out = out.reshape(bshape)
|
|
1586
|
+
return out
|
|
1587
|
+
|
|
1588
|
+
|
|
1589
|
+
compare_chararrays.__text_signature__ = (
|
|
1590
|
+
"(a1, a2, cmp='asciibetical', rstrip=False)")
|