| 1 | /* |
| 2 | * Copyright (c) 2015, Intel Corporation |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * * Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of Intel Corporation nor the names of its contributors |
| 13 | * may be used to endorse or promote products derived from this software |
| 14 | * without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "vermicelli.h" |
| 30 | |
| 31 | static really_inline |
| 32 | const u8 *find_xverm_run(char c, char nocase, u32 repeat, UNUSED const u8 *buf, |
| 33 | const u8 *buf_start, const u8 *buf_end, char negate) { |
| 34 | DEBUG_PRINTF("looking for 0x%hhx{%u} in %p [%zd, %zd)\n" , c, repeat, buf, |
| 35 | buf_start - buf, buf_end - buf); |
| 36 | |
| 37 | /* TODO optimise on where it is easy to get a dense bitfield of character |
| 38 | * matches */ |
| 39 | if (repeat == 1) { |
| 40 | return negate ? nvermicelliExec(c, nocase, buf_start, buf_end) |
| 41 | : vermicelliExec(c, nocase, buf_start, buf_end); |
| 42 | } |
| 43 | |
| 44 | while (1) { |
| 45 | const u8 *s; |
| 46 | if (negate) { |
| 47 | s = nvermicelliExec(c, nocase, buf_start, buf_end); |
| 48 | } else if (buf_end - buf_start >= VERM_BOUNDARY && !nocase) { |
| 49 | s = vermicelliDoubleExec(c, c, nocase, buf_start, buf_end); |
| 50 | |
| 51 | if (s != buf_end && *s != c) { /* double verm is not certain to be |
| 52 | * precise */ |
| 53 | s = vermicelliExec(c, nocase, s, buf_end); |
| 54 | } |
| 55 | } else { |
| 56 | s = vermicelliExec(c, nocase, buf_start, buf_end); |
| 57 | } |
| 58 | if (s == buf_end) { |
| 59 | return s; |
| 60 | } |
| 61 | |
| 62 | DEBUG_PRINTF("cand %zd\n" , s - buf); |
| 63 | |
| 64 | const u8 *test_e = MIN(s + repeat, buf_end); |
| 65 | |
| 66 | const u8 *rv = negate ? vermicelliExec(c, nocase, s, test_e) |
| 67 | : nvermicelliExec(c, nocase, s, test_e); |
| 68 | |
| 69 | assert(rv > buf_start); |
| 70 | assert(rv <= buf_end); |
| 71 | |
| 72 | if (rv == test_e) { |
| 73 | return s; |
| 74 | } |
| 75 | |
| 76 | buf_start = rv; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static really_inline |
| 81 | const u8 *find_verm_run(char c, char nocase, u32 repeat, const u8 *buf, |
| 82 | const u8 *buf_start, const u8 *buf_end) { |
| 83 | return find_xverm_run(c, nocase, repeat, buf, buf_start, buf_end, 0); |
| 84 | } |
| 85 | |
| 86 | static really_inline |
| 87 | const u8 *find_nverm_run(char c, char nocase, u32 repeat, const u8 *buf, |
| 88 | const u8 *buf_start, const u8 *buf_end) { |
| 89 | return find_xverm_run(c, nocase, repeat, buf, buf_start, buf_end, 1); |
| 90 | } |
| 91 | |