MCPcopy Create free account
hub / github.com/NomicFoundation/hardhat / bytesIncludesUtf8String

Function bytesIncludesUtf8String

packages/hardhat-utils/src/bytes.ts:74–108  ·  view source on GitHub ↗
(
  haystack: Uint8Array,
  needle: string,
)

Source from the content-addressed store, hash-verified

72 * @returns True if the needle's byte sequence is found in the haystack.
73 */
74export 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.

Callers 3

bytes.tsFile · 0.85

Calls 2

utf8StringToBytesFunction · 0.85
buildLpsTableFunction · 0.85

Tested by

no test coverage detected