add UTF-8 support.

* glib/gpattern.c: add UTF-8 support.

        * tests/patterntest.c: add UTF-8 and equality tests.

        * docs/reference/glib/Makefile.am (MKDB_OPTIONS): Add --sgml-mode.

        * docs/reference/glib/tmpl/patterns.sgml: Document UTF-8 support.
This commit is contained in:
Matthias Clasen
2001-11-14 22:22:34 +00:00
parent 2180c1ed05
commit a49a78a3b3
13 changed files with 206 additions and 121 deletions

View File

@@ -16,10 +16,9 @@
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "glib.h"
#include "glib/gpattern.h"
/* keep enum and structure of gpattern.c and patterntest.c in sync */
typedef enum
@@ -31,10 +30,12 @@ typedef enum
G_MATCH_EXACT, /* "AAAAA" */
G_MATCH_LAST
} GMatchType;
struct _GPatternSpec
{
GMatchType match_type;
guint pattern_length;
guint min_length;
gchar *pattern;
};
@@ -65,16 +66,22 @@ match_type_name (GMatchType match_type)
}
}
/* this leakes memory, but we don't care */
#define utf8(str) g_convert (str, -1, "Latin1", "UTF-8", NULL, NULL, NULL)
#define latin1(str) g_convert (str, -1, "UTF-8", "Latin1", NULL, NULL, NULL)
static gboolean
test_compilation (gchar *src,
GMatchType match_type,
gchar *pattern)
gchar *pattern,
guint min)
{
GPatternSpec *spec;
g_print ("compiling \"%s\" \t", src);
g_print ("compiling \"%s\" \t", utf8(src));
spec = g_pattern_spec_new (src);
if (spec->match_type != match_type)
{
g_print ("failed \t(match_type: %s, expected %s)\n",
@@ -86,8 +93,8 @@ test_compilation (gchar *src,
if (strcmp (spec->pattern, pattern) != 0)
{
g_print ("failed \t(pattern: \"%s\", expected \"%s\")\n",
spec->pattern,
pattern);
utf8(spec->pattern),
utf8(pattern));
return FALSE;
}
@@ -99,6 +106,14 @@ test_compilation (gchar *src,
return FALSE;
}
if (spec->min_length != min)
{
g_print ("failed \t(min_length: %d, expected %d)\n",
spec->min_length,
min);
return FALSE;
}
g_print ("passed (%s: \"%s\")\n",
match_type_name (spec->match_type),
spec->pattern);
@@ -111,7 +126,7 @@ test_match (gchar *pattern,
gchar *string,
gboolean match)
{
g_print ("matching \"%s\" against \"%s\" \t", string, pattern);
g_print ("matching \"%s\" against \"%s\" \t", utf8(string), utf8(pattern));
if (g_pattern_match_simple (pattern, string) != match)
{
@@ -132,14 +147,14 @@ test_equal (gchar *pattern1,
GPatternSpec *p2 = g_pattern_spec_new (pattern2);
gboolean equal = g_pattern_spec_equal (p1, p2);
g_print ("comparing \"%s\" with \"%s\" \t", pattern1, pattern2);
g_print ("comparing \"%s\" with \"%s\" \t", utf8(pattern1), utf8(pattern2));
if (expected != equal)
{
g_print ("failed \t{%s, %u, \"%s\"} %s {%s, %u, \"%s\"}\n",
match_type_name (p1->match_type), p1->pattern_length, p1->pattern,
match_type_name (p1->match_type), p1->pattern_length, utf8(p1->pattern),
expected ? "!=" : "==",
match_type_name (p2->match_type), p2->pattern_length, p2->pattern);
match_type_name (p2->match_type), p2->pattern_length, utf8(p2->pattern));
}
else
g_print ("passed (%s)\n", equal ? "equal" : "unequal");
@@ -150,9 +165,9 @@ test_equal (gchar *pattern1,
return expected == equal;
}
#define TEST_COMPILATION(src, type, pattern) { \
#define TEST_COMPILATION(src, type, pattern, min) { \
total++; \
if (test_compilation (src, type, pattern)) \
if (test_compilation (latin1(src), type, latin1(pattern), min)) \
passed++; \
else \
failed++; \
@@ -160,7 +175,7 @@ test_equal (gchar *pattern1,
#define TEST_MATCH(pattern, string, match) { \
total++; \
if (test_match (pattern, string, match)) \
if (test_match (latin1(pattern), latin1(string), match)) \
passed++; \
else \
failed++; \
@@ -168,7 +183,7 @@ test_equal (gchar *pattern1,
#define TEST_EQUAL(pattern1, pattern2, match) { \
total++; \
if (test_equal (pattern1, pattern2, match)) \
if (test_equal (latin1(pattern1), latin1(pattern2), match)) \
passed++; \
else \
failed++; \
@@ -180,16 +195,21 @@ main (int argc, char** argv)
gint total = 0;
gint passed = 0;
gint failed = 0;
gchar *string;
gchar *string, *pattern;
TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*");
TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA");
TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH");
TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*?*??*DEF*GH");
TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*");
TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD");
TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "DCBA");
TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE");
TEST_COMPILATION("*A?B*", G_MATCH_ALL, "*A?B*", 3);
TEST_COMPILATION("ABC*DEFGH", G_MATCH_ALL_TAIL, "HGFED*CBA", 8);
TEST_COMPILATION("ABCDEF*GH", G_MATCH_ALL, "ABCDEF*GH", 8);
TEST_COMPILATION("ABC**?***??**DEF*GH", G_MATCH_ALL, "ABC*???DEF*GH", 11);
TEST_COMPILATION("*A?AA", G_MATCH_ALL_TAIL, "AA?A*", 4);
TEST_COMPILATION("ABCD*", G_MATCH_HEAD, "ABCD", 4);
TEST_COMPILATION("*ABCD", G_MATCH_TAIL, "ABCD", 4);
TEST_COMPILATION("ABCDE", G_MATCH_EXACT, "ABCDE", 5);
TEST_COMPILATION("A?C?E", G_MATCH_ALL, "A?C?E", 5);
TEST_COMPILATION("*?x", G_MATCH_ALL_TAIL, "x?*", 2);
TEST_COMPILATION("?*x", G_MATCH_ALL_TAIL, "x?*", 2);
TEST_COMPILATION("*?*x", G_MATCH_ALL_TAIL, "x?*", 2);
TEST_COMPILATION("x*??", G_MATCH_ALL_TAIL, "??*x", 3);
TEST_EQUAL ("*A?B*", "*A?B*", TRUE);
TEST_EQUAL ("A*BCD", "A*BCD", TRUE);
@@ -198,6 +218,8 @@ main (int argc, char** argv)
TEST_EQUAL ("*YZ", "*YZ", TRUE);
TEST_EQUAL ("A1x", "A1x", TRUE);
TEST_EQUAL ("AB*CD", "AB**CD", TRUE);
TEST_EQUAL ("AB*?*CD", "AB*?CD", TRUE);
TEST_EQUAL ("AB*?CD", "AB?*CD", TRUE);
TEST_EQUAL ("AB*CD", "AB*?*CD", FALSE);
TEST_EQUAL ("ABC*", "ABC?", FALSE);
@@ -215,11 +237,20 @@ main (int argc, char** argv)
TEST_MATCH("?*x", "x", FALSE);
TEST_MATCH("*?*x", "yx", TRUE);
TEST_MATCH("*?*x", "xxxx", TRUE);
string = g_convert ("<EFBFBD>x", -1, "UTF-8", "Latin1", NULL, NULL, NULL);
TEST_MATCH("*x", string, TRUE);
TEST_MATCH("?x", string, TRUE);
TEST_MATCH("??x", string, FALSE);
TEST_MATCH("x*??", "xyzw", TRUE);
TEST_MATCH("*x", "<EFBFBD>x", TRUE);
TEST_MATCH("?x", "<EFBFBD>x", TRUE);
TEST_MATCH("??x", "<EFBFBD>x", FALSE);
TEST_MATCH("ab<EFBFBD><EFBFBD>", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab<EFBFBD><EFBFBD>", "abao", FALSE);
TEST_MATCH("ab?<3F>", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab?<3F>", "abao", FALSE);
TEST_MATCH("ab<EFBFBD>?", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab<EFBFBD>?", "abao", FALSE);
TEST_MATCH("ab??", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab*", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab*<2A>", "ab<EFBFBD><EFBFBD>", TRUE);
TEST_MATCH("ab*<2A>", "aba<EFBFBD>x<EFBFBD>", TRUE);
g_print ("\n%u tests passed, %u failed\n", passed, failed);