Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #2106: toulong.patch

File toulong.patch, 2.2 kB (added by llucax, 4 months ago)

Patch

  • a/tango/text/convert/Integer.d

    old new  
    8383 
    8484/****************************************************************************** 
    8585 
     86        Parse an unsignedinteger value from the provided 'digits' string. 
     87 
     88        The string is inspected for an optional radix prefix. A 
     89        radix may be provided as an argument instead, whereupon 
     90        it must match the prefix (where present). When radix is 
     91        set to zero, conversion will default to decimal. 
     92 
     93        Throws: IllegalArgumentException where the input text is not parsable 
     94        in its entirety. 
     95 
     96        See_also: the low level functions parse() and convert() 
     97 
     98******************************************************************************/ 
     99 
     100ulong toUlong(T, U=uint) (T[] digits, U radix=0) 
     101{return toLong!(T)(digits, radix);} 
     102 
     103ulong toUlong(T) (T[] digits, uint radix=0) 
     104{ 
     105        bool sign = false; 
     106 
     107        auto eaten = trim (digits, sign, radix); 
     108        if (sign) 
     109            throw new IllegalArgumentException ("Integer.toUlong :: invalid literal"); 
     110 
     111        uint len = 0; 
     112        auto value = convert (digits[eaten..$], radix, &len); 
     113        if (len == 0 || eaten + len < digits.length) 
     114            throw new IllegalArgumentException ("Integer.toUlong :: invalid literal"); 
     115 
     116        return value; 
     117} 
     118 
     119/****************************************************************************** 
     120 
    86121        Wrapper to make life simpler. Returns a text version 
    87122        of the provided value. 
    88123 
     
    607642        assert (toLong("1") is 1); 
    608643        assert (toInt("1", 10) is 1); 
    609644        assert (toLong("1", 10) is 1); 
     645        assert (toUlong("1", 10) is 1); 
     646        assert (toUlong("18446744073709551615") is ulong.max); 
    610647 
    611648        assert (atoi ("12345") is 12345); 
    612649        assert (itoa (tmp, 12345) == "12345");