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