Changeset 500
- Timestamp:
- 11/18/07 23:54:01 (1 year ago)
- Files:
-
- candidate/phobos/std/conv.d (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
candidate/phobos/std/conv.d
r433 r500 58 58 } 59 59 60 private void conv_error(string s) 61 { 62 throw new ConvError(s); 60 private 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)); 63 65 } 64 66 … … 349 351 private T parseString(T)(const(char)[] v) 350 352 { 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 } 352 360 return parse!(T)(v); 353 361 } … … 668 676 auto v = parseIntegral!(S, N1)(s); 669 677 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 } 671 682 return result; 672 683 } … … 725 736 conv_overflow(to!(string)(s)); 726 737 Lerr: 727 conv_error (to!(string)(s));738 conv_error!(S, N)(s); 728 739 return 0; 729 740 } … … 737 748 int toInt(string s) 738 749 { 739 scope(exit) { if (s.length) conv_error (s); }750 scope(exit) { if (s.length) conv_error!(string, int)(s); } 740 751 return parseIntegral!(string, int)(s); 741 752 } … … 812 823 uint toUint(string s) 813 824 { 814 scope(exit) if (s.length) conv_error (s);825 scope(exit) if (s.length) conv_error!(string, uint)(s); 815 826 return parseIntegral!(string, uint)(s); 816 827 } … … 881 892 long toLong(string s) 882 893 { 883 scope(exit) if (s.length) conv_error (s);894 scope(exit) if (s.length) conv_error!(string, long)(s); 884 895 return parseIntegral!(string, long)(s); 885 896 } … … 962 973 ulong toUlong(string s) 963 974 { 964 scope(exit) if (s.length) conv_error (s);975 scope(exit) if (s.length) conv_error!(string, ulong)(s); 965 976 return parseIntegral!(string, ulong)(s); 966 977 } … … 1038 1049 short toShort(string s) 1039 1050 { 1040 scope(exit) if (s.length) conv_error (s);1051 scope(exit) if (s.length) conv_error!(string, short)(s); 1041 1052 return parseIntegral!(string, short)(s); 1042 1053 } … … 1113 1124 ushort toUshort(string s) 1114 1125 { 1115 scope(exit) if (s.length) conv_error (s);1126 scope(exit) if (s.length) conv_error!(string, ushort)(s); 1116 1127 return parseIntegral!(string, ushort)(s); 1117 1128 } … … 1183 1194 byte toByte(string s) 1184 1195 { 1185 scope(exit) if (s.length) conv_error (s);1196 scope(exit) if (s.length) conv_error!(string, byte)(s); 1186 1197 return parseIntegral!(string, byte)(s); 1187 1198 } … … 1258 1269 ubyte toUbyte(string s) 1259 1270 { 1260 scope(exit) if (s.length) conv_error (s);1271 scope(exit) if (s.length) conv_error!(string, ubyte)(s); 1261 1272 return parseIntegral!(string, ubyte)(s); 1262 1273 } … … 1328 1339 float toFloat(Char)(Char[] s) 1329 1340 { 1330 scope(exit) if (s.length) conv_error (s);1341 scope(exit) if (s.length) conv_error!(Char[], float)(s); 1331 1342 return parseFloating!(Char[], float)(s); 1332 1343 } … … 1341 1352 if (std.ctype.isspace(*sz)) 1342 1353 goto Lerr; 1354 1355 // issue 1589 1356 version (Windows) 1357 { 1358 if (icmp(s, "nan") == 0) return F.nan; 1359 } 1343 1360 1344 1361 // BUG: should set __locale_decpoint to "." for DMC … … 1365 1382 return f; 1366 1383 Lerr: 1367 conv_error (to!(string)(s) ~ " not representable as a " ~ F.stringof);1384 conv_error!(S[], F)(s); 1368 1385 assert(0); 1369 1386 } … … 1374 1391 float f; 1375 1392 1393 f = toFloat( "nAn" ); 1394 assert(isnan(f)); 1376 1395 f = toFloat( "123" ); 1377 1396 assert( f == 123f ); … … 1408 1427 double toDouble(Char)(Char[] s) 1409 1428 { 1410 scope(exit) if (s.length) conv_error (s);1429 scope(exit) if (s.length) conv_error!(Char[], double)(s); 1411 1430 return parseFloating!(Char[], double)(s); 1412 1431 } … … 1453 1472 real toReal(Char)(Char[] s) 1454 1473 { 1455 scope(exit) if (s.length) conv_error (s);1474 scope(exit) if (s.length) conv_error!(Char[], real)(s); 1456 1475 return parseFloating!(Char[], real)(s); 1457 1476 } … … 1921 1940 Lerr: 1922 1941 // Display the original string in the error message. 1923 conv_error("getComplexStrings() \"" ~ s ~ "\"" ~ " s1=\""1942 throw new ConvError("getComplexStrings() \"" ~ s ~ "\"" ~ " s1=\"" 1924 1943 ~ s1 ~ "\"" ~ " s2=\"" ~ s2 ~ "\""); 1925 1944 return 0;
