-
Notifications
You must be signed in to change notification settings - Fork 0
/
wchar_.d
428 lines (391 loc) · 10.3 KB
/
wchar_.d
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* D header file for C99.
*
* $(C_HEADER_DESCRIPTION pubs.opengroup.org/onlinepubs/009695399/basedefs/_wchar.h.html, _wchar.h)
*
* Copyright: Copyright Sean Kelly 2005 - 2009.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Sean Kelly
* Source: $(DRUNTIMESRC core/stdc/_wchar_.d)
* Standards: ISO/IEC 9899:1999 (E)
*/
private import config;
private import stdarg; // for va_list
private import stdio; // for FILE, not exposed per spec
public import stddef; // for wchar_t
public import time; // for tm
public import stdint; // for WCHAR_MIN, WCHAR_MAX
import errno;
import stdlib;
extern (C):
@system:
nothrow:
@nogc:
version (CRuntime_Glibc)
{
///
struct mbstate_t
{
int __count;
union ___value
{
wint_t __wch = 0;
char[4] __wchb;
}
___value __value;
}
}
else version (FreeBSD)
{
///
union __mbstate_t // <sys/_types.h>
{
char[128] _mbstate8 = 0;
long _mbstateL;
}
///
alias mbstate_t = __mbstate_t;
}
else version (NetBSD)
{
///
union __mbstate_t
{
int64_t __mbstateL;
char[128] __mbstate8;
}
///
alias mbstate_t = __mbstate_t;
}
else version (OpenBSD)
{
///
union __mbstate_t
{
char[128] __mbstate8 = 0;
int64_t __mbstateL;
}
///
alias mbstate_t = __mbstate_t;
}
else version (DragonFlyBSD)
{
///
union __mbstate_t // <sys/stdint.h>
{
char[128] _mbstate8 = 0;
long _mbstateL;
}
///
alias mbstate_t = __mbstate_t;
}
else version (Solaris)
{
///
struct __mbstate_t
{
version (D_LP64)
{
long[4] __filler;
}
else
{
int[6] __filler;
}
}
///
alias mbstate_t = __mbstate_t;
}
else version (CRuntime_UClibc)
{
///
struct mbstate_t
{
wchar_t __mask = 0;
wchar_t __wc = 0;
}
}
else
{
///
alias int mbstate_t;
}
///
alias wchar_t wint_t;
///
enum wchar_t WEOF = 0xFFFF;
///
int fwprintf(FILE* stream, const scope wchar_t* format, ...);
///
int fwscanf(FILE* stream, const scope wchar_t* format, ...);
///
int swprintf(wchar_t* s, size_t n, const scope wchar_t* format, ...);
///
int swscanf(const scope wchar_t* s, const scope wchar_t* format, ...);
///
int vfwprintf(FILE* stream, const scope wchar_t* format, va_list arg);
///
int vfwscanf(FILE* stream, const scope wchar_t* format, va_list arg);
///
int vswprintf(wchar_t* s, size_t n, const scope wchar_t* format, va_list arg);
///
int vswscanf(const scope wchar_t* s, const scope wchar_t* format, va_list arg);
///
int vwprintf(const scope wchar_t* format, va_list arg);
///
int vwscanf(const scope wchar_t* format, va_list arg);
///
int wprintf(const scope wchar_t* format, ...);
///
int wscanf(const scope wchar_t* format, ...);
// No unsafe pointer manipulation.
@trusted
{
///
wint_t fgetwc(FILE* stream);
///
wint_t fputwc(wchar_t c, FILE* stream);
}
///
wchar_t* fgetws(wchar_t* s, int n, FILE* stream);
///
int fputws(const scope wchar_t* s, FILE* stream);
// No unsafe pointer manipulation.
extern (D) @trusted
{
///
wint_t getwchar() { return fgetwc(stdin); }
///
wint_t putwchar(wchar_t c) { return fputwc(c,stdout); }
///
wint_t getwc(FILE* stream) { return fgetwc(stream); }
///
wint_t putwc(wchar_t c, FILE* stream) { return fputwc(c, stream); }
}
// No unsafe pointer manipulation.
@trusted
{
///
wint_t ungetwc(wint_t c, FILE* stream);
///
version (CRuntime_Microsoft)
{
// MSVC defines this as an inline function.
int fwide(FILE* stream, int mode) { return mode; }
}
else
{
int fwide(FILE* stream, int mode);
}
}
///
double wcstod(const scope wchar_t* nptr, wchar_t** endptr);
///
float wcstof(const scope wchar_t* nptr, wchar_t** endptr);
///
real wcstold(const scope wchar_t* nptr, wchar_t** endptr);
///
c_long wcstol(const scope wchar_t* nptr, wchar_t** endptr, int base);
///
long wcstoll(const scope wchar_t* nptr, wchar_t** endptr, int base);
///
c_ulong wcstoul(const scope wchar_t* nptr, wchar_t** endptr, int base);
///
ulong wcstoull(const scope wchar_t* nptr, wchar_t** endptr, int base);
///
pure wchar_t* wcscpy(return wchar_t* s1, scope const wchar_t* s2);
///
pure wchar_t* wcsncpy(return wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure wchar_t* wcscat(return wchar_t* s1, scope const wchar_t* s2);
///
pure wchar_t* wcsncat(return wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure int wcscmp(scope const wchar_t* s1, scope const wchar_t* s2);
///
int wcscoll(scope const wchar_t* s1, scope const wchar_t* s2);
///
pure int wcsncmp(scope const wchar_t* s1, scope const wchar_t* s2, size_t n);
///
size_t wcsxfrm(scope wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure inout(wchar_t)* wcschr(return inout(wchar_t)* s, wchar_t c);
///
pure size_t wcscspn(scope const wchar_t* s1, scope const wchar_t* s2);
///
pure inout(wchar_t)* wcspbrk(return inout(wchar_t)* s1, scope const wchar_t* s2);
///
pure inout(wchar_t)* wcsrchr(return inout(wchar_t)* s, wchar_t c);
///
pure size_t wcsspn(scope const wchar_t* s1, scope const wchar_t* s2);
///
pure inout(wchar_t)* wcsstr(return inout(wchar_t)* s1, scope const wchar_t* s2);
///
wchar_t* wcstok(return wchar_t* s1, scope const wchar_t* s2, wchar_t** ptr);
///
pure size_t wcslen(scope const wchar_t* s);
///
pure wchar_t* wmemchr(return const wchar_t* s, wchar_t c, size_t n);
///
pure int wmemcmp(scope const wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure wchar_t* wmemcpy(return wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure wchar_t* wmemmove(return wchar_t* s1, scope const wchar_t* s2, size_t n);
///
pure wchar_t* wmemset(return wchar_t* s, wchar_t c, size_t n);
///
size_t wcsftime(wchar_t* s, size_t maxsize, const scope wchar_t* format, const scope tm* timeptr);
version (Windows)
{
///
wchar_t* _wasctime(tm*); // non-standard
///
wchar_t* _wctime(time_t*); // non-standard
///
wchar_t* _wstrdate(wchar_t*); // non-standard
///
wchar_t* _wstrtime(wchar_t*); // non-standard
}
// No unsafe pointer manipulation.
@trusted
{
///
wint_t btowc(int c);
///
int wctob(wint_t c);
}
///
int mbsinit(const scope mbstate_t* ps);
///
size_t mbrlen(const scope char* s, size_t n, mbstate_t* ps);
///
//size_t mbrtowc(wchar_t* pwc, const scope char* s, size_t n, mbstate_t* ps);
///
size_t wcrtomb(char* s, wchar_t wc, mbstate_t* ps);
///
size_t mbsrtowcs(wchar_t* dst, const scope char** src, size_t len, mbstate_t* ps);
///
size_t wcsrtombs(char* dst, const scope wchar_t** src, size_t len, mbstate_t* ps);
uint32_t OOB(uint32_t c, uint32_t b) {
return ((((b)>>3)-0x10)|(((b)>>3)+(cast(int32_t)(c)>>26))) & ~7;
}
uint32_t R(uint32_t a, uint32_t b) { return ( a == 0x80 ? 0x40u - b : 0u - a) << 23;}
uint32_t FAILSTATE() { return R(0x80,0x80); }
uint32_t C(uint32_t x) { return x = ( x < 2 ? -1 : (R(0x80,0xc0) | x) ); }
uint32_t D(uint32_t x) { return x = C(x+16); }
uint32_t E(uint32_t x) { return x = ( x == 0 ? R(0xa0,0xc0) :
x == 0xd ? R(0x80,0xa0) :
R(0x80,0xc0))
| ( R(0x80,0xc0) >> 6 )
| x; }
uint32_t F(uint32_t x) { return x = ( x >= 5 ? 0:
x == 0 ? R(0x90,0xc0) :
x == 4 ? R(0x80,0x90) :
R(0x80,0xc0) )
| ( R(0x80,0xc0) >> 6 )
| ( R(0x80,0xc0) >> 12 )
| x; }
const uint32_t[] bittab = [
C(0x2),C(0x3),C(0x4),C(0x5),C(0x6),C(0x7),
C(0x8),C(0x9),C(0xa),C(0xb),C(0xc),C(0xd),C(0xe),C(0xf),
D(0x0),D(0x1),D(0x2),D(0x3),D(0x4),D(0x5),D(0x6),D(0x7),
D(0x8),D(0x9),D(0xa),D(0xb),D(0xc),D(0xd),D(0xe),D(0xf),
E(0x0),E(0x1),E(0x2),E(0x3),E(0x4),E(0x5),E(0x6),E(0x7),
E(0x8),E(0x9),E(0xa),E(0xb),E(0xc),E(0xd),E(0xe),E(0xf),
F(0x0),F(0x1),F(0x2),F(0x3),F(0x4)
];
enum SA = 0xc2u;
enum SB = 0xf5u;
/*
* musl
* This code was written by Rich Felker in 2010; no copyright is claimed.
* This code is in the public domain. Attribution is appreciated but
* unnecessary.
*/
size_t mbrtowc(wchar_t *wc, const char *src, size_t n, mbstate_t *st)
{
static uint internal_state;
uint c;
ubyte *s = cast(ubyte *)src;
const ulong N = n;
if (!st) st = cast(mbstate_t *)&internal_state;
c = *cast(uint *)st;
if (!s) {
s = cast(ubyte *)"";
wc = cast(dchar *)&wc;
n = 1;
} else if (!wc) wc = cast(dchar *)&wc;
if (!n) return -2;
if (!c) {
if (*s < 0x80) {
*wc = *s;
return !!*wc;
}
if (*s-SA > SB-SA) goto ilseq;
c = bittab[*s++-SA]; n--;
}
if (n) {
if (OOB(c,*s)) goto ilseq;
loop:
c = c<<6 | *s++-0x80; n--;
if (!(c&(1U<<31))) {
*cast(uint *)st = 0;
*wc = c;
return N-n;
}
if (n) {
if (*s-0x80u >= 0x40) goto ilseq;
goto loop;
}
}
*cast(uint *)st = c;
return -2;
ilseq:
*cast(uint *)st = FAILSTATE;
errno.errno(EILSEQ);
return -1;
}
int wctomb(char* s, wchar_t wc)
{
if (!s) return 0;
return cast(int)wcrtomb(s, wc, cast(mbstate_t*)(0));
}
bool IS_CODEUNIT(dchar c)
{
return cast(uint)c - 0xdf80 < 0x80;
}
size_t wcrtomb(char* s, wchar_t wc, mbstate_t* st)
{
if (!s) return 1;
if (cast(char)wc < 0x80) {
*s = cast(char)wc;
return 1;
} else if (MB_CUR_MAX == 1) {
if (!IS_CODEUNIT(wc)) {
errno.errno(EILSEQ);
return -1;
}
*s = cast(char)wc;
return 1;
} else if (cast(char)wc < 0x800) {
*s++ = cast(char)(0xc0 | (wc>>6));
*s = 0x80 | (wc&0x3f);
return 2;
} else if (cast(char)wc < 0xd800 || (cast(char)wc-0xe000 < 0x2000)) {
*s++ = cast(char)(0xe0 | (wc>>12));
*s++ = 0x80 | ((wc>>6)&0x3f);
*s = 0x80 | (wc&0x3f);
return 3;
} else if (cast(char)wc-0x10000 < 0x100000) {
*s++ = 0xf0 | (wc>>18);
*s++ = 0x80 | ((wc>>12)&0x3f);
*s++ = 0x80 | ((wc>>6)&0x3f);
*s = 0x80 | (wc&0x3f);
return 4;
}
errno.errno(EILSEQ);
return -1;
}