forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
133 lines (114 loc) · 3.75 KB
/
utf8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*****************************************************************************
* utf8.c: Test for UTF-8 encoding/decoding stuff
*****************************************************************************
* Copyright (C) 2006 Rémi Denis-Courmont
* $Id$
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_charset.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static void test (const char *in, const char *out)
{
bool isutf8 = !strcmp (in, out);
char *str = strdup (in);
if (str == NULL)
abort ();
if (isutf8)
printf ("\"%s\" should be accepted...\n", in);
else
printf ("\"%s\" should be rewritten as \"%s\"...\n", in, out);
if ((IsUTF8 (in) != NULL) != isutf8)
{
printf (" ERROR: IsUTF8 (%s) failed\n", in);
exit (1);
}
if ((EnsureUTF8 (str) != NULL) != isutf8)
{
printf (" ERROR: EnsureUTF8 (%s) failed\n", in);
exit (2);
}
if (strcmp (str, out))
{
printf (" ERROR: got \"%s\"\n", str);
exit (3);
}
if ((EnsureUTF8 (str) == NULL) || IsUTF8 (str) == NULL)
{
printf (" ERROR: EnsureUTF8 (%s) is not UTF-8\n", in);
exit (4);
}
free (str);
}
static void test_strcasestr (const char *h, const char *n, ssize_t offset)
{
printf ("\"%s\" should %sbe found in \"%s\"...\n", n,
(offset != -1) ? "" : "not ", h);
const char *ret = vlc_strcasestr (h, n);
if (offset == -1)
{
if (ret != NULL)
{
printf ("ERROR: got \"%s\"\n", ret);
exit (10);
}
}
else
{
if (ret == NULL)
{
printf ("ERROR: not found\n");
exit (11);
}
if ((ret - h) != offset)
{
printf ("ERROR: got \"%s\" instead of \"%s\"\n",
ret, h + offset);
exit (12);
}
}
}
int main (void)
{
(void)setvbuf (stdout, NULL, _IONBF, 0);
test ("", "");
test ("this_should_not_be_modified_1234",
"this_should_not_be_modified_1234");
test ("\xFF", "?"); // invalid byte
test ("\xEF\xBB\xBFHello", "\xEF\xBB\xBFHello"); // BOM
test ("\x00\xE9", ""); // no conversion past end of string
test ("T\xC3\xA9l\xC3\xA9vision \xE2\x82\xAC", "Télévision €");
test ("T\xE9l\xE9vision", "T?l?vision");
test ("\xC1\x94\xC3\xa9l\xC3\xA9vision", "??élévision"); /* overlong */
test ("Hel\xF0\x83\x85\x87lo", "Hel????lo"); /* more overlong */
test_strcasestr ("", "", 0);
test_strcasestr ("", "a", -1);
test_strcasestr ("a", "", 0);
test_strcasestr ("heLLo", "l", 2);
test_strcasestr ("heLLo", "lo", 3);
test_strcasestr ("heLLo", "llo", 2);
test_strcasestr ("heLLo", "la", -1);
test_strcasestr ("heLLo", "oa", -1);
test_strcasestr ("Télé", "é", 1);
test_strcasestr ("Télé", "élé", 1);
test_strcasestr ("Télé", "léé", -1);
return 0;
}