@@ -17,6 +17,8 @@ from cpython cimport (
17
17
PyObject_RichCompareBool,
18
18
PyObject_RichCompare,
19
19
PyString_Check,
20
+ PyUnicode_Contains,
21
+ PyString_AsString,
20
22
Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
21
23
)
22
24
@@ -2290,10 +2292,12 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
2290
2292
"""
2291
2293
2292
2294
cdef:
2293
- str c
2295
+ char c
2296
+ bytes bc
2294
2297
bint neg= 0 , have_dot= 0 , have_value= 0 , have_hhmmss= 0
2295
2298
object current_unit= None
2296
- int64_t result= 0 , m= 0 , r
2299
+ Py_ssize_t i
2300
+ int64_t result= 0 , m, r
2297
2301
list number= [], frac= [], unit= []
2298
2302
2299
2303
# neg : tracks if we have a leading negative for the value
@@ -2304,38 +2308,39 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
2304
2308
if ts in _nat_strings or not len (ts):
2305
2309
return iNaT
2306
2310
2307
- for c in ts:
2311
+ for c in PyString_AsString(ts):
2312
+ bc = < bytes> c
2308
2313
2309
2314
# skip whitespace / commas
2310
- if c == ' ' or c == ' ,' :
2315
+ if bc == ' ' or bc == ' ,' :
2311
2316
pass
2312
2317
2313
2318
# positive signs are ignored
2314
- elif c == ' +' :
2319
+ elif bc == ' +' :
2315
2320
pass
2316
2321
2317
2322
# neg
2318
- elif c == ' -' :
2323
+ elif bc == ' -' :
2319
2324
2320
2325
if neg or have_value or have_hhmmss:
2321
2326
raise ValueError (" only leading negative signs are allowed" )
2322
2327
2323
2328
neg = 1
2324
2329
2325
2330
# number (ascii codes)
2326
- elif ord (c) >= 48 and ord (c) <= 57 :
2331
+ elif c >= 48 and c <= 57 :
2327
2332
2328
2333
if have_dot:
2329
2334
2330
2335
# we found a dot, but now its just a fraction
2331
2336
if len (unit):
2332
- number.append(c )
2337
+ number.append(bc )
2333
2338
have_dot = 0
2334
2339
else :
2335
- frac.append(c )
2340
+ frac.append(bc )
2336
2341
2337
2342
elif not len (unit):
2338
- number.append(c )
2343
+ number.append(bc )
2339
2344
2340
2345
else :
2341
2346
@@ -2345,12 +2350,12 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
2345
2350
if coerce :
2346
2351
return iNaT
2347
2352
raise
2348
- unit, number, frac = [], [c ], []
2353
+ unit, number, frac = [], [bc ], []
2349
2354
2350
2355
result += timedelta_as_neg(r, neg)
2351
2356
2352
2357
# hh:mm:ss.
2353
- elif c == ' :' :
2358
+ elif bc == ' :' :
2354
2359
2355
2360
# we flip this off if we have a leading value
2356
2361
if have_value:
@@ -2377,7 +2382,7 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
2377
2382
unit, number = [], []
2378
2383
2379
2384
# after the decimal point
2380
- elif c == ' .' :
2385
+ elif bc == ' .' :
2381
2386
2382
2387
if len (number) and current_unit is not None :
2383
2388
@@ -2396,7 +2401,7 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
2396
2401
2397
2402
# unit
2398
2403
else :
2399
- unit.append(c )
2404
+ unit.append(bc )
2400
2405
have_value = 1
2401
2406
have_dot = 0
2402
2407
0 commit comments