00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 module mango.convert.Unicode;
00041
00042 private import mango.convert.Utf;
00043 private import mango.convert.Type;
00044
00045
00046
00047
00048
00049
00050 class Unicode
00051 {
00052
00053 enum {
00054 Unknown,
00055 UTF_8,
00056 UTF_8N,
00057 UTF_16,
00058 UTF_16BE,
00059 UTF_16LE,
00060 UTF_32,
00061 UTF_32BE,
00062 UTF_32LE,
00063 };
00064
00065
00066
00067
00068
00069 static bool isValid (int encoding)
00070 {
00071 return (encoding >= Unknown && encoding <= UTF_32LE);
00072 }
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 class Into(T)
00086 {
00087 private void[] tmp;
00088
00089 this (int size = 0)
00090 {
00091 tmp = new ubyte[size];
00092 }
00093
00094 private void[] update (void[] t)
00095 {
00096 if (t.length > tmp.length)
00097 tmp = t;
00098 return t;
00099 }
00100
00101 uint type ()
00102 {
00103 static if (is (T == char))
00104 return Type.Utf8;
00105 static if (is (T == wchar))
00106 return Type.Utf16;
00107 static if (is (T == dchar))
00108 return Type.Utf32;
00109 }
00110
00111 void[] convert (void[] x, uint type)
00112 {
00113 static if (is (T == char))
00114 {
00115 if (type == Type.Utf8)
00116 return cast(char[]) x;
00117
00118 if (type == Type.Utf16)
00119 return update (Utf.toUtf8 (cast(wchar[]) x, cast(char[]) tmp));
00120
00121 if (type == Type.Utf32)
00122 return update (Utf.toUtf8 (cast(dchar[]) x, cast(char[]) tmp));
00123 }
00124
00125 static if (is (T == wchar))
00126 {
00127 if (type == Type.Utf8)
00128 return update (Utf.toUtf16 (cast(char[]) x, cast(wchar[]) tmp));
00129
00130 if (type == Type.Utf16)
00131 return cast(wchar[]) x;
00132
00133 if (type == Type.Utf32)
00134 return update (Utf.toUtf16 (cast(dchar[]) x, cast(wchar[]) tmp));
00135 }
00136
00137 static if (is (T == dchar))
00138 {
00139 if (type == Type.Utf8)
00140 return update (Utf.toUtf32 (cast(char[]) x, cast(dchar[]) tmp));
00141
00142 if (type == Type.Utf16)
00143 return update (Utf.toUtf32 (cast(wchar[]) x, cast(dchar[]) tmp));
00144
00145 if (type == Type.Utf32)
00146 return cast(dchar[]) x;
00147 }
00148 }
00149 }
00150
00151
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 class From(T)
00167 {
00168 private void[] tmp;
00169
00170 this (int size = 0)
00171 {
00172 tmp = new ubyte[size];
00173 }
00174
00175 private void[] update (void[] t)
00176 {
00177 if (t.length > tmp.length)
00178 tmp = t;
00179 return t;
00180 }
00181
00182 uint type ()
00183 {
00184 static if (is (T == char))
00185 return Type.Utf8;
00186 static if (is (T == wchar))
00187 return Type.Utf16;
00188 static if (is (T == dchar))
00189 return Type.Utf32;
00190 }
00191
00192 void[] convert (void[] x, uint type)
00193 {
00194 static if (is (T == char))
00195 {
00196 if (type == Type.Utf8)
00197 return x;
00198
00199 if (type == Type.Utf16)
00200 return update (Utf.toUtf16 (cast(char[]) x, cast(wchar[]) tmp));
00201
00202 if (type == Type.Utf32)
00203 return update (Utf.toUtf32 (cast(char[]) x, cast(dchar[]) tmp));
00204 }
00205
00206 static if (is (T == wchar))
00207 {
00208 if (type == Type.Utf8)
00209 return update (Utf.toUtf8 (cast(wchar[]) x, cast(char[]) tmp));
00210
00211 if (type == Type.Utf16)
00212 return x;
00213
00214 if (type == Type.Utf32)
00215 return update (Utf.toUtf32 (cast(wchar[]) x, cast(dchar[]) tmp));
00216 }
00217
00218 static if (is (T == dchar))
00219 {
00220 if (type == Type.Utf8)
00221 return update (Utf.toUtf8 (cast(dchar[]) x, cast(char[]) tmp));
00222
00223 if (type == Type.Utf16)
00224 return update (Utf.toUtf16 (cast(dchar[]) x, cast(wchar[]) tmp));
00225
00226 if (type == Type.Utf32)
00227 return x;
00228 }
00229 }
00230 }
00231
00232
00233
00234
00235
00236
00237