Skip to content

Commit 7eed8f9

Browse files
Update src/Nethermind/Nethermind.Runner/scripts/utilities.ts
Co-authored-by: Lautaro Emanuel <[email protected]>
1 parent 8c039eb commit 7eed8f9

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/Nethermind/Nethermind.Runner/scripts/utilities.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ export function parseExtraData(extraData: string | null | undefined): string {
8181
// Convert hex -> bytes
8282
const bytes = hexStringToUint8Array(hex);
8383

84-
// Use the "toCleanUtf8String" logic
85-
const data = toCleanUtf8String(bytes);
86-
87-
// The C# snippet's comment says:
88-
// "If the cleaned text is less than half length of input size, output it as hex, else output the text."
89-
// The original code in the snippet has a minor mismatch in the condition, but we’ll follow the comment:
90-
if (data.length > (bytes.length / 2)) {
91-
// It's "mostly" valid text
92-
return data;
93-
} else {
94-
// It's "mostly" not decodable text => show hex
84+
// Decode to UTF-8
85+
let decoder = new TextDecoder('utf-8', { fatal: false });
86+
let decoded = decoder.decode(bytes);
87+
88+
// Count the number of control characters
89+
let controlCount = decoded.split('').filter(c => isControlCharacter(c.charCodeAt(0))).length;
90+
91+
// Return the original hex string if there are too many control characters
92+
if (controlCount >= decoded.length / 2) {
9593
return `0x${hex}`;
96-
}
94+
95+
// Return the decoded string
96+
return decoded;
9797
}
9898

9999
/**

0 commit comments

Comments
 (0)