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

Copy (2) 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 struct 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         private uint _type = Type.Utf16;
00066 
00067         private void[] tmp;
00068 
00069         void dthis (int size = 0)
00070         {
00071                 tmp = new ubyte[size];
00072         }
00073 
00074         uint type ()
00075         {
00076                 return _type;
00077         }
00078 
00079         private void[] update (void[] t)
00080         {
00081                 if (t.length > tmp.length)
00082                     tmp = t;
00083                 return t;
00084         }
00085 
00086         /***********************************************************************
00087 
00088         ***********************************************************************/
00089 
00090         static bool isValid (int encoding)
00091         {
00092                 return (encoding >= Unknown && encoding <= UTF_32LE);
00093         }
00094 
00095 
00096         /***********************************************************************
00097 
00098         ***********************************************************************/
00099 
00100         final void[] convert (void[] src, uint srcType, uint dstType)
00101         {
00102                 enum : ubyte {char2char, char2wchar, char2dchar, 
00103                               wchar2char, wchar2wchar, wchar2dchar, 
00104                               dchar2char, dchar2wchar, dchar2dchar};
00105 
00106                 const int[][4] router = [
00107                                         [char2char,  char2wchar,  char2dchar, 0], 
00108                                         [wchar2char, wchar2wchar, wchar2dchar, 0], 
00109                                         [dchar2char, dchar2wchar, dchar2dchar, 0], 
00110                                         [0, 0, 0, 0], 
00111                                         ];
00112 
00113 
00114                 srcType -= Type.Utf8;
00115                 dstType -= Type.Utf8;
00116                 assert (srcType < 3);
00117                 assert (dstType < 3);
00118                 
00119                 switch (srcType)
00120                        {
00121                        case char2char: 
00122                             return src;
00123 
00124                        case char2wchar: 
00125                             return Utf.toUtf16 (cast(char[]) src, cast(wchar[]) tmp);
00126 
00127                        case char2dchar: 
00128                             return Utf.toUtf32 (cast(char[]) src, cast(dchar[]) tmp);
00129 
00130 
00131                        case wchar2char: 
00132                             return Utf.toUtf8 (cast(wchar[]) src, cast(char[]) tmp);
00133 
00134                        case wchar2wchar:
00135                             return src; 
00136 
00137                        case wchar2dchar: 
00138                             return Utf.toUtf32 (cast(wchar[]) src, cast(dchar[]) tmp);
00139 
00140 
00141                        case dchar2char: 
00142                             return Utf.toUtf8 (cast(dchar[]) src, cast(char[]) tmp);
00143 
00144                        case dchar2wchar: 
00145                             return Utf.toUtf16 (cast(dchar[]) src, cast(wchar[]) tmp);
00146 
00147                        case dchar2dchar: 
00148                             return src;
00149 
00150                        default:
00151                             return null;
00152                        }
00153         }
00154 }

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