[gjs] console: Handle both (valid) Unicode strings and binary correctly
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] console: Handle both (valid) Unicode strings and binary correctly
- Date: Thu, 9 Jun 2011 21:06:25 +0000 (UTC)
commit 05bd1aee04f375a733d87ac165a23bdb68f8d5ad
Author: Colin Walters <walters verbum org>
Date: Thu Jun 9 14:40:46 2011 -0400
console: Handle both (valid) Unicode strings and binary correctly
When printing a value that is a string back to the terminal, check if
it's valid Unicode; if so, print it. Otherwise, we use escape
sequences for non-ASCII.
https://bugzilla.gnome.org/show_bug.cgi?id=643479
gjs/jsapi-util.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index cec3304..ac1237f 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -761,6 +761,51 @@ gjs_define_string_array(JSContext *context,
}
/**
+ * gjs_string_readable:
+ *
+ * Return a string that can be read back by gjs-console; for
+ * JS strings that contain valid Unicode, we return a UTF-8 formatted
+ * string. Otherwise, we return one where non-ASCII-printable bytes
+ * are \x escaped.
+ *
+ */
+static char *
+gjs_string_readable (JSContext *context,
+ JSString *string)
+{
+ GString *buf = g_string_new("");
+ char *chars;
+
+ JS_BeginRequest(context);
+
+ g_string_append_c(buf, '"');
+
+ if (!gjs_try_string_to_utf8(context, STRING_TO_JSVAL(string), &chars, NULL)) {
+ size_t i, len;
+ const jschar *uchars;
+
+ uchars = JS_GetStringCharsAndLength(context, string, &len);
+
+ for (i = 0; i < len; i++) {
+ jschar c = uchars[i];
+ if (c >> 8 == 0 && g_ascii_isprint(c & 0xFF))
+ g_string_append_c(buf, c & 0xFF);
+ else
+ g_string_append_printf(buf, "\\u%04X", c);
+ }
+ } else {
+ g_string_append(buf, chars);
+ g_free(chars);
+ }
+
+ g_string_append_c(buf, '"');
+
+ JS_EndRequest(context);
+
+ return g_string_free(buf, FALSE);
+}
+
+/**
* gjs_value_debug_string:
* @context:
* @value: Any JavaScript value
@@ -775,6 +820,11 @@ gjs_value_debug_string(JSContext *context,
char *bytes;
char *debugstr;
+ /* Special case debug strings for strings */
+ if (JSVAL_IS_STRING(value)) {
+ return gjs_string_readable(context, JSVAL_TO_STRING(value));
+ }
+
JS_BeginRequest(context);
str = JS_ValueToString(context, value);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]