Changeset 500

Show
Ignore:
Timestamp:
11/18/07 23:54:01 (1 year ago)
Author:
andrei
Message:

Made conv_error a template parameterized on the types being converted

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • candidate/phobos/std/conv.d

    r433 r500  
    5858} 
    5959 
    60 private void conv_error(string s) 
    61 
    62     throw new ConvError(s); 
     60private void conv_error(S, T)(S source) 
     61
     62    throw new ConvError(cast(string) 
     63                        ("Can't convert value `"~to!(string)(source)~"' of type " 
     64                         ~S.stringof~" to type "~T.stringof)); 
    6365} 
    6466 
     
    349351private T parseString(T)(const(char)[] v) 
    350352{ 
    351     scope(exit) { if (v.length) conv_error(v.idup); } 
     353    scope(exit)  
     354    { 
     355        if (v.length)  
     356        { 
     357            conv_error!(const(char)[], T)(v);  
     358        } 
     359    } 
    352360    return parse!(T)(v); 
    353361} 
     
    668676        auto v = parseIntegral!(S, N1)(s); 
    669677        auto result = cast(N) v; 
    670         if (result != v) conv_error(to!(string)(s)); 
     678        if (result != v)  
     679        { 
     680            conv_error!(S, N)(s);  
     681        } 
    671682        return result; 
    672683    } 
     
    725736        conv_overflow(to!(string)(s)); 
    726737    Lerr: 
    727         conv_error(to!(string)(s)); 
     738        conv_error!(S, N)(s); 
    728739        return 0; 
    729740    } 
     
    737748int toInt(string s) 
    738749{ 
    739     scope(exit) { if (s.length) conv_error(s); } 
     750    scope(exit) { if (s.length) conv_error!(string, int)(s); } 
    740751    return parseIntegral!(string, int)(s); 
    741752} 
     
    812823uint toUint(string s) 
    813824{ 
    814     scope(exit) if (s.length) conv_error(s); 
     825    scope(exit) if (s.length) conv_error!(string, uint)(s); 
    815826    return parseIntegral!(string, uint)(s); 
    816827} 
     
    881892long toLong(string s) 
    882893{ 
    883     scope(exit) if (s.length) conv_error(s); 
     894    scope(exit) if (s.length) conv_error!(string, long)(s); 
    884895    return parseIntegral!(string, long)(s); 
    885896} 
     
    962973ulong toUlong(string s) 
    963974{ 
    964     scope(exit) if (s.length) conv_error(s); 
     975    scope(exit) if (s.length) conv_error!(string, ulong)(s); 
    965976    return parseIntegral!(string, ulong)(s); 
    966977} 
     
    10381049short toShort(string s) 
    10391050{ 
    1040     scope(exit) if (s.length) conv_error(s); 
     1051    scope(exit) if (s.length) conv_error!(string, short)(s); 
    10411052    return parseIntegral!(string, short)(s); 
    10421053} 
     
    11131124ushort toUshort(string s) 
    11141125{ 
    1115     scope(exit) if (s.length) conv_error(s); 
     1126    scope(exit) if (s.length) conv_error!(string, ushort)(s); 
    11161127    return parseIntegral!(string, ushort)(s); 
    11171128} 
     
    11831194byte toByte(string s) 
    11841195{ 
    1185     scope(exit) if (s.length) conv_error(s); 
     1196    scope(exit) if (s.length) conv_error!(string, byte)(s); 
    11861197    return parseIntegral!(string, byte)(s); 
    11871198} 
     
    12581269ubyte toUbyte(string s) 
    12591270{ 
    1260     scope(exit) if (s.length) conv_error(s); 
     1271    scope(exit) if (s.length) conv_error!(string, ubyte)(s); 
    12611272    return parseIntegral!(string, ubyte)(s); 
    12621273} 
     
    13281339float toFloat(Char)(Char[] s) 
    13291340{ 
    1330     scope(exit) if (s.length) conv_error(s); 
     1341    scope(exit) if (s.length) conv_error!(Char[], float)(s); 
    13311342    return parseFloating!(Char[], float)(s); 
    13321343} 
     
    13411352    if (std.ctype.isspace(*sz)) 
    13421353    goto Lerr; 
     1354 
     1355    // issue 1589 
     1356    version (Windows) 
     1357    { 
     1358        if (icmp(s, "nan") == 0) return F.nan; 
     1359    } 
    13431360 
    13441361    // BUG: should set __locale_decpoint to "." for DMC 
     
    13651382    return f; 
    13661383  Lerr: 
    1367     conv_error(to!(string)(s) ~ " not representable as a " ~ F.stringof); 
     1384    conv_error!(S[], F)(s); 
    13681385    assert(0); 
    13691386} 
     
    13741391    float f; 
    13751392     
     1393    f = toFloat( "nAn" ); 
     1394    assert(isnan(f)); 
    13761395    f = toFloat( "123" ); 
    13771396    assert( f == 123f ); 
     
    14081427double toDouble(Char)(Char[] s) 
    14091428{ 
    1410     scope(exit) if (s.length) conv_error(s); 
     1429    scope(exit) if (s.length) conv_error!(Char[], double)(s); 
    14111430    return parseFloating!(Char[], double)(s); 
    14121431} 
     
    14531472real toReal(Char)(Char[] s) 
    14541473{ 
    1455     scope(exit) if (s.length) conv_error(s); 
     1474    scope(exit) if (s.length) conv_error!(Char[], real)(s); 
    14561475    return parseFloating!(Char[], real)(s); 
    14571476} 
     
    19211940    Lerr: 
    19221941        // Display the original string in the error message. 
    1923     conv_error("getComplexStrings() \"" ~ s ~ "\"" ~ " s1=\"" 
     1942    throw new ConvError("getComplexStrings() \"" ~ s ~ "\"" ~ " s1=\"" 
    19241943                             ~ s1 ~ "\"" ~ " s2=\"" ~ s2 ~ "\""); 
    19251944        return 0;