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

Int.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file Int.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, Feb 2005
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.format.Int;
00040 
00041 private import mango.format.Number;
00042 
00043 /******************************************************************************
00044 
00045         A set of functions for converting between string and numeric 
00046         values. 
00047 
00048         Used by modules Token and DisplayWriter
00049 
00050 ******************************************************************************/
00051 
00052 class Int : Number
00053 {
00054 /+
00055         alias put opCall;
00056 
00057         private tChar   fill;
00058         private tChar   space[32];
00059         private int     width = space.length;
00060         private Radix   radix = Radix.Decimal;
00061 
00062         /**************************************************************
00063 
00064         **************************************************************/
00065 
00066         void setRadix (Radix radix)
00067         {
00068                 this.radix = radix;    
00069         }
00070 
00071 
00072         /**************************************************************
00073 
00074         **************************************************************/
00075 
00076         void setFill (tChar fill, uint width)
00077         {
00078                 this.fill = fill;    
00079                 this.width = width;
00080         }
00081 
00082 
00083         /**************************************************************
00084 
00085         **************************************************************/
00086 
00087         tChar[] format (uint x)
00088         {
00089                 return format (space[0..width], x, radix, fill);
00090         }
00091 +/
00092 
00093         /**************************************************************
00094 
00095         **************************************************************/
00096 
00097         final static tChar[] format (int i)
00098         {
00099                 return format (new tChar[16], i);
00100         }
00101 
00102 
00103         /**************************************************************
00104 
00105         **************************************************************/
00106 
00107         final static tChar[] format (uint i, Radix radix, tChar fill = tChar.init)
00108         {
00109                 return format (new tChar[32], i, radix, fill);
00110         }
00111 
00112 
00113         /**************************************************************
00114 
00115         **************************************************************/
00116 
00117         final static tChar[] format (tChar[] dst, int i)
00118         in {
00119            assert (dst.length);
00120            }
00121         body
00122         {
00123                 if (i >= 0)
00124                     return format (dst, i, Radix.Decimal);
00125 
00126                 uint len = dst.length - format (dst[1..length], -i, Radix.Decimal).length;
00127                 dst [--len] = '-';
00128                 return dst [len..length];
00129         }
00130 
00131 
00132         /***********************************************************************
00133         
00134         ***********************************************************************/
00135 
00136         final static tChar[] format (tChar[] dst, uint i, Radix radix, tChar fill = tChar.init)
00137         {
00138                 uint len = dst.length;
00139                 char* p = dst.ptr + len;
00140 
00141                 if (len)
00142                     switch (radix)
00143                            {
00144                            case Radix.Binary:
00145                                 do {
00146                                    *--p = '0' + (i & 1);
00147                                    } while ((i >>>= 1) && --len);
00148                                 break;
00149 
00150                            case Radix.Octal:
00151                                 do {
00152                                    *--p = '0' + (i & 7);
00153                                    } while ((i >>>= 3) && --len);
00154                                 break;
00155 
00156                            case Radix.Hexadecimal:
00157                                  do {
00158                                     char c = i & 0x0f;
00159                                     *--p = c + ((c < 10) ? '0' : ('a' - 10));
00160                                     } while ((i >>= 4) && --len);
00161                                 break;
00162 
00163                            default:
00164                                 do {
00165                                    *--p = '0' + (i % 10);
00166                                    } while ((i /= 10) && --len);
00167                                 break;
00168                            }
00169 
00170                 if (! len)
00171                       throw error;
00172 
00173                 if (fill != tChar.init)
00174                     while (p > dst.ptr)
00175                            *--p = fill;
00176 
00177                 return dst [p-dst.ptr..length];                               
00178         }
00179 
00180 
00181         /**************************************************************
00182 
00183         **************************************************************/
00184 
00185         final static uint parse (tChar[] src, Radix radix=Radix.Decimal, uint* ate=null)
00186         {
00187                 bool  sign;
00188                 uint  count = trim (src, sign, &radix);
00189 
00190                 uint value;
00191                 foreach (tChar c; src [count..length])
00192                         {
00193                         if (c >= '0' || c <= '9')
00194                            {}
00195                         else
00196                            if (c >= 'a' && c <= 'f')
00197                                c -= 39;
00198                            else
00199                               if (c >= 'A' && c <= 'F')
00200                                   c -= 7;
00201                               else
00202                                  break;
00203 
00204                         value = value * radix + (c - '0');
00205                         ++count;
00206                         }
00207 
00208                 if (ate)
00209                     *ate = count;
00210                 return sign ? -value : value;
00211         }
00212 }
00213 

Generated on Sun Mar 6 00:30:57 2005 for Mango by doxygen 1.3.6