Changeset 89

Show
Ignore:
Timestamp:
12/20/05 03:33:53 (3 years ago)
Author:
Don Clugston
Message:

Added meta.conv.atoi(). Also added another string hack.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/meta/conv.d

    r78 r89  
    77   
    88module meta.conv; 
    9 import meta.math; 
    10 import meta.string; 
     9private import meta.math; 
     10private import meta.string; 
     11private import meta.ctype; 
    1112 
    1213template decimaldigit(int n) { const char [] decimaldigit = "0123456789"[n..n+1]; } 
    1314template hexdigit(int n) { const char [] hexdigit = "0123456789ABCDEF"[n..n+1]; } 
    1415 
     16/******************************************************* 
     17 *  char [] itoa!(long n); 
     18 */ 
    1519template itoa(long n) 
    1620{ 
     
    2630} 
    2731 
     32/******************************************************* 
     33 *  long atoi!(char [] s); 
     34 */ 
     35template 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 */ 
     46template 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 
    2855// Given a number x, where 0<= x <1, 
    2956// returns the first 'maxdigs' digits after the decimal point. 
     
    3461} 
    3562 
    36 /**  char [] fcvt!(real x) 
    37  * 
     63/******************************************************* 
     64 *  char [] fcvt!(real x) 
     65 *  Convert a real number x to %f format 
    3866 */ 
    3967template fcvt(real x) 
     
    5078} 
    5179 
    52 /** char [] pcvt!(real x) 
     80/******************************************************* 
     81 * char [] pcvt!(real x) 
    5382 * Convert a real number x to %a format, eg 0x1.ABCDp+30 
    5483 */ 
     
    6796 static assert( streq!(pcvt!(0x1.12345p954L), "0x1.123450p+954") ); 
    6897 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); 
    69102} 
  • trunk/meta/strhacks.d

    r83 r89  
    11/** Workarounds required for DMD 0.141 
    22 * 
     3 * Most of these are only required when they are used as a template value parameter. 
    34 */ 
    45module meta.strhacks; 
     
    1314{ 
    1415  const char [] slice = str[from..to]; 
     16} 
     17 
     18// = str[n] 
     19template getcharat(char [] str, int n) 
     20{ 
     21  const char getcharat = str[n]; 
    1522} 
    1623