Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Copy of Unicode.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file Unicode.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version; November 2005
00034 
00035         @author         Kris
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         // see http://icu.sourceforge.net/docs/papers/forms_of_unicode/#t2
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         Convert from an external coding of 'type' to an internally normalized
00079         representation of T.
00080 
00081         T refers to the destination, whereas 'type' refers to the source.
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 //alias Into!(wchar) IntoUtf16;
00154 //alias Into!(dchar) IntoUtf32;
00155 
00156 
00157 /*******************************************************************************
00158 
00159         Convert to an external coding of 'type' from an internally normalized
00160         representation of T.
00161 
00162         T refers to the source, whereas 'type' refers to the destination.
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 //alias From!(char)  FromUtf8;
00233 //alias From!(wchar) FromUtf16;
00234 //alias From!(dchar) FromUtf32;
00235 
00236 
00237 

Generated on Sat Dec 24 17:28:32 2005 for Mango by  doxygen 1.4.0