-
Notifications
You must be signed in to change notification settings - Fork 7
/
unicode-character-class.c
82 lines (72 loc) · 1.75 KB
/
unicode-character-class.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "unicode.h"
#include "unicode-character-class.h"
/* Return a true or false value depending on whether ucs is in the
range of kana. */
int32_t ucs2_is_kana (int32_t ucs)
{
if (ucs >= 0x3000 && ucs <= 0x3100) {
return 1;
}
return 0;
}
/* Are all the characters in "utf8" kana characters? */
int32_t utf8_is_kana_chars (const uint8_t * utf8, int32_t len)
{
const uint8_t * p;
if (len == 0) {
len = strlen ((const char *) utf8);
}
for (p = utf8; p - utf8 < len; ) {
int ucs2;
ucs2 = utf8_to_ucs2 (p, & p);
if (ucs2 < 0) {
/* The input is incorrectly formatted in some way. */
return 0;
}
if (! ucs2_is_kana (ucs2)) {
return 0;
}
}
return 1;
}
int32_t utf16_is_kanji (int32_t utf16)
{
if (utf16 < 0x4e00 || utf16 > 0x9fff) {
return 0;
}
return 1;
}
#ifdef TEST
#define OK(test, counter, message, ...) { \
counter++; \
if (test) { \
printf ("ok %d - ", counter); \
} \
else { \
printf ("not ok %d - ", counter); \
} \
printf (message, ## __VA_ARGS__); \
printf (".\n"); \
}
int main ()
{
int counter = 0;
int i;
int expect[] = {1, 0, 0};
char * test[] = {"ひらがなカタカナスゲー",
"かな This is not all kana ",
"漢字"};
for (i = 0; i < sizeof (test) / sizeof (uint8_t *); i++) {
OK (utf8_is_kana_chars ((const uint8_t *) test[i], 0) == expect[i],
counter, "%s %d", test[i], expect[i]);
}
OK (utf16_is_kanji (0x6050), counter, "OK");
OK (! utf16_is_kanji (0x60), counter, "OK");
printf ("1..%d\n", counter);
return 0;
}
#endif /* TEST */