From b115b84dd140d17a36cfe6278e1537fd606b8704 Mon Sep 17 00:00:00 2001 From: albertodemichelis Date: Sun, 6 Mar 2016 22:00:17 +0800 Subject: [PATCH] escape() fix for different SQChar sizes --- sqstdlib/sqstdstring.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/sqstdlib/sqstdstring.cpp b/sqstdlib/sqstdstring.cpp index 615ce75..1ce1149 100644 --- a/sqstdlib/sqstdstring.cpp +++ b/sqstdlib/sqstdstring.cpp @@ -257,7 +257,19 @@ static SQInteger _string_escape(HSQUIRRELVM v) sq_push(v,2); return 1; } - SQInteger destcharsize = (size * 6); //assumes every char could be escaped +#ifdef SQUNICODE +#if WCHAR_SIZE == 2 + const SQChar *escpat = _SC("\\x%04x"); + const SQInteger maxescsize = 6; +#else //WCHAR_SIZE == 4 + const SQChar *escpat = _SC("\\x%08x"); + const SQInteger maxescsize = 10; +#endif +#else + const SQChar *escpat = _SC("\\x%02x"); + const SQInteger maxescsize = 4; +#endif + SQInteger destcharsize = (size * maxescsize); //assumes every char could be escaped resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar)); SQChar c; SQChar escch; @@ -289,7 +301,8 @@ static SQInteger _string_escape(HSQUIRRELVM v) } } else { - dest += scsprintf(dest,destcharsize,_SC("\\x%x"),c); + + dest += scsprintf(dest, destcharsize, escpat, c); escaped++; } }