1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/* Type for adding whitespace. */
typedef struct json_s {
SV * sv;
char * s;
STRLEN sl;
}
json_s_t;
typedef struct json_ws {
SV * news;
SV * olds;
/* Length of original string. */
STRLEN olds_l;
/* Length of new string. */
unsigned int news_l;
/* Copy point. */
char * q;
/* Origin */
char * p;
/* Top of token tree. */
json_token_t * t;
/* Token under examination now. */
json_token_t * next;
/* Whitespace to add before and after. */
char * before[n_json_tokens];
char * after[n_json_tokens];
int array_depth;
int object_depth;
char * array_indent;
char * object_indent;
}
json_ws_t;
static void copy_whitespace (json_ws_t * ws, char * w)
{
char * q;
q = ws->q;
while (*w) {
*q++ = *w++;
}
ws->q = q;
}
static INLINE int whitespace_json (json_ws_t * ws)
{
/* Copy place. */
char * c;
/* Value of q at entry to this routine, used to calculate added
length. */
char * qorig;
json_token_t * next;
char * q;
q = ws->q;
qorig = q;
next = ws->next;
while (next) {
/* Copy start of string. */
copy_whitespace (ws, ws->before[next->type]);
switch (next->type) {
case json_token_object:
*q++ = '{';
ws->object_depth++;
ws->q = q;
q += whitespace_json (ws);
ws->object_depth--;
*q++ = '}';
break;
case json_token_array:
*q++ = '[';
ws->array_depth++;
ws->q = q;
q += whitespace_json (ws);
ws->object_depth--;
*q++ = ']';
break;
case json_token_string:
case json_token_key:
case json_token_literal:
case json_token_number:
for (c = ws->p + next->start; c <= ws->p + next->end; c++) {
*q++ = *c;
}
break;
case json_token_comma:
*q++ = ',';
break;
case json_token_colon:
*q++ = ':';
break;
default:
croak ("unhandled token type %d", next->type);
}
/* Copy end of string. */
c = ws->after[next->type];
while (*c) {
*q++ = *c++;
}
next = next->next;
}
return q - qorig;
}
static int copy_json (char * p, char * q, json_token_t * t)
{
/* Loop variable. */
json_token_t * next;
/* Copy place. */
char * c;
/* Value of q at entry to this routine, used to calculate added
length. */
char * qorig;
next = t;
qorig = q;
while (next) {
switch (next->type) {
case json_token_object:
*q++ = '{';
q += copy_json (p, q, next->child);
*q++ = '}';
break;
case json_token_array:
*q++ = '[';
q += copy_json (p, q, next->child);
*q++ = ']';
break;
case json_token_string:
case json_token_key:
case json_token_literal:
case json_token_number:
for (c = p + next->start; c < p + next->end; c++) {
*q++ = *c;
}
break;
case json_token_comma:
*q++ = ',';
break;
case json_token_colon:
*q++ = ':';
break;
default:
croak ("unhandled token type %d", next->type);
}
next = next->next;
}
return q - qorig;
}
/* Remove all the whitespace. */
static SV * strip_whitespace (json_token_t * tokens, SV * json)
{
SV * stripped;
char * p;
char * q;
/* Original length. */
STRLEN l;
/* Length of output. */
unsigned int m;
p = SvPV (json, l);
stripped = newSV (l);
/* Tell Perl it's a string. */
SvPOK_on (stripped);
/* Set UTF-8 if necessary. */
if (SvUTF8 (json)) {
SvUTF8_on (stripped);
}
/* Get a pointer to the string inside "stripped". */
q = SvPVX (stripped);
m = copy_json (p, q, tokens);
/* Set the length. */
SvCUR_set (stripped, m);
return stripped;
}
static SV * indent (json_token_t * tokens, SV * json)
{
int i;
json_ws_t j = {0};
j.olds = json;
j.p = SvPV (j.olds, j.olds_l);
j.t = tokens;
j.next = tokens;
for (i = 0; i < n_json_tokens; i++) {
j.before[i] = "";
j.after[i] = "";
}
j.after[json_token_comma] = "\n";
j.after[json_token_object] = "\n";
j.after[json_token_array] = "\n";
return &PL_sv_undef;
}
|