MCPcopy Create free account
hub / github.com/albertodemichelis/squirrel / _string_escape

Function _string_escape

sqstdlib/sqstdstring.cpp:288–356  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

286}
287
288static SQInteger _string_escape(HSQUIRRELVM v)
289{
290 const SQChar *str;
291 SQChar *dest,*resstr;
292 SQInteger size;
293 sq_getstring(v,2,&str);
294 size = sq_getsize(v,2);
295 if(size == 0) {
296 sq_push(v,2);
297 return 1;
298 }
299#ifdef SQUNICODE
300#if WCHAR_SIZE == 2
301 const SQChar *escpat = _SC("\\x%04x");
302 const SQInteger maxescsize = 6;
303#else //WCHAR_SIZE == 4
304 const SQChar *escpat = _SC("\\x%08x");
305 const SQInteger maxescsize = 10;
306#endif
307#else
308 const SQChar *escpat = _SC("\\x%02x");
309 const SQInteger maxescsize = 4;
310#endif
311 SQInteger destcharsize = (size * maxescsize); //assumes every char could be escaped
312 resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar));
313 SQChar c;
314 SQChar escch;
315 SQInteger escaped = 0;
316 for(int n = 0; n < size; n++){
317 c = *str++;
318 escch = 0;
319 if(scisprint(c) || c == 0) {
320 switch(c) {
321 case '\a': escch = 'a'; break;
322 case '\b': escch = 'b'; break;
323 case '\t': escch = 't'; break;
324 case '\n': escch = 'n'; break;
325 case '\v': escch = 'v'; break;
326 case '\f': escch = 'f'; break;
327 case '\r': escch = 'r'; break;
328 case '\\': escch = '\\'; break;
329 case '\"': escch = '\"'; break;
330 case '\'': escch = '\''; break;
331 case 0: escch = '0'; break;
332 }
333 if(escch) {
334 *dest++ = '\\';
335 *dest++ = escch;
336 escaped++;
337 }
338 else {
339 *dest++ = c;
340 }
341 }
342 else {
343
344 dest += scsprintf(dest, destcharsize, escpat, c);
345 escaped++;

Callers

nothing calls this directly

Calls 5

sq_getstringFunction · 0.85
sq_getsizeFunction · 0.85
sq_pushFunction · 0.85
sq_getscratchpadFunction · 0.85
sq_pushstringFunction · 0.85

Tested by

no test coverage detected