summaryrefslogtreecommitdiff
path: root/src/unicode.c
blob: 7ddc0d5120a46c5fd9ab05e5cab116181a02073b (plain)
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
#include <stdbool.h>
#include <string.h>

#include "unicode.h"

uint8_t utf32_to_utf8(uint32_t c, char *buf)
{
	return g_unichar_to_utf8(c, buf);
}

uint32_t utf8_to_utf32(const char *s)
{
	return g_utf8_get_char(s);
}

uint32_t utf8_to_utf32_validate(const char *s)
{
	return g_utf8_get_char_validated(s, -1);
}

uint32_t utf32_isprint(uint32_t c)
{
	return g_unichar_isprint(c);
}

uint32_t utf32_isspace(uint32_t c)
{
	return g_unichar_isspace(c);
}

uint32_t utf32_isupper(uint32_t c)
{
	return g_unichar_isupper(c);
}

uint32_t utf32_islower(uint32_t c)
{
	return g_unichar_islower(c);
}

uint32_t utf32_isalnum(uint32_t c)
{
	return g_unichar_isalnum(c);
}

uint32_t utf32_toupper(uint32_t c)
{
	return g_unichar_toupper(c);
}

uint32_t utf32_tolower(uint32_t c)
{
	return g_unichar_tolower(c);
}

char *utf8_next_char(const char *s)
{
	return g_utf8_next_char(s);
}

char *utf8_prev_char(const char *s)
{
	return g_utf8_prev_char(s);
}

char *utf8_strchr(const char *s, uint32_t c)
{
	return g_utf8_strchr(s, -1, c);
}

char *utf8_strcasechr(const char *s, uint32_t c)
{
	c = g_unichar_tolower(c);

	const char *p = s;
	while (*p != '\0' && g_unichar_tolower(g_utf8_get_char(p)) != c) {
		p = g_utf8_next_char(p);
	}
	if (*p == '\0') {
		return NULL;
	}
	return (char *)p;
}

size_t utf8_strlen(const char *s)
{
	return g_utf8_strlen(s, -1);
}

char *utf8_strcasestr(const char * restrict haystack, const char * restrict needle)
{
	char *h = g_utf8_casefold(haystack, -1);
	char *n = g_utf8_casefold(needle, -1);

	char *cmp = strstr(h, n);
	char *ret;

	if (cmp == NULL) {
		ret = NULL;
	} else {
		ret = (char *)haystack + (cmp - h);
	}

	free(h);
	free(n);

	return ret;
}

char *utf8_normalize(const char *s)
{
	return g_utf8_normalize(s, -1, G_NORMALIZE_DEFAULT);
}

char *utf8_compose(const char *s)
{
	return g_utf8_normalize(s, -1, G_NORMALIZE_DEFAULT_COMPOSE);
}

bool utf8_validate(const char *s)
{
	return g_utf8_validate(s, -1, NULL);
}