css: Split out a common function

It's used in two places already, and Iwant to use it in a third one.
This commit is contained in:
Benjamin Otte 2014-05-01 19:47:58 +02:00
parent d495ab1ca0
commit b356d81410
4 changed files with 52 additions and 67 deletions

View File

@ -1012,3 +1012,47 @@ _gtk_css_parser_resync (GtkCssParser *parser,
gtk_css_parser_resync_internal (parser, sync_at_semicolon, TRUE, terminator);
}
void
_gtk_css_print_string (GString *str,
const char *string)
{
gsize len;
g_return_if_fail (str != NULL);
g_return_if_fail (string != NULL);
g_string_append_c (str, '"');
do {
len = strcspn (string, "\"\n\r\f");
g_string_append_len (str, string, len);
string += len;
switch (*string)
{
case '\0':
break;
case '\n':
g_string_append (str, "\\A ");
break;
case '\r':
g_string_append (str, "\\D ");
break;
case '\f':
g_string_append (str, "\\C ");
break;
case '\"':
g_string_append (str, "\\\"");
break;
case '\\':
g_string_append (str, "\\\\");
break;
default:
g_assert_not_reached ();
break;
}
} while (*string);
g_string_append_c (str, '"');
}

View File

@ -97,6 +97,11 @@ void _gtk_css_parser_resync (GtkCssParser *parser
gboolean sync_at_semicolon,
char terminator);
/* XXX: Find better place to put it? */
void _gtk_css_print_string (GString *str,
const char *string);
G_END_DECLS
#endif /* __GTK_CSS_PARSER_PRIVATE_H__ */

View File

@ -65,47 +65,13 @@ static void
gtk_css_value_string_print (const GtkCssValue *value,
GString *str)
{
char *string = value->string;
gsize len;
if (string == NULL)
if (value->string == NULL)
{
g_string_append (str, "none");
return;
}
g_string_append_c (str, '"');
do {
len = strcspn (string, "\"\n\r\f");
g_string_append_len (str, string, len);
string += len;
switch (*string)
{
case '\0':
break;
case '\n':
g_string_append (str, "\\A ");
break;
case '\r':
g_string_append (str, "\\D ");
break;
case '\f':
g_string_append (str, "\\C ");
break;
case '\"':
g_string_append (str, "\\\"");
break;
case '\\':
g_string_append (str, "\\\\");
break;
default:
g_assert_not_reached ();
break;
}
} while (*string);
g_string_append_c (str, '"');
_gtk_css_print_string (str, value->string);
}
static void

View File

@ -89,37 +89,7 @@ static void
string_append_string (GString *str,
const char *string)
{
gsize len;
g_string_append_c (str, '"');
do {
len = strcspn (string, "\"\n\r\f");
g_string_append (str, string);
string += len;
switch (*string)
{
case '\0':
break;
case '\n':
g_string_append (str, "\\A ");
break;
case '\r':
g_string_append (str, "\\D ");
break;
case '\f':
g_string_append (str, "\\C ");
break;
case '\"':
g_string_append (str, "\\\"");
break;
default:
g_assert_not_reached ();
break;
}
} while (*string);
g_string_append_c (str, '"');
_gtk_css_print_string (str, string);
}
/*** IMPLEMENTATIONS ***/