pike.git/
src/
stralloc.c
Branch:
Tag:
Non-build tags
All tags
No tags
2006-03-20
2006-03-20 18:25:04 by Henrik Grubbström (Grubba) <grubba@grubba.org>
64b07775fcfe80f3c8af8cf36f41af4dc935bfd8 (
66
lines) (+
65
/-
1
)
[
Show
|
Annotate
]
Branch:
7.9
Added string_builder_quote_string().
Rev: src/stralloc.c:1.206
Rev: src/stralloc.h:1.97
2:
|| This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information.
-
|| $Id: stralloc.c,v 1.
205
2006/03/
11
12
:
30
:
40
grubba Exp $
+
|| $Id: stralloc.c,v 1.
206
2006/03/
20
18
:
25
:
03
grubba Exp $
*/ #include "global.h"
2444:
s->s->str[s->s->len << s->s->size_shift] = 0; }
+
PMOD_EXPORT void string_builder_quote_string(struct string_builder *buf,
+
struct pike_string *str,
+
int flags, ptrdiff_t max_len)
+
{
+
ptrdiff_t old_len = buf->s->len;
+
ptrdiff_t i;
+
+
for (i = 0; i < str->len; i++) {
+
p_wchar2 ch = index_shared_string(str, i);
+
if (ch > 0xffff) {
+
/* Huge character. */
+
string_builder_binary_strcat(buf, "\\U", 2);
+
string_builder_append_integer(buf, ch, 16, APPEND_ZERO_PAD, 8, 8);
+
} else if (ch > 0xff) {
+
/* Unicode character. */
+
string_builder_binary_strcat(buf, "\\u", 2);
+
string_builder_append_integer(buf, ch, 16, APPEND_ZERO_PAD, 4, 4);
+
} else if (ch & 0x60) {
+
/* Printable character or DEL. */
+
if (ch == '\177') {
+
/* DEL */
+
string_builder_binary_strcat(buf, "\\177", 4);
+
continue;
+
} else if ((ch == '"') || (ch == '\\')) {
+
string_builder_putchar(buf, '\\');
+
}
+
string_builder_putchar(buf, ch);
+
} else {
+
p_wchar2 next_ch;
+
/* Control character. */
+
string_builder_putchar(buf, '\\');
+
if ((ch > 6) && (ch < 14)) {
+
string_builder_putchar(buf, "0123456abtnvfr"[ch]);
+
continue;
+
}
+
if (ch == 27) {
+
string_builder_putchar(buf, 'e');
+
continue;
+
}
+
/* Check if we can use an octal escape. */
+
if ((i+1 < str->len) &&
+
((next_ch = index_shared_string(str, i+1)) >= '0') &&
+
(next_ch <= '7')) {
+
/* No. */
+
if (flags & QUOTE_NO_STRING_CONCAT) {
+
string_builder_putchar(buf, 'u');
+
string_builder_append_integer(buf, 16, ch, APPEND_ZERO_PAD, 4, 4);
+
} else {
+
string_builder_append_integer(buf, ch, 8, 0, 1, 1);
+
string_builder_binary_strcat(buf, "\"\"", 2);
+
}
+
continue;
+
}
+
string_builder_append_integer(buf, ch, 8, 0, 1, 1);
+
}
+
if (buf->s->len > max_len) {
+
buf->s->len = old_len;
+
break;
+
}
+
old_len = buf->s->len;
+
}
+
}
+
PMOD_EXPORT void string_builder_append_integer(struct string_builder *s, LONGEST val, unsigned int base,