( haystack: Uint8Array, needle: string, )
| 72 | * @returns True if the needle's byte sequence is found in the haystack. |
| 73 | */ |
| 74 | export function bytesIncludesUtf8String( |
| 75 | haystack: Uint8Array, |
| 76 | needle: string, |
| 77 | ): boolean { |
| 78 | if (needle.length === 0) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | const needleBytes = utf8StringToBytes(needle); |
| 83 | const needleLen = needleBytes.length; |
| 84 | const haystackLen = haystack.length; |
| 85 | if (needleLen > haystackLen) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | const lps = buildLpsTable(needleBytes); |
| 90 | |
| 91 | let haystackI = 0; |
| 92 | let needleI = 0; |
| 93 | while (haystackI < haystackLen) { |
| 94 | if (haystack[haystackI] === needleBytes[needleI]) { |
| 95 | haystackI++; |
| 96 | needleI++; |
| 97 | if (needleI === needleLen) { |
| 98 | return true; |
| 99 | } |
| 100 | } else if (needleI > 0) { |
| 101 | needleI = lps[needleI - 1]; |
| 102 | } else { |
| 103 | haystackI++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Parses UTF-8 encoded JSON bytes into a value. |
no test coverage detected