summaryrefslogtreecommitdiff
path: root/lib/mlibc/options/internal/generic/charset.cpp
blob: c42b4f457e778514d96f809587f8075e57da090c (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <bits/ensure.h>
#include <mlibc/charset.hpp>
#include <mlibc/debug.hpp>

namespace mlibc {

bool charset::is_ascii_superset() {
	// TODO: For locales that change the meaning of ASCII chars, this needs to be changed.
	return true;
}

bool charset::is_alpha(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_alpha() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_digit(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c >= '0' && c <= '9';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_digit() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_xdigit(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_xdigit() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_alnum(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_alnum() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_punct(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c == '!' || c == '"' || c == '#' || c == '$' || c == '%' || c == '&'
				|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' || c == ','
				|| c == '-' || c == '.' || c == '/'
				|| c == ':' || c == ';' || c == '<' || c == '=' || c == '>' || c == '?'
				|| c == '@'
				|| c == '[' || c == '\\' || c == ']' || c == '^' || c == '_' || c == '`'
				|| c == '{' || c == '|' || c == '}' || c == '~';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_punct() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_graph(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c >= 0x21 && c <= 0x7E;
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_graph() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_blank(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c == ' ' || c == '\t';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_blank() is not implemented"
				" for the full Unicode charset " << c << frg::endlog;
	return false;
}

bool charset::is_space(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_space() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_print(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return c >= 0x20 && c <= 0x7E;
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_print() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_lower(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return (c >= 'a' && c <= 'z');
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_print() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

bool charset::is_upper(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		return (c >= 'A' && c <= 'Z');
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::is_print() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return false;
}

codepoint charset::to_lower(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		if(c >= 'A' && c <= 'Z')
			return c - 'A' + 'a';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::to_lower() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return c;
}

codepoint charset::to_upper(codepoint c) {
	if(c <= 0x7F && is_ascii_superset())
		if(c >= 'a' && c <= 'z')
			return c - 'a' + 'A';
	if(c > 0x7F)
		mlibc::infoLogger() << "mlibc: charset::to_upper() is not implemented"
				" for the full Unicode charset" << frg::endlog;
	return c;
}

charset *current_charset() {
	static charset global_charset;
	return &global_charset;
}

} // namespace mlibc