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 struct Into(T)
00085 {
00086
00087
00088
00089
00090 static uint type ()
00091 {
00092 static if (is (T == char))
00093 return Type.Utf8;
00094 static if (is (T == wchar))
00095 return Type.Utf16;
00096 static if (is (T == dchar))
00097 return Type.Utf32;
00098 }
00099
00100
00101
00102
00103
00104 static void[] convert (void[] x, uint type, void[] dst=null, uint* ate=null)
00105 {
00106 void[] ret;
00107
00108 static if (is (T == char))
00109 {
00110 if (type == Type.Utf8)
00111 return x;
00112
00113 if (type == Type.Utf16)
00114 ret = Utf.toUtf8 (cast(wchar[]) x, cast(char[]) dst, ate);
00115 else
00116 if (type == Type.Utf32)
00117 ret = Utf.toUtf8 (cast(dchar[]) x, cast(char[]) dst, ate);
00118 }
00119
00120 static if (is (T == wchar))
00121 {
00122 if (type == Type.Utf16)
00123 return x;
00124
00125 if (type == Type.Utf8)
00126 ret = Utf.toUtf16 (cast(char[]) x, cast(wchar[]) dst, ate);
00127 else
00128 if (type == Type.Utf32)
00129 ret = Utf.toUtf16 (cast(dchar[]) x, cast(wchar[]) dst, ate);
00130 }
00131
00132 static if (is (T == dchar))
00133 {
00134 if (type == Type.Utf32)
00135 return x;
00136
00137 if (type == Type.Utf8)
00138 ret = Utf.toUtf32 (cast(char[]) x, cast(dchar[]) dst, ate);
00139 else
00140 if (type == Type.Utf16)
00141 ret = Utf.toUtf32 (cast(wchar[]) x, cast(dchar[]) dst, ate);
00142 }
00143 if (ate)
00144 *ate *= Type.widths[type];
00145 return ret;
00146 }
00147 }
00148
00149
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 struct From(T)
00165 {
00166
00167
00168
00169
00170 static uint type ()
00171 {
00172 static if (is (T == char))
00173 return Type.Utf8;
00174 static if (is (T == wchar))
00175 return Type.Utf16;
00176 static if (is (T == dchar))
00177 return Type.Utf32;
00178 }
00179
00180
00181
00182
00183
00184 static void[] convert (void[] x, uint type, void[] dst=null, uint* ate=null)
00185 {
00186 void[] ret;
00187
00188 static if (is (T == char))
00189 {
00190 if (type == Type.Utf8)
00191 return x;
00192
00193 if (type == Type.Utf16)
00194 ret = Utf.toUtf16 (cast(char[]) x, cast(wchar[]) dst, ate);
00195 else
00196 if (type == Type.Utf32)
00197 ret = Utf.toUtf32 (cast(char[]) x, cast(dchar[]) dst, ate);
00198 }
00199
00200 static if (is (T == wchar))
00201 {
00202 if (type == Type.Utf16)
00203 return x;
00204
00205 if (type == Type.Utf8)
00206 ret = Utf.toUtf8 (cast(wchar[]) x, cast(char[]) dst, ate);
00207 else
00208 if (type == Type.Utf32)
00209 ret = Utf.toUtf32 (cast(wchar[]) x, cast(dchar[]) dst, ate);
00210 }
00211
00212 static if (is (T == dchar))
00213 {
00214 if (type == Type.Utf32)
00215 return x;
00216
00217 if (type == Type.Utf8)
00218 ret = Utf.toUtf8 (cast(dchar[]) x, cast(char[]) dst, ate);
00219 else
00220 if (type == Type.Utf16)
00221 ret = Utf.toUtf16 (cast(dchar[]) x, cast(wchar[]) dst, ate);
00222 }
00223
00224 static if (is (T == wchar))
00225 {
00226 if (ate)
00227 *ate *= 2;
00228 }
00229 static if (is (T == dchar))
00230 {
00231 if (ate)
00232 *ate *= 4;
00233 }
00234 return ret;
00235 }
00236 }
00237
00238
00239
00240
00241 }