MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / decodeMostlyUtf8

Function decodeMostlyUtf8

src/pkg/utils/encoding.ts:142–161  ·  view source on GitHub ↗
(data: Uint8Array)

Source from the content-addressed store, hash-verified

140};
141
142const decodeMostlyUtf8 = (data: Uint8Array): string | null => {
143 const decoded = new TextDecoder("utf-8").decode(data);
144 let replacements = 0;
145 let nonAsciiSignals = 0;
146
147 for (let i = 0; i < decoded.length; i++) {
148 const code = decoded.charCodeAt(i);
149 if (code === 0xfffd) {
150 replacements++;
151 } else if (code > 0x7f) {
152 nonAsciiSignals++;
153 }
154 }
155
156 if (nonAsciiSignals >= replacements * 4 && replacements > 0 && replacements <= 8) {
157 return decoded;
158 }
159
160 return null;
161};
162
163const hasSuspiciousDecodedControlChars = (text: string): boolean => {
164 let controls = 0;

Callers 1

readRawContentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected