Skip to content

Commit e2d1fd3

Browse files
committed
Reimplement memrchr (for Mac)
1 parent f753317 commit e2d1fd3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/cstring.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
#include <Python.h>
22

3+
4+
/*
5+
* memrchr not available on some systems, so reimplement.
6+
*/
7+
const char *_memrchr(const char *s, int c, size_t n) {
8+
for(const char *p = s + n - 1; p >= s; --p) {
9+
if(*p == c)
10+
return p;
11+
}
12+
return NULL;
13+
}
14+
15+
16+
317
struct cstring {
418
PyObject_VAR_HEAD
519
Py_hash_t hash;
@@ -10,7 +24,6 @@ struct cstring {
1024
#define CSTRING_VALUE(self) (((struct cstring *)self)->value)
1125
#define CSTRING_VALUE_AT(self, i) (&CSTRING_VALUE(self)[(i)])
1226

13-
1427
static PyObject *_cstring_new(PyTypeObject *type, const char *value, size_t len) {
1528
struct cstring *new = (struct cstring *)type->tp_alloc(type, len + 1);
1629
new->hash = -1;
@@ -268,7 +281,7 @@ static const char *_substr_params_str(const struct _substr_params *params) {
268281
static const char *_substr_params_rstr(const struct _substr_params *params) {
269282
const char *p = params->end - params->substr_len + 1;
270283
for(;;) {
271-
p = memrchr(params->start, *params->substr, p - params->start);
284+
p = _memrchr(params->start, *params->substr, p - params->start);
272285
if(!p)
273286
goto done;
274287
if(memcmp(p, params->substr, params->substr_len) == 0)
@@ -316,6 +329,7 @@ PyObject *cstring_rfind(PyObject *self, PyObject *args) {
316329
return NULL;
317330

318331
const char *p = _substr_params_rstr(&params);
332+
319333
if(!p)
320334
return PyLong_FromLong(-1);
321335

0 commit comments

Comments
 (0)