Add g_markup_printf_escaped(), g_markup_vprintf_escaped().

Thu Sep 11 20:11:05 2003  Owen Taylor  <otaylor@redhat.com>

        * glib/gmarkup.c: Add g_markup_printf_escaped(),
        g_markup_vprintf_escaped().

        * tests/markup-escape-test.c (main): Test for
        g_markup_escape_text(), g_markup_printf_escaped().
This commit is contained in:
Owen Taylor 2003-09-12 00:17:02 +00:00 committed by Owen Taylor
parent 8d91ba8c58
commit 2f4c3d8a6d
17 changed files with 494 additions and 3 deletions

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -1,3 +1,11 @@
Thu Sep 11 20:11:05 2003 Owen Taylor <otaylor@redhat.com>
* glib/gmarkup.c: Add g_markup_printf_escaped(),
g_markup_vprintf_escaped().
* tests/markup-escape-test.c (main): Test for
g_markup_escape_text(), g_markup_printf_escaped().
2003-09-10 Noah Levitt <nlevitt@columbia.edu>
* glib/gunicodeprivate.h:

View File

@ -32,7 +32,7 @@ IGNORE_HFILES= \
glibconfig-sysdefs.h \
gdebug.h \
gprintfint.h \
trio
gnulib
# Extra options to supply to gtkdoc-mkdb
MKDB_OPTIONS=--sgml-mode --output-format=xml --ignore-files=trio

View File

@ -843,6 +843,8 @@ GMarkupParseFlags
GMarkupParseContext
GMarkupParser
g_markup_escape_text
g_markup_printf_escaped
g_markup_vprintf_escaped
g_markup_parse_context_end_parse
g_markup_parse_context_free
g_markup_parse_context_get_position

View File

@ -169,6 +169,16 @@ A test to perform an a file using g_file_test().
@Returns:
<!-- ##### FUNCTION g_file_read_link ##### -->
<para>
</para>
@filename:
@error:
@Returns:
<!-- ##### STRUCT GDir ##### -->
<para>
An opaque structure representing an opened directory.

View File

@ -346,6 +346,7 @@ specifiers for values of type #gint32. See also #G_GINT16_MODIFIER.
@Since: 2.4
<!-- ##### MACRO G_GINT32_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and
@ -378,6 +379,7 @@ is not defined.
@Since: 2.4
<!-- ##### MACRO G_GINT64_FORMAT ##### -->
<para>
This is the platform dependent conversion specifier for scanning and

View File

@ -153,6 +153,26 @@ passthrough text back out in the same position
@Returns:
<!-- ##### FUNCTION g_markup_printf_escaped ##### -->
<para>
</para>
@format:
@Varargs:
@Returns:
<!-- ##### FUNCTION g_markup_vprintf_escaped ##### -->
<para>
</para>
@format:
@args:
@Returns:
<!-- ##### FUNCTION g_markup_parse_context_end_parse ##### -->
<para>

View File

@ -57,7 +57,7 @@ reasons this function can only be called once.
</para>
@variable:
@variable:
@Returns:

View File

@ -304,6 +304,8 @@ See <ulink url="http://www.unicode.org/unicode/reports/tr14/"
@G_UNICODE_BREAK_COMPLEX_CONTEXT:
@G_UNICODE_BREAK_AMBIGUOUS:
@G_UNICODE_BREAK_UNKNOWN:
@G_UNICODE_BREAK_NEXT_LINE:
@G_UNICODE_BREAK_WORD_JOINER:
<!-- ##### FUNCTION g_unichar_break_type ##### -->
<para>

View File

@ -1,6 +1,6 @@
/* gmarkup.c - Simple XML-like parser
*
* Copyright 2000 Red Hat, Inc.
* Copyright 2000, 2003 Red Hat, Inc.
*
* GLib is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
@ -20,6 +20,7 @@
#include "config.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@ -1826,3 +1827,305 @@ g_markup_escape_text (const gchar *text,
return g_string_free (str, FALSE);
}
/**
* find_conversion:
* @format: a printf-style format string
* @after: location to store a pointer to the character after
* the returned conversion. On a %NULL return, returns the
* pointer to the trailing NUL in the string
*
* Find the next conversion in a printf-style format string.
* Partially based on code from printf-parser.c,
* Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
*
* Return value: pointer to the next conversion in @format,
* or %NULL, if none.
**/
static const char *
find_conversion (const char *format,
const char **after)
{
const char *start = format;
const char *cp;
while (*start != '\0' && *start != '%')
start++;
if (*start == '\0')
{
*after = start;
return NULL;
}
cp = start + 1;
if (*cp == '\0')
{
*after = cp;
return NULL;
}
/* Test for positional argument. */
if (*cp >= '0' && *cp <= '9')
{
const char *np;
for (np = cp; *np >= '0' && *np <= '9'; np++)
;
if (*np == '$')
cp = np + 1;
}
/* Skip the flags. */
for (;;)
{
if (*cp == '\'' ||
*cp == '-' ||
*cp == '+' ||
*cp == ' ' ||
*cp == '#' ||
*cp == '0')
cp++;
else
break;
}
/* Skip the field width. */
if (*cp == '*')
{
cp++;
/* Test for positional argument. */
if (*cp >= '0' && *cp <= '9')
{
const char *np;
for (np = cp; *np >= '0' && *np <= '9'; np++)
;
if (*np == '$')
cp = np + 1;
}
}
else
{
for (; *cp >= '0' && *cp <= '9'; cp++)
;
}
/* Skip the precision. */
if (*cp == '.')
{
cp++;
if (*cp == '*')
{
/* Test for positional argument. */
if (*cp >= '0' && *cp <= '9')
{
const char *np;
for (np = cp; *np >= '0' && *np <= '9'; np++)
;
if (*np == '$')
cp = np + 1;
}
}
else
{
for (; *cp >= '0' && *cp <= '9'; cp++)
;
}
}
/* Skip argument type/size specifiers. */
while (*cp == 'h' ||
*cp == 'L' ||
*cp == 'l' ||
*cp == 'j' ||
*cp == 'z' ||
*cp == 'Z' ||
*cp == 't')
cp++;
/* Skip the conversion character. */
cp++;
*after = cp;
return start;
}
/**
* g_markup_vprintf_escaped:
* @format: printf() style format string
* @args: variable argument list, similar to vprintf()
*
* Formats the data in @args according to @format, escaping
* all string and character arguments in the fashion
* of g_markup_escape_text(). See g_markup_printf_escaped().
*
* Return value: newly allocated result from formatting
* operation. Free with g_free().
**/
char *
g_markup_vprintf_escaped (const char *format,
va_list args)
{
GString *format1;
GString *format2;
GString *result = NULL;
gchar *output1 = NULL;
gchar *output2 = NULL;
const char *p, *op1, *op2;
va_list args2;
/* The technique here, is that we make two format strings that
* have the identical conversions in the identical order to the
* original strings, but differ in the text in-between. We
* then use the normal g_strdup_vprintf() to format the arguments
* with the two new format strings. By comparing the results,
* we can figure out what segments of the output come from
* the the original format string, and what from the arguments,
* and thus know what portions of the string to escape.
*
* For instance, for:
*
* g_markup_printf_escaped ("%s ate %d apples", "Susan & Fred", 5);
*
* We form the two format strings "%sX%dX" and %sY%sY". The results
* of formatting with those two strings are
*
* "%sX%dX" => "Susan & FredX5X"
* "%sY%dY" => "Susan & FredY5Y"
*
* To find the span of the first argument, we find the first position
* where the two arguments differ, which tells us that the first
* argument formatted to "Susan & Fred". We then escape that
* to "Susan &amp; Fred" and join up with the intermediate portions
* of the format string and the second argument to get
* "Susan &amp; Fred ate 5 apples".
*/
/* Create the two modified format strings
*/
format1 = g_string_new (NULL);
format2 = g_string_new (NULL);
p = format;
while (TRUE)
{
const char *after;
const char *conv = find_conversion (p, &after);
if (!conv)
break;
g_string_append_len (format1, conv, after - conv);
g_string_append_c (format1, 'X');
g_string_append_len (format2, conv, after - conv);
g_string_append_c (format2, 'Y');
p = after;
}
/* Use them to format the arguments
*/
G_VA_COPY (args2, args);
output1 = g_strdup_vprintf (format1->str, args);
va_end (args);
if (!output1)
goto cleanup;
output2 = g_strdup_vprintf (format2->str, args2);
va_end (args2);
if (!output2)
goto cleanup;
result = g_string_new (NULL);
/* Iterate through the original format string again,
* copying the non-conversion portions and the escaped
* converted arguments to the output string.
*/
op1 = output1;
op2 = output2;
p = format;
while (TRUE)
{
const char *after;
const char *output_start;
const char *conv = find_conversion (p, &after);
char *escaped;
if (!conv) /* The end, after points to the trailing \0 */
{
g_string_append_len (result, p, after - p);
break;
}
g_string_append_len (result, p, conv - p);
output_start = op1;
while (*op1 == *op2)
{
op1++;
op2++;
}
escaped = g_markup_escape_text (output_start, op1 - output_start);
g_string_append (result, escaped);
g_free (escaped);
p = after;
op1++;
op2++;
}
cleanup:
g_string_free (format1, TRUE);
g_string_free (format2, TRUE);
g_free (output1);
g_free (output2);
if (result)
return g_string_free (result, FALSE);
else
return NULL;
}
/**
* g_markup_printf_escaped:
* @format: printf() style format string
* @Varargs: the arguments to insert in the format string
*
* Formats arguments according to @format, escaping
* all string and character arguments in the fashion
* of g_markup_escape_text(). This is useful when you
* want to insert literal strings into XML-style markup
* output, without having to worry that the strings
* might themselves contain markup.
*
* <informalexample><programlisting>
* const char *store = "Fortnum & Mason";
* const char *item = "Tea";
* char *output;
* &nbsp;
* output = g_markup_printf_escaped ("&lt;purchase&gt;"
* "&lt;store&gt;&percnt;s&lt;/store&gt;"
* "&lt;item&gt;&percnt;s&lt;/item&gt;"
* "&lt;/purchase&gt;",
* store, item);
* </programlisting></informalexample>
*
* Return value: newly allocated result from formatting
* operation. Free with g_free().
**/
char *
g_markup_printf_escaped (const char *format, ...)
{
char *result;
va_list args;
va_start (args, format);
result = g_markup_vprintf_escaped (format, args);
va_end (args);
return result;
}

View File

@ -21,6 +21,8 @@
#ifndef __G_MARKUP_H__
#define __G_MARKUP_H__
#include <stdarg.h>
#include <glib/gerror.h>
G_BEGIN_DECLS
@ -118,6 +120,11 @@ void g_markup_parse_context_get_position (GMarkupParseContext *c
gchar* g_markup_escape_text (const gchar *text,
gssize length);
gchar *g_markup_printf_escaped (const char *format,
...) G_GNUC_PRINTF (1, 2);
gchar *g_markup_vprintf_escaped (const char *format,
va_list args);
G_END_DECLS
#endif /* __G_MARKUP_H__ */

View File

@ -73,6 +73,7 @@ test_programs = \
iochannel-test \
list-test \
mainloop-test \
markup-escape-test \
module-test \
node-test \
patterntest \
@ -121,6 +122,7 @@ iochannel_test_LDADD = $(progs_ldadd)
list_test_LDADD = $(progs_ldadd)
mainloop_test_LDADD = $(thread_ldadd)
markup_test_LDADD = $(progs_ldadd)
markup_escape_test_LDADD = $(progs_ldadd)
module_test_LDADD = $(module_ldadd) $(module_test_exp)
module_test_LDFLAGS = $(G_MODULE_LDFLAGS)
node_test_LDADD = $(progs_ldadd)

View File

@ -0,0 +1,95 @@
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <stdarg.h>
#include <string.h>
#include <glib.h>
static void test_format (const gchar *format,
const gchar *expected, ...) G_GNUC_PRINTF (1, 3);
static gboolean error = FALSE;
static void
test (const gchar *original,
const gchar *expected)
{
gchar *result = g_markup_escape_text (original, -1);
if (strcmp (result, expected) != 0)
{
g_printerr ("g_markup_escape_text(): expected '%s', got '%s'\n",
expected, result);
error = TRUE;
}
g_free (result);
}
static void
test_format (const gchar *format,
const gchar *expected,
...)
{
gchar *result;
va_list args;
va_start (args, expected);
result = g_markup_vprintf_escaped (format, args);
va_end (args);
if (strcmp (result, expected) != 0)
{
g_printerr ("g_markup_printf_escaped(): expected '%s', got '%s'\n",
expected, result);
error = TRUE;
}
g_free (result);
}
int main (int argc, char **argv)
{
/* Tests for g_markup_escape_text() */
test ("&", "&amp;");
test ("<", "&lt;");
test (">", "&gt;");
test ("'", "&apos;");
test ("\"", "&quot;");
test ("", "");
test ("A", "A");
test ("A&", "A&amp;");
test ("&A", "&amp;A");
test ("A&A", "A&amp;A");
test ("&&A", "&amp;&amp;A");
test ("A&&", "A&amp;&amp;");
test ("A&&A", "A&amp;&amp;A");
test ("A&A&A", "A&amp;A&amp;A");
/* Tests for g_markup_printf_escaped() */
test_format ("A", "A");
test_format ("A%s", "A&amp;", "&");
test_format ("%sA", "&amp;A", "&");
test_format ("A%sA", "A&amp;A", "&");
test_format ("%s%sA", "&amp;&amp;A", "&", "&");
test_format ("A%s%s", "A&amp;&amp;", "&", "&");
test_format ("A%s%sA", "A&amp;&amp;A", "&", "&");
test_format ("A%sA%sA", "A&amp;A&amp;A", "&", "&");
test_format ("%s", "&lt;B&gt;&amp;",
"<B>&");
test_format ("%c%c", "&lt;&amp;",
'<', '&');
test_format (".%c.%c.", ".&lt;.&amp;.",
'<', '&');
test_format ("%s", "",
"");
test_format ("%-5s", "A ",
"A");
test_format ("%2$s%1$s", "B.A.",
"A.", "B.");
return error ? 1 : 0;
}