-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtext.d
More file actions
823 lines (681 loc) · 25.9 KB
/
text.d
File metadata and controls
823 lines (681 loc) · 25.9 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/**
* Contains classes representing ASCII, UTF-7, UTF-8 and UTF-16 character encodings.
*
* Copyright: (c) 2009 John Chapman
*
* License: See $(LINK2 ..\..\licence.txt, licence.txt) for use and distribution terms.
*/
module juno.base.text;
import juno.base.core,
juno.base.string,
juno.base.native,
juno.com.core;
import std.conv;
import std.string : icmp;
// MLang
// Doesn't support UTF-32 yet.
enum : uint {
MIMECONTF_MAILNEWS = 0x1,
MIMECONTF_BROWSER = 0x2,
MIMECONTF_MINIMAL = 0x4,
MIMECONTF_IMPORT = 0x8,
MIMECONTF_SAVABLE_MAILNEWS = 0x100,
MIMECONTF_SAVABLE_BROWSER = 0x200,
MIMECONTF_EXPORT = 0x400,
MIMECONTF_PRIVCONVERTER = 0x10000,
MIMECONTF_VALID = 0x20000,
MIMECONTF_VALID_NLS = 0x40000,
MIMECONTF_MIME_IE4 = 0x10000000,
MIMECONTF_MIME_LATEST = 0x20000000,
MIMECONTF_MIME_REGISTRY = 0x40000000
}
struct MIMECPINFO {
uint dwFlags;
uint uiCodePage;
uint uiFamilyCodePage;
wchar[64] wszDescription;
wchar[50] wszWebCharset;
wchar[50] wszHeaderCharset;
wchar[50] wszBodyCharset;
wchar[32] wszFixedWidthFont;
wchar[32] wszProportionalFont;
ubyte bGDICharset;
}
struct MIMECSETINFO {
uint uiCodePage;
uint uiInternetEncoding;
wchar[50] wszCharset;
}
struct RFC1766INFO {
uint lcid;
wchar[6] wszRfc1766;
wchar[32] wszLocaleName;
}
struct SCRIPTINFO {
ubyte ScriptId;
uint uiCodePage;
wchar[64] wszDescription;
wchar[32] wszFixedWidthFont;
wchar[32] wszProportionalFont;
}
struct DetectEncodingInfo {
uint nLangID;
uint nCodePage;
int nDocPercent;
int nConfidence;
}
interface IEnumCodePage : IUnknown {
mixin(uuid("275c23e3-3747-11d0-9fea-00aa003f8646"));
int Clone(out IEnumCodePage ppEnum);
int Next(uint celt, MIMECPINFO* rgelt, out uint pceltFetched);
int Reset();
int Skip(uint celt);
}
interface IEnumRfc1766 : IUnknown {
mixin(uuid("3dc39d1d-c030-11d0-b81b-00c04fc9b31f"));
int Clone(out IEnumRfc1766 ppEnum);
int Next(uint celt, RFC1766INFO* rgelt, out uint pceltFetched);
int Reset();
int Skip(uint celt);
}
interface IEnumScript : IUnknown {
mixin(uuid("AE5F1430-388B-11d2-8380-00C04F8F5DA1"));
int Clone(out IEnumScript ppEnum);
int Next(uint celt, SCRIPTINFO* rgelt, out uint pceltFetched);
int Reset();
int Skip(uint celt);
}
interface IMLangConvertCharset : IUnknown {
mixin(uuid("d66d6f98-cdaa-11d0-b822-00c04fc9b31f"));
int Initialize(uint uiSrcCodePage, uint uiDstCodePage, uint dwProperty);
int GetSourceCodePage(out uint puiSrcCodePage);
int GetDestinationCodePage(out uint puiDstCodePage);
int GetProperty(out uint pdwProperty);
int DoConversion(ubyte* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
int DoConversionToUnicode(ubyte* pSrcStr, ref uint pcSrcSize, wchar* pDstStr, ref uint pcDstSize);
int DoConversionFromUnicode(wchar* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
}
interface IMultiLanguage : IUnknown {
mixin(uuid("275c23e1-3747-11d0-9fea-00aa003f8646"));
int GetNumberOfCodePageInfo(out uint pcCodePage);
int GetCodePageInfo(uint uiCodePage, out MIMECPINFO pCodePageInfo);
int GetFamilyCodePage(uint uiCodePage, out uint puiFamilyCodePage);
int EnumCodePages(uint grfFlags, out IEnumCodePage ppEnumCodePage);
int GetCharsetInfo(wchar* Charset, out MIMECSETINFO pCharsetInfo);
int IsConvertible(uint dwSrcEncoding, uint dwDstEncoding);
int ConvertString(ref uint pdwMode, uint dwSrcEncoding, uint dwDstEncoding, ubyte* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
int ConvertStringToUnicode(ref uint pdwMode, uint dwEncoding, ubyte* pSrcStr, ref uint pcSrcSize, wchar* pDstStr, ref uint pcDstSize);
int ConvertStringFromUnicode(ref uint pdwMode, uint dwEncoding, wchar* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
int ConvertStringReset();
int GetRfc1766FromLcid(uint Locale, out wchar* pbstrRfc1766);
int GetLcidFromRfc1766(out uint Locale, wchar* bstrRfc1766);
int EnumRfc1766(out IEnumRfc1766 ppEnumRfc1766);
int GetRfc1766Info(uint Locale, out RFC1766INFO pRfc1766Info);
int CreateConvertCharset(uint uiSrcCodePage, uint uiDstCodePage, uint dwProperty, out IMLangConvertCharset ppMLangConvertCharset);
}
interface IMultiLanguage2 : IUnknown {
mixin(uuid("DCCFC164-2B38-11d2-B7EC-00C04F8F5D9A"));
int GetNumberOfCodePageInfo(out uint pcCodePage);
int GetCodePageInfo(uint uiCodePage, ushort LangId, out MIMECPINFO pCodePageInfo);
int GetFamilyCodePage(uint uiCodePage, out uint puiFamilyCodePage);
int EnumCodePages(uint grfFlags, ushort LangId, out IEnumCodePage ppEnumCodePage);
int GetCharsetInfo(wchar* Charset, out MIMECSETINFO pCharsetInfo);
int IsConvertible(uint dwSrcEncoding, uint dwDstEncoding);
int ConvertString(ref uint pdwMode, uint dwSrcEncoding, uint dwDstEncoding, ubyte* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
int ConvertStringToUnicode(ref uint pdwMode, uint dwEncoding, ubyte* pSrcStr, ref uint pcSrcSize, wchar* pDstStr, ref uint pcDstSize);
int ConvertStringFromUnicode(ref uint pdwMode, uint dwEncoding, wchar* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize);
int ConvertStringReset();
int GetRfc1766FromLcid(uint Locale, out wchar* pbstrRfc1766);
int GetLcidFromRfc1766(out uint Locale, wchar* bstrRfc1766);
int EnumRfc1766(out IEnumRfc1766 ppEnumRfc1766);
int GetRfc1766Info(uint Locale, out RFC1766INFO pRfc1766Info);
int CreateConvertCharset(uint uiSrcCodePage, uint uiDstCodePage, uint dwProperty, out IMLangConvertCharset ppMLangConvertCharset);
int ConvertStringInIStream(ref uint pdwMode, uint dwFlag, wchar* lpFallBack, uint dwSrcEncoding, uint dwDstEncoding, IStream pstmIn, IStream pstmOut);
int ConvertStringToUnicodeEx(uint dwEncoding, ubyte* pSrcStr, ref uint pcSrcSize, wchar* pDstStr, ref uint pcDstSize, uint dwFlag, wchar* lpFallBack);
int ConvertStringFromUnicodeEx(uint dwEncoding, wchar* pSrcStr, ref uint pcSrcSize, ubyte* pDstStr, ref uint pcDstSize, uint dwFlag, wchar* lpFallBack);
int DetectCodepageInIStream(uint dwFlag, uint dwPrefWinCodePage, IStream pstmIn, ref DetectEncodingInfo lpEncoding, ref int pcScores);
int DetectInputCodepage(uint dwFlag, uint dwPrefWinCodePage, ubyte* pSrcStr, ref int pcSrcSize, ref DetectEncodingInfo lpEncoding, ref int pcScores);
int ValidateCodePage(uint uiCodePage, Handle hwnd);
int GetCodePageDescription(uint uiCodePage, uint lcid, wchar* lpWideCharStr, int cchWideChar);
int IsCodePageInstallable(uint uiCodePage);
int SetMimeDBSource(uint dwSource);
int GetNumberOfScripts(out uint pnScripts);
int EnumScripts(uint dwFlags, ushort LangId, out IEnumScript ppEnumScript);
int ValidateCodePageEx(uint uiCodePage, Handle hwnd, uint dwfIODControl);
}
interface IMultiLanguage3 : IMultiLanguage2 {
mixin(uuid("4e5868ab-b157-4623-9acc-6a1d9caebe04"));
int DetectOutboundCodePage(uint dwFlags, wchar* lpWideCharStr, int cchWideChar, uint* puiPreferredCodePages, uint nPreferredCodePages, uint* puiDetectedCodePages, ref uint pnDetectedCodePages, wchar* lpSpecialChar);
int DetectOutboundCodePageInIStream(uint dwFlags, IStream pStrIn, uint* puiPreferredCodePages, uint nPreferredCodePages, uint* puiDetectedCodePages, wchar* lpSpecialChar);
}
abstract final class CMultiLanguage {
mixin(uuid("275c23e2-3747-11d0-9fea-00aa003f8646"));
mixin Interfaces!(IMultiLanguage2);
}
extern(Windows)
alias DllImport!("mlang.dll", "ConvertINetString",
int function(uint* lpdwMode, uint dwSrcEncoding, uint dwDstEncoding, in ubyte* lpSrcStr, int* lpnSrcSize, ubyte* lpDstStr, int* lpnDstSize))
ConvertINetString;
extern(Windows)
alias DllImport!("mlang.dll", "IsConvertINetStringAvailable",
int function(uint dwSrcEncoding, uint dwDstEncoding))
IsConvertINetStringAvailable;
private struct CodePageInfo {
uint codePage;
uint familyCodePage;
string webName;
string headerName;
string bodyName;
string description;
uint flags;
}
private CodePageInfo[] codePageInfoTable;
private CodePageInfo[uint] codePageInfoByCodePage;
private uint[string] codePageByName;
static ~this() {
codePageInfoTable = null;
codePageInfoByCodePage = null;
codePageByName = null;
}
private void initCodePageInfo() {
synchronized {
if (auto mlang = CMultiLanguage.coCreate!(IMultiLanguage2)()) {
scope(exit) mlang.Release();
IEnumCodePage cp;
if (SUCCEEDED(mlang.EnumCodePages(MIMECONTF_MIME_LATEST, 0, cp))) {
scope(exit) cp.Release();
uint num = 0;
if (SUCCEEDED(mlang.GetNumberOfCodePageInfo(num)) && num > 0) {
MIMECPINFO* cpInfo = cast(MIMECPINFO*)CoTaskMemAlloc(num * MIMECPINFO.sizeof);
uint count = 0;
if (SUCCEEDED(cp.Next(num, cpInfo, count)) && count > 0) {
codePageInfoTable.length = count;
for (uint index = 0; index < count; index++) {
with (cpInfo[index]) {
codePageInfoTable[index] = CodePageInfo(
uiCodePage,
uiFamilyCodePage,
to!string(toArray(wszWebCharset.ptr)),
to!string(toArray(wszHeaderCharset.ptr)),
to!string(toArray(wszBodyCharset.ptr)),
to!string(toArray(wszDescription.ptr)),
dwFlags);
}
}
}
CoTaskMemFree(cpInfo);
}
else assert(false);
}
}
else assert(false);
}
}
private CodePageInfo* getCodePageInfo(uint codePage) {
if (codePageInfoTable == null)
initCodePageInfo();
if (auto value = codePage in codePageInfoByCodePage)
return value;
uint cp;
for (int i = 0; i < codePageInfoTable.length && (cp = codePageInfoTable[i].codePage) != 0; i++) {
if (cp == codePage) {
codePageInfoByCodePage[codePage] = codePageInfoTable[i];
return &codePageInfoByCodePage[codePage];
}
}
return null;
}
private uint getCodePageFromName(string name) {
if (codePageInfoTable == null)
initCodePageInfo();
if (auto value = name in codePageByName)
return *value;
for (int i = 0; i < codePageInfoTable.length; i++) {
if (icmp(codePageInfoTable[i].webName, name) == 0
|| (codePageInfoTable[i].codePage == 1200 && icmp("utf-16", name) == 0)) {
uint cp = codePageInfoTable[i].codePage;
codePageByName[name] = cp;
return cp;
}
}
throw new ArgumentException("'" ~ name ~ "' is not a supported encoding name.", "name");
}
/**
* Represents a character encoding.
*/
abstract class Encoding {
private static const uint CP_DEFAULT = 0;
private static const uint CP_ASCII = 20127;
private static const uint CP_UTF16 = 1200;
private static const uint CP_UTF16BE = 1201;
private static const uint CP_UTF32 = 12000;
private static const uint CP_UTF32BE = 12001;
private static const uint CP_WINDOWS_1252 = 1252;
private static const uint ISO_8859_1 = 28591;
private static const uint ISO_SIMPLIFIED_CN = 50227;
private static const uint GB18030 = 54936;
private static const uint ISO_8859_8I = 38598;
private static const uint ISCII_DEVANAGARI = 57002;
private static const uint ISCII_BENGALI = 57003;
private static const uint ISCII_TAMIL = 57004;
private static const uint ISCII_TELUGU = 57005;
private static const uint ISCII_ASSEMESE = 57006;
private static const uint ISCII_ORIYA = 57007;
private static const uint ISCII_KANNADA = 57008;
private static const uint ISCII_MALAYALAM = 57009;
private static const uint ISCII_GUJARATHI = 57010;
private static const uint ISCII_PUNJABI = 507011;
private static Encoding[uint] encodings_;
private static Encoding defaultEncoding_;
private static Encoding asciiEncoding_;
private static Encoding utf7Encoding_;
private static Encoding utf8Encoding_;
private static Encoding utf16Encoding_;
protected uint codePage_;
private CodePageInfo* cpInfo_;
static ~this() {
defaultEncoding_ = null;
asciiEncoding_ = null;
utf7Encoding_ = null;
utf8Encoding_ = null;
utf16Encoding_ = null;
encodings_ = null;
}
/**
* Converts a byte array from one encoding to another.
* Params:
* sourceEncoding = The encoding format of bytes.
* destEncoding = The target encoding format.
* bytes = The _bytes to _convert.
* Returns: A byte array containing the results of converting bytes from srcEncoding to destEncoding.
*/
static ubyte[] convert(Encoding sourceEncoding, Encoding destEncoding, in ubyte[] bytes) {
return convert(sourceEncoding, destEncoding, bytes, 0, to!int(bytes.length));
}
/**
* Converts a range of _bytes in a byte array from one encoding to another.
* Params:
* sourceEncoding = The encoding format of bytes.
* destEncoding = The target encoding format.
* bytes = The _bytes to _convert.
* index = The _index of the first element of bytes to _convert.
* count = The number of _bytes to _convert.
* Returns: A byte array containing the results of converting bytes from srcEncoding to destEncoding.
*/
static ubyte[] convert(Encoding sourceEncoding, Encoding destEncoding, in ubyte[] bytes, int index, int count) {
return destEncoding.encode(sourceEncoding.decode(bytes, index, count));
}
/**
*/
abstract int encodeLength(in char[] chars, int index, int count);
/**
* ditto
*/
int encodeLength(in char[] chars) {
return encodeLength(chars, 0, to!int(chars.length));
}
/**
*/
abstract int decodeLength(in ubyte[] bytes, int index, int count);
/**
* ditto
*/
int decodeLength(in ubyte[] bytes) {
return decodeLength(bytes, 0, to!int(bytes.length));
}
abstract int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex);
/**
* Encodes a set of characters from the specified character array into a sequence of bytes.
* Params:
* chars = The character array containing the set of characters to _encode.
* index = The _index of the first character to _encode.
* count = The number of characters to _encode.
* Returns: A byte array containing the results of encoding the specified set of characters.
*/
ubyte[] encode(in char[] chars, int index, int count) {
ubyte[] bytes = new ubyte[encodeLength(chars, index, count)];
encode(chars, index, count, bytes, 0);
return bytes;
}
/**
* ditto
*/
ubyte[] encode(in char[] chars) {
return encode(chars, 0, to!int(chars.length));
}
/**
*/
abstract int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
/**
* Decodes a sequence of _bytes from the specified byte array into a set of characters.
* Params:
* bytes = The byte array containing the sequence of _bytes to _decode.
* index = The _index of the first byte to _decode.
* count = The number of _bytes to _decode.
* Returns: A character array containing the results of decoding the specified sequence of _bytes.
*/
char[] decode(in ubyte[] bytes, int index, int count) {
char[] chars = new char[decodeLength(bytes, index, count)];
decode(bytes, index, count, chars, 0);
return chars;
}
/**
* ditto
*/
char[] decode(in ubyte[] bytes) {
return decode(bytes, 0, to!int(bytes.length));
}
/**
* Returns an encoding associated with the specified code page identifier.
* Params: codePage = The code page identifier of the encoding.
* Returns: The encoding associated with the specified code page.
*/
static Encoding get(uint codePage) {
if (auto value = codePage in encodings_)
return *value;
synchronized (Encoding.classinfo) {
Encoding enc = null;
switch (codePage) {
case CP_DEFAULT:
enc = DEFAULT;
break;
case CP_ASCII:
enc = Encoding.ASCII;
break;
case CP_UTF8:
enc = Encoding.UTF8;
break;
case CP_UTF7:
enc = Encoding.UTF7;
break;
case CP_UTF16:
enc = Encoding.UTF16;
break;
case CP_UTF16BE:
enc = new Utf16Encoding(true);
break;
case CP_WINDOWS_1252,
ISO_SIMPLIFIED_CN, GB18030, ISO_8859_8I, ISCII_DEVANAGARI,
ISCII_BENGALI, ISCII_TAMIL, ISCII_TELUGU, ISCII_ASSEMESE,
ISCII_ORIYA, ISCII_KANNADA, ISCII_MALAYALAM, ISCII_GUJARATHI,
ISCII_PUNJABI:
enc = new MLangEncoding(codePage);
break;
default:
CPINFO cpi;
GetCPInfo(codePage, cpi);
if (cpi.MaxCharSize == 1 || cpi.MaxCharSize == 2) {
enc = new MLangEncoding(codePage);
break;
}
import std.string;
throw new NotSupportedException(std.string.format("%s is not a supported code page.", codePage));
}
return encodings_[codePage] = enc;
}
}
/**
* Returns an encoding associated with the specified code page name.
* Params: name = The code page name of the encoding.
* Returns: The encoding associated with the specified code page.
*/
static Encoding get(string name) {
return Encoding.get(getCodePageFromName(name));
}
/**
* Gets an encoding for the system's ANSI code page.
* Returns: An encoding for the system's ANSI code page.
*/
static @property Encoding DEFAULT() {
if (defaultEncoding_ is null)
defaultEncoding_ = Encoding.get(GetACP());
return defaultEncoding_;
}
/**
* Gets an encoding for the ASCII character set.
* Returns: An encoding for the ASCII character set.
*/
static @property Encoding ASCII() {
if (asciiEncoding_ is null)
asciiEncoding_ = new AscIIEncoding;
return asciiEncoding_;
}
/**
* Gets an encoding for the UTF-7 format.
* Returns: An encoding for the UTF-7 format.
*/
static @property Encoding UTF7() {
if (utf7Encoding_ is null)
utf7Encoding_ = new Utf7Encoding;
return utf7Encoding_;
}
/**
* Gets an encoding for the UTF-8 format.
* Returns: An encoding for the UTF-8 format.
*/
static @property Encoding UTF8() {
if (utf8Encoding_ is null)
utf8Encoding_ = new Utf8Encoding;
return utf8Encoding_;
}
/**
* Gets an encoding for the UTF-16 format using the little endian byte order.
* Returns: An encoding for the UTF-16 format using the little endian byte order.
*/
static @property Encoding UTF16() {
if (utf16Encoding_ is null)
utf16Encoding_ = new Utf16Encoding;
return utf16Encoding_;
}
@property uint codePage() {
return codePage_;
}
/**
* Gets a _description of the encoding.
* Returns: A _description of the encoding.
*/
@property string description() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return cpInfo_.description;
}
/**
* Gets the name registered with the IANA.
* Returns: The IANA name.
*/
@property string webName() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return cpInfo_.webName;
}
/**
* Gets a name that can be used with mail agent header tags.
* Returns: A name that can be used with mail agent header tags.
*/
@property string headerName() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return cpInfo_.headerName;
}
/**
* Gets a name that can be used with mail agent body tags.
* Returns: A name that can be used with mail agent body tags.
*/
@property string bodyName() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return cpInfo_.bodyName;
}
bool isBrowserDisplay() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return (cpInfo_.flags & MIMECONTF_BROWSER) != 0;
}
bool isMailNewsDisplay() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return (cpInfo_.flags & MIMECONTF_MAILNEWS) != 0;
}
bool isBrowserSave() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return (cpInfo_.flags & MIMECONTF_SAVABLE_BROWSER) != 0;
}
bool isMailNewsSave() {
if (cpInfo_ == null)
cpInfo_ = getCodePageInfo(codePage_);
return (cpInfo_.flags & MIMECONTF_SAVABLE_MAILNEWS) != 0;
}
/**
* Initialize a new instance that corresponds to the specified code page.
* Params: codePage = The code page identifier of the encoding.
*/
protected this(uint codePage) {
codePage_ = codePage;
}
}
private final class MLangEncoding : Encoding {
this(uint codePage) {
super(codePage == 0 ? GetACP() : codePage);
}
override int encodeLength(in char[] chars, int index, int count) {
if (IsConvertINetStringAvailable(CP_UTF8, codePage_) == S_FALSE)
throw new ArgumentException("Could not encode.");
uint dwMode;
int bytesLength;
int charsLength = count;
ConvertINetString(&dwMode, CP_UTF8, codePage_, cast(ubyte*)(chars.ptr + index), &charsLength, null, &bytesLength);
return bytesLength;
}
override int decodeLength(in ubyte[] bytes, int index, int count) {
if (IsConvertINetStringAvailable(codePage_, CP_UTF8) == S_FALSE)
throw new ArgumentException("Could not decode.");
uint dwMode;
int charsLength;
int bytesLength = count;
ConvertINetString(&dwMode, codePage_, CP_UTF8, bytes.ptr + index, &bytesLength, null, &charsLength);
return charsLength;
}
override int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex) {
if (IsConvertINetStringAvailable(CP_UTF8, codePage_) == S_FALSE)
throw new ArgumentException("Could not encode.");
uint dwMode;
int charsLength = charCount;
int bytesLength = to!int(bytes.length - byteIndex);
ConvertINetString(&dwMode, CP_UTF8, codePage_, cast(ubyte*)(chars.ptr + charIndex), &charsLength, bytes.ptr + byteIndex, &bytesLength);
return bytesLength;
}
override int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
if (IsConvertINetStringAvailable(codePage_, CP_UTF8) == S_FALSE)
throw new ArgumentException("Could not decode.");
uint dwMode;
int bytesLength = byteCount;
int charsLength = to!int(chars.length - charIndex);
ConvertINetString(&dwMode, codePage_, CP_UTF8, bytes.ptr + byteIndex, &bytesLength, cast(ubyte*)chars.ptr + charIndex, &charsLength);
return charsLength;
}
}
/**
* Represents an ASCII encoding of characters.
*/
class AscIIEncoding : Encoding {
private Encoding baseEncoding_;
this() {
super(CP_ASCII);
baseEncoding_ = new MLangEncoding(CP_ASCII);
}
alias Encoding.encodeLength encodeLength;
override int encodeLength(in char[] chars, int index, int count) {
return baseEncoding_.encodeLength(chars, index, count);
}
alias Encoding.decodeLength decodeLength;
override int decodeLength(in ubyte[] bytes, int index, int count) {
return baseEncoding_.decodeLength(bytes, index, count);
}
alias Encoding.encode encode;
override int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex) {
return baseEncoding_.encode(chars, charIndex, charCount, bytes, byteIndex);
}
alias Encoding.decode decode;
override int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
return baseEncoding_.decode(bytes, byteIndex, byteCount, chars, charIndex);
}
}
/**
* Represents a UTF-7 encoding of characters.
*/
class Utf7Encoding : Encoding {
private Encoding baseEncoding_;
this() {
super(CP_UTF7);
baseEncoding_ = new MLangEncoding(CP_UTF7);
}
alias Encoding.encodeLength encodeLength;
override int encodeLength(in char[] chars, int index, int count) {
return baseEncoding_.encodeLength(chars, index, count);
}
alias Encoding.decodeLength decodeLength;
override int decodeLength(in ubyte[] bytes, int index, int count) {
return baseEncoding_.decodeLength(bytes, index, count);
}
alias Encoding.encode encode;
override int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex) {
return baseEncoding_.encode(chars, charIndex, charCount, bytes, byteIndex);
}
alias Encoding.decode decode;
override int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
return baseEncoding_.decode(bytes, byteIndex, byteCount, chars, charIndex);
}
}
/**
* Represents a UTF-8 encoding of characters.
*/
class Utf8Encoding : Encoding {
private Encoding baseEncoding_;
this() {
super(CP_UTF8);
baseEncoding_ = new MLangEncoding(CP_UTF8);
}
alias Encoding.encodeLength encodeLength;
override int encodeLength(in char[] chars, int index, int count) {
return baseEncoding_.encodeLength(chars, index, count);
}
alias Encoding.decodeLength decodeLength;
override int decodeLength(in ubyte[] bytes, int index, int count) {
return baseEncoding_.decodeLength(bytes, index, count);
}
alias Encoding.encode encode;
override int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex) {
return baseEncoding_.encode(chars, charIndex, charCount, bytes, byteIndex);
}
alias Encoding.decode decode;
override int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
return baseEncoding_.decode(bytes, byteIndex, byteCount, chars, charIndex);
}
}
/**
* Represents a UTF-16 encoding of characters.
*/
class Utf16Encoding : Encoding {
private Encoding baseEncoding_;
this(bool bigEndian = false) {
super(bigEndian ? CP_UTF16BE : CP_UTF16);
baseEncoding_ = new MLangEncoding(codePage_);
}
alias Encoding.encodeLength encodeLength;
override int encodeLength(in char[] chars, int index, int count) {
return baseEncoding_.encodeLength(chars, index, count);
}
alias Encoding.decodeLength decodeLength;
override int decodeLength(in ubyte[] bytes, int index, int count) {
return baseEncoding_.decodeLength(bytes, index, count);
}
alias Encoding.encode encode;
override int encode(in char[] chars, int charIndex, int charCount, ubyte[] bytes, int byteIndex) {
return baseEncoding_.encode(chars, charIndex, charCount, bytes, byteIndex);
}
alias Encoding.decode decode;
override int decode(in ubyte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
return baseEncoding_.decode(bytes, byteIndex, byteCount, chars, charIndex);
}
}