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

Long.d

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

Generated on Sat Apr 9 20:11:27 2005 for Mango by doxygen 1.3.6