mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 01:41:37 +08:00
Quote strings properly in jsdump.
This commit is contained in:
23
jsdump.c
23
jsdump.c
@@ -3,6 +3,8 @@
|
||||
#include "jscompile.h"
|
||||
#include "jsvalue.h"
|
||||
|
||||
#include "utf.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#define nelem(a) (sizeof (a) / sizeof (a)[0])
|
||||
@@ -219,14 +221,29 @@ static void pobject(int d, js_Ast *list)
|
||||
|
||||
static void pstr(const char *s)
|
||||
{
|
||||
int c;
|
||||
static const char *HEX = "0123456789ABCDEF";
|
||||
Rune c;
|
||||
pc('"');
|
||||
while ((c = *s++)) {
|
||||
while (*s) {
|
||||
s += chartorune(&c, s);
|
||||
switch (c) {
|
||||
case '"': ps("\\\""); break;
|
||||
case '\\': ps("\\\\"); break;
|
||||
case '\b': ps("\\b"); break;
|
||||
case '\f': ps("\\f"); break;
|
||||
case '\n': ps("\\n"); break;
|
||||
default: pc(c); break;
|
||||
case '\r': ps("\\r"); break;
|
||||
case '\t': ps("\\t"); break;
|
||||
default:
|
||||
if (c < ' ' || c > 127) {
|
||||
ps("\\u");
|
||||
pc(HEX[(c>>12)&15]);
|
||||
pc(HEX[(c>>8)&15]);
|
||||
pc(HEX[(c>>4)&15]);
|
||||
pc(HEX[c&15]);
|
||||
} else {
|
||||
pc(c); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pc('"');
|
||||
|
||||
Reference in New Issue
Block a user