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

Copy (3) 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                 Convert from an external coding of 'type' to an internally
00077                 normalized representation of T.
00078 
00079                 T refers to the destination, whereas 'type' refers to the 
00080                 source.
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         //alias Into!(wchar) IntoUtf16;
00152         //alias Into!(dchar) IntoUtf32;
00153 
00154 
00155         /***********************************************************************
00156 
00157                 Convert to an external coding of 'type' from an internally 
00158                 normalized representation of T.
00159 
00160                 T refers to the source, whereas 'type' is the destination.
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         //alias From!(char)  FromUtf8;
00239         //alias From!(wchar) FromUtf16;
00240         //alias From!(dchar) FromUtf32;
00241 }

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