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

Function bytesIncludesUtf8String

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

Source from the content-addressed store, hash-verified

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

Callers 2

bytes.tsFile · 0.85

Calls 2

utf8StringToBytesFunction · 0.85
buildLpsTableFunction · 0.85

Tested by

no test coverage detected