File: Convert.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (201 lines) | stat: -rw-r--r-- 4,553 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "stdafx.h"
#include "Convert.h"
#include "Utf.h"

namespace storm {

#define WRITE(x)								\
	do {										\
		if (out < maxCount)						\
			to[out] = (x);						\
		out++;									\
	} while (false)

#define TERMINATE()								\
	do {										\
		WRITE(0);								\
		if (maxCount > 0)						\
			to[maxCount - 1] = 0;				\
	} while (false)

#define WRAP_CONVERT(NAME, FROM, TO, TO_GC)								\
	GcArray<TO> *NAME(Engine &e, const FROM *from) {					\
		size_t space = convert(from, (TO *)null, 0);					\
		GcArray<TO> *r = runtime::allocArray<TO>(e, &TO_GC, space);		\
		convert(from, r->v, space);										\
		return r;														\
	}

#define WRAP_CONVERT_SIZE(NAME, FROM, TO, TO_GC)						\
	GcArray<TO> *NAME ## NoNull(Engine &e, const FROM *from, size_t fromCount) { \
		size_t space = convert(from, fromCount, (TO *)null, 0, false);	\
		GcArray<TO> *r = runtime::allocArray<TO>(e, &TO_GC, space);		\
		convert(from, fromCount, r->v, space, false);					\
		return r;														\
	}																	\
	GcArray<TO> *NAME(Engine &e, const FROM *from, size_t fromCount) {	\
		size_t space = convert(from, fromCount, (TO *)null, 0, true);	\
		GcArray<TO> *r = runtime::allocArray<TO>(e, &TO_GC, space);		\
		convert(from, fromCount, r->v, space, true);					\
		return r;														\
	}

	size_t convert(const char *from, wchar *to, size_t maxCount) {
		size_t out = 0;

		const char *at = from;
		while (*at) {
			nat left;
			nat cp = utf8::firstData(*at, left);
			at++;

			for (nat i = 0; i < left; i++) {
				if (utf8::isCont(*at)) {
					cp = utf8::addCont(cp, *at);
					at++;
				} else {
					cp = replacementChar;
					break;
				}
			}

			// Encode...
			if (utf16::split(cp)) {
				WRITE(utf16::splitLeading(cp));
				WRITE(utf16::splitTrailing(cp));
			} else {
				WRITE(wchar(cp));
			}
		}

		TERMINATE();
		return out;
	}

	size_t convert(const char *from, size_t fromCount, wchar *to, size_t maxCount, bool nullTerminate) {
		size_t out = 0;

		size_t at = 0;
		while (at < fromCount) {
			nat left;
			nat cp = utf8::firstData(from[at++], left);

			for (nat i = 0; i < left; i++) {
				if (at < fromCount && utf8::isCont(from[at])) {
					cp = utf8::addCont(cp, from[at++]);
				} else {
					cp = replacementChar;
					break;
				}
			}

			// Encode...
			if (utf16::split(cp)) {
				WRITE(utf16::splitLeading(cp));
				WRITE(utf16::splitTrailing(cp));
			} else {
				WRITE(wchar(cp));
			}
		}

		if (nullTerminate)
			TERMINATE();
		return out;
	}

	size_t convert(const char *from, size_t fromCount, wchar *to, size_t maxCount) {
		return convert(from, fromCount, to, maxCount, true);
	}

	size_t convert(const wchar *from, char *to, size_t maxCount) {
		size_t out = 0;

		const wchar *at = from;
		byte buffer[utf8::maxBytes];
		while (*at) {
			wchar now = *at++;
			nat cp = 0;
			if (utf16::leading(now)) {
				if (utf16::trailing(*at))
					cp = utf16::assemble(now, *at++);
				else
					cp = replacementChar;
			} else {
				cp = now;
			}

			// Encode...
			nat count = 0;
			byte *buf = utf8::encode(cp, buffer, &count);
			for (nat i = 0; i < count; i++)
				WRITE(buf[i]);
		}

		TERMINATE();
		return out;
	}

	size_t convert(const wchar *from, size_t fromCount, char *to, size_t maxCount, bool nullTerminate) {
		size_t out = 0;

		size_t at = 0;
		byte buffer[utf8::maxBytes];
		while (at < fromCount) {
			wchar now = from[at++];
			nat cp = 0;
			if (utf16::leading(now)) {
				if (utf16::trailing(from[at]))
					cp = utf16::assemble(now, from[at++]);
				else
					cp = replacementChar;
			} else {
				cp = now;
			}

			// Encode...
			nat count = 0;
			byte *buf = utf8::encode(cp, buffer, &count);
			for (nat i = 0; i < count; i++)
				WRITE(buf[i]);
		}

		if (nullTerminate)
			TERMINATE();
		return out;
	}

	size_t convert(const wchar *from, size_t fromCount, char *to, size_t maxCount) {
		return convert(from, fromCount, to, maxCount, true);
	}

	WRAP_CONVERT(toWChar, char, wchar, wcharArrayType);
	WRAP_CONVERT_SIZE(toWChar, char, wchar, wcharArrayType);

	WRAP_CONVERT(toChar, wchar, char, byteArrayType);
	WRAP_CONVERT_SIZE(toChar, wchar, char, byteArrayType);


#ifdef POSIX

	size_t convert(const wchar_t *from, wchar *to, size_t maxCount) {
		size_t out = 0;

		for (const wchar_t *at = from; *at; at++) {
			nat cp = *at;
			if (utf16::split(cp)) {
				WRITE(utf16::splitLeading(cp));
				WRITE(utf16::splitTrailing(cp));
			} else {
				WRITE(wchar(cp));
			}
		}

		TERMINATE();
		return out;
	}

	WRAP_CONVERT(toWChar, wchar_t, wchar, wcharArrayType);

#endif

}