diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-01-24 11:15:58 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-01-24 11:15:58 +0100 |
commit | 73b31302ed210ea4fc7389433ead8b324b799cee (patch) | |
tree | c8515b9b44a524c0e00f277916dd09bbab2f5a64 | |
parent | 9e0e8d5650b6b810849c2fe6cce353ae150b1429 (diff) | |
download | php-git-73b31302ed210ea4fc7389433ead8b324b799cee.tar.gz |
Extract calculation of offset from pointer
-rw-r--r-- | ext/mbstring/libmbfl/mbfl/mbfilter.c | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/ext/mbstring/libmbfl/mbfl/mbfilter.c b/ext/mbstring/libmbfl/mbfl/mbfilter.c index 3a3de216f3..071e07c831 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfilter.c +++ b/ext/mbstring/libmbfl/mbfl/mbfilter.c @@ -840,6 +840,19 @@ static const unsigned char *mbfl_find_offset_utf8(const mbfl_string *str, ssize_ } } +static size_t mbfl_pointer_to_offset_utf8(const unsigned char *start, const unsigned char *pos) { + size_t result = 0; + while (pos > start) { + unsigned char c = *--pos; + if (c < 0x80) { + ++result; + } else if ((c & 0xc0) != 0x80) { + ++result; + } + } + return result; +} + size_t mbfl_strpos( mbfl_string *haystack, @@ -920,15 +933,7 @@ mbfl_strpos( q = needle_u8_val + needle_u8_len; for (;;) { if (q == needle_u8_val) { - result = 0; - while (p > haystack_u8_val) { - unsigned char c = *--p; - if (c < 0x80) { - ++result; - } else if ((c & 0xc0) != 0x80) { - ++result; - } - } + result = mbfl_pointer_to_offset_utf8(haystack_u8_val, p); goto out; } if (*--q != *--p) { @@ -999,16 +1004,8 @@ mbfl_strpos( q = needle_u8_val; for (;;) { if (q == qe) { - result = 0; p -= needle_u8_len; - while (p > haystack_u8_val) { - unsigned char c = *--p; - if (c < 0x80) { - ++result; - } else if ((c & 0xc0) != 0x80) { - ++result; - } - } + result = mbfl_pointer_to_offset_utf8(haystack_u8_val, p); goto out; } if (*q != *p) { |