MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / strEscape

Function strEscape

python/pythonmonkey/builtin_modules/util.js:172–207  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

170// Escape control characters, single quotes and the backslash.
171// This is similar to JSON stringify escaping.
172function strEscape(str)
173{
174 // Some magic numbers that worked out fine while benchmarking with v8 6.0
175 if (str.length < 5000 && !strEscapeSequencesRegExp.test(str))
176 return `'${str}'`;
177 if (str.length > 100)
178 return `'${str.replace(strEscapeSequencesReplacer, escapeFn)}'`;
179 let result = '';
180 let last = 0;
181 let i;
182 for (i = 0; i < str.length; i++)
183 {
184 const point = str.charCodeAt(i);
185 if (point === 39 || point === 92 || point < 32)
186 {
187 if (last === i)
188 {
189 result += meta[point];
190 }
191 else
192 {
193 result += `${str.slice(last, i)}${meta[point]}`;
194 }
195 last = i + 1;
196 }
197 }
198 if (last === 0)
199 {
200 result = str;
201 }
202 else if (last !== i)
203 {
204 result += str.slice(last);
205 }
206 return `'${result}'`;
207}
208
209function tryStringify(arg)
210{

Callers 2

formatPrimitiveFunction · 0.85
formatPropertyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected