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.Number; 00042 00043 /****************************************************************************** 00044 00045 A set of functions for converting between string and longint 00046 values. Additional methods are inherited via the super-class 00047 Number. 00048 00049 While these functions are all static, they are encapsulated within 00050 a class inheritance to preserve some namespace cohesion. One might 00051 use structs for encapsualtion instead, but then inheritance would 00052 be lost. Note that the root class, Styled, is abstract to prevent 00053 accidental instantiation of these classes. 00054 00055 ******************************************************************************/ 00056 00057 class Long : Number 00058 { 00059 alias Number.format format; 00060 00061 /********************************************************************** 00062 00063 Format a longint based upon the Style provided. This 00064 is used by the Format class to convert integer values. 00065 See classes Styled & Format for more information. 00066 00067 **********************************************************************/ 00068 00069 final static int format (long i, inout Style style) 00070 { 00071 return style.emit (format (style.workspace[0..style.width], i, cast(NFormat) style.type, style.flags)); 00072 } 00073 00074 00075 /********************************************************************** 00076 00077 Convert a longint using the provided NFormat and flags. 00078 00079 The traditional printf() conversion specifiers are adhered 00080 to, and the following types are supported: 00081 00082 u - unsigned decimal 00083 d - signed decimal 00084 o - octal 00085 x - lowercase hexadecimal 00086 X - uppercase hexadecimal 00087 b - binary 00088 00089 Modifiers supported include: 00090 00091 # : prefix the conversion with a type identifier 00092 + : prefix positive decimals with a '+' 00093 space : prefix positive decimals with one space 00094 0 : left-pad the number with zeros 00095 00096 These modifiers are specifed via the 'flags' provided, 00097 and are represented via these identifiers: 00098 00099 # : Flags.Hash 00100 + : Flags.Plus 00101 space : Flags.Space 00102 0 : Flags.Zero 00103 00104 **********************************************************************/ 00105 00106 final static char[] format (long i, NFormat fmt = NFormat.Decimal, Flags flags = 0) 00107 { 00108 return format (new char[64], i, fmt, flags); 00109 } 00110 00111 00112 /********************************************************************** 00113 00114 Parse a longint value from the provided 'src' string. 00115 The 'radix' used defaults to 10, but can specified as 00116 desired. In particular, one should specify a radix of 00117 zero to parse that also (via a type specifier). 00118 00119 **********************************************************************/ 00120 00121 final static ulong parse (char[] src, uint radix=10, uint* ate=null) 00122 { 00123 bool sign; 00124 ulong value; 00125 uint count = trim (src, sign, &radix); 00126 00127 foreach (char c; src[count..length]) 00128 { 00129 if (c >= '0' && c <= '9') 00130 {} 00131 else 00132 if (c >= 'a' && c <= 'f') 00133 c -= 39; 00134 else 00135 if (c >= 'A' && c <= 'F') 00136 c -= 7; 00137 else 00138 break; 00139 00140 value = value * radix + (c - '0'); 00141 ++count; 00142 } 00143 00144 if (ate) 00145 *ate = count; 00146 return sign ? -value : value; 00147 } 00148 } 00149 00150