Skip to content

Commit 73b3130

Browse files
committed
Extract calculation of offset from pointer
1 parent 9e0e8d5 commit 73b3130

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,19 @@ static const unsigned char *mbfl_find_offset_utf8(const mbfl_string *str, ssize_
840840
}
841841
}
842842

843+
static size_t mbfl_pointer_to_offset_utf8(const unsigned char *start, const unsigned char *pos) {
844+
size_t result = 0;
845+
while (pos > start) {
846+
unsigned char c = *--pos;
847+
if (c < 0x80) {
848+
++result;
849+
} else if ((c & 0xc0) != 0x80) {
850+
++result;
851+
}
852+
}
853+
return result;
854+
}
855+
843856
size_t
844857
mbfl_strpos(
845858
mbfl_string *haystack,
@@ -920,15 +933,7 @@ mbfl_strpos(
920933
q = needle_u8_val + needle_u8_len;
921934
for (;;) {
922935
if (q == needle_u8_val) {
923-
result = 0;
924-
while (p > haystack_u8_val) {
925-
unsigned char c = *--p;
926-
if (c < 0x80) {
927-
++result;
928-
} else if ((c & 0xc0) != 0x80) {
929-
++result;
930-
}
931-
}
936+
result = mbfl_pointer_to_offset_utf8(haystack_u8_val, p);
932937
goto out;
933938
}
934939
if (*--q != *--p) {
@@ -999,16 +1004,8 @@ mbfl_strpos(
9991004
q = needle_u8_val;
10001005
for (;;) {
10011006
if (q == qe) {
1002-
result = 0;
10031007
p -= needle_u8_len;
1004-
while (p > haystack_u8_val) {
1005-
unsigned char c = *--p;
1006-
if (c < 0x80) {
1007-
++result;
1008-
} else if ((c & 0xc0) != 0x80) {
1009-
++result;
1010-
}
1011-
}
1008+
result = mbfl_pointer_to_offset_utf8(haystack_u8_val, p);
10121009
goto out;
10131010
}
10141011
if (*q != *p) {

0 commit comments

Comments
 (0)