Returns the position of the first occurrence of needle in the haystack. If haystack doesn't contain needle, it returns -1.
| 93 | // Returns the position of the first occurrence of needle in the haystack. If haystack doesn't |
| 94 | // contain needle, it returns -1. |
| 95 | int64_t Find::find(const uint8_t* haystack, uint32_t haystackLen, const uint8_t* needle, |
| 96 | uint32_t needleLen) { |
| 97 | auto firstMatchCharPos = (uint8_t*)memchr(haystack, needle[0], haystackLen); |
| 98 | if (firstMatchCharPos == nullptr) { |
| 99 | return -1; |
| 100 | } |
| 101 | auto firstMatchCharOffset = firstMatchCharPos - haystack; |
| 102 | auto numCharsToMatch = haystackLen - firstMatchCharOffset; |
| 103 | switch (needleLen) { |
| 104 | case 1: |
| 105 | return firstMatchCharOffset; |
| 106 | case 2: |
| 107 | return alignedNeedleSizeFind<uint16_t>(firstMatchCharPos, numCharsToMatch, needle, |
| 108 | firstMatchCharOffset); |
| 109 | case 3: |
| 110 | return unalignedNeedleSizeFind<uint32_t>(firstMatchCharPos, numCharsToMatch, needle, 3, |
| 111 | firstMatchCharOffset); |
| 112 | case 4: |
| 113 | return alignedNeedleSizeFind<uint32_t>(firstMatchCharPos, numCharsToMatch, needle, |
| 114 | firstMatchCharOffset); |
| 115 | case 5: |
| 116 | case 6: |
| 117 | case 7: |
| 118 | return unalignedNeedleSizeFind<uint64_t>(firstMatchCharPos, numCharsToMatch, needle, |
| 119 | needleLen, firstMatchCharOffset); |
| 120 | case 8: |
| 121 | return alignedNeedleSizeFind<uint64_t>(firstMatchCharPos, numCharsToMatch, needle, |
| 122 | firstMatchCharOffset); |
| 123 | default: |
| 124 | return genericFind(firstMatchCharPos, numCharsToMatch, needle, needleLen, |
| 125 | firstMatchCharOffset); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } // namespace function |
| 130 | } // namespace lbug |
no outgoing calls