Changeset 89
- Timestamp:
- 12/20/05 03:33:53 (3 years ago)
- Files:
-
- trunk/meta/conv.d (modified) (5 diffs)
- trunk/meta/strhacks.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/meta/conv.d
r78 r89 7 7 8 8 module meta.conv; 9 import meta.math; 10 import meta.string; 9 private import meta.math; 10 private import meta.string; 11 private import meta.ctype; 11 12 12 13 template decimaldigit(int n) { const char [] decimaldigit = "0123456789"[n..n+1]; } 13 14 template hexdigit(int n) { const char [] hexdigit = "0123456789ABCDEF"[n..n+1]; } 14 15 16 /******************************************************* 17 * char [] itoa!(long n); 18 */ 15 19 template itoa(long n) 16 20 { … … 26 30 } 27 31 32 /******************************************************* 33 * long atoi!(char [] s); 34 */ 35 template atoi(char [] s, long sofar=0, int indx=0) 36 { 37 static if (s.length==indx) const long atoi=sofar; 38 else static if (indx==0 && s[indx]=='-') const long atoi = -.atoi!(s, 0, 1); 39 else static if (!isdigit!( getcharat!(s, indx) ) ) const long atoi = sofar; 40 else const long atoi = .atoi!(s, sofar * 10 + (getcharat!(s, indx)-'0'), indx+1); 41 } 42 43 /******************************************************* 44 * the number of digits which would be 'consumed' by atoi!(). 45 */ 46 template countleadingdigits(char [] s, int indx=0) 47 { 48 static if (s.length==indx) const int countleadingdigits=indx; 49 else static if (indx==0 && s[indx]=='-') const int countleadingdigits = .countleadingdigits!(s, indx+1); 50 else static if (!isdigit!(getcharat!(s, indx))) const int countleadingdigits=indx; 51 else const int countleadingdigits = .countleadingdigits!(s, indx+1); 52 } 53 54 28 55 // Given a number x, where 0<= x <1, 29 56 // returns the first 'maxdigs' digits after the decimal point. … … 34 61 } 35 62 36 /** char [] fcvt!(real x) 37 * 63 /******************************************************* 64 * char [] fcvt!(real x) 65 * Convert a real number x to %f format 38 66 */ 39 67 template fcvt(real x) … … 50 78 } 51 79 52 /** char [] pcvt!(real x) 80 /******************************************************* 81 * char [] pcvt!(real x) 53 82 * Convert a real number x to %a format, eg 0x1.ABCDp+30 54 83 */ … … 67 96 static assert( streq!(pcvt!(0x1.12345p954L), "0x1.123450p+954") ); 68 97 static assert( streq!(fcvt!(12.345), "12.345") ); 98 static assert( atoi!("3580wip")==3580); 99 static assert( atoi!("-0326")==-326); 100 static assert( countleadingdigits!("325827wip")==6); 101 static assert( countleadingdigits!("abc")==0); 69 102 } trunk/meta/strhacks.d
r83 r89 1 1 /** Workarounds required for DMD 0.141 2 2 * 3 * Most of these are only required when they are used as a template value parameter. 3 4 */ 4 5 module meta.strhacks; … … 13 14 { 14 15 const char [] slice = str[from..to]; 16 } 17 18 // = str[n] 19 template getcharat(char [] str, int n) 20 { 21 const char getcharat = str[n]; 15 22 } 16 23
