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

Changeset 2526

Show
Ignore:
Timestamp:
08/30/07 04:28:26 (1 year ago)
Author:
kris
Message:

After extended discussions, Integer.parse() and Integer.trim() have been adjusted such that an optional radix-prefix in the text must match an explicit radix argument (where both are provided). Set the numeric radix argument to zero in order to retain prior behavior (which it defaults to).

closes #546, and thanks to eao197 for the ticket :)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/text/convert/Float.d

    r2475 r2526  
    251251        int             exp; 
    252252        bool            sign; 
    253         uint            radix = 10
     253        uint            radix
    254254        NumType         value = 0.0; 
    255255 
     
    300300              { 
    301301              uint eaten; 
    302               exp += Integer.parse (src[(++p-src.ptr) .. $], 10u, &eaten); 
     302              exp += Integer.parse (src[(++p-src.ptr) .. $], 0, &eaten); 
    303303              p += eaten; 
    304304              } 
  • trunk/tango/text/convert/Integer.d

    r2525 r2526  
    6161 
    6262        Parse an integer value from the provided 'digits' string.  
    63         The string is inspected for a sign and radix, where the 
    64         latter will override the default radix provided. 
     63 
     64        The string is inspected for a sign and an optional radix  
     65        prefix. A radix may be provided as an argument instead,  
     66        whereupon it must match the prefix (where present). When 
     67        radix is set to zero, conversion will default to decimal. 
    6568 
    6669        Throws an exception where the input text is not parsable 
     
    6972******************************************************************************/ 
    7073 
    71 int toInt(T, U=uint) (T[] digits, U radix=10) 
     74int toInt(T, U=uint) (T[] digits, U radix=0) 
    7275{return toInt!(T)(digits, radix);} 
    7376 
    74 int toInt(T) (T[] digits, uint radix=10) 
     77int toInt(T) (T[] digits, uint radix=0) 
    7578{ 
    7679        auto x = toLong (digits, radix); 
    7780        if (x > int.max) 
    78             throw new IllegalArgumentException ("Integer.toInt :: numeric overflow"); 
     81            throw new IllegalArgumentException ("Integer.toInt :: integer overflow on: "~digits); 
    7982        return cast(int) x; 
    8083} 
     
    8285/****************************************************************************** 
    8386 
    84         Parse an integer value from the provided 'digits' string.  
    85         The string is inspected for a sign and radix, where the 
    86         latter will override the default radix provided. 
     87        Parse an integer value from the provided 'digits' string.        
     88         
     89        The string is inspected for a sign and an optional radix  
     90        prefix. A radix may be provided as an argument instead,  
     91        whereupon it must match the prefix (where present). When 
     92        radix is set to zero, conversion will default to decimal. 
    8793 
    8894        Throws an exception where the input text is not parsable 
     
    9197******************************************************************************/ 
    9298 
    93 long toLong(T, U=uint) (T[] digits, U radix=10) 
     99long toLong(T, U=uint) (T[] digits, U radix=0) 
    94100{return toLong!(T)(digits, radix);} 
    95101 
    96 long toLong(T) (T[] digits, uint radix=10) 
     102long toLong(T) (T[] digits, uint radix=0) 
    97103{ 
    98104        uint len; 
     
    100106        auto x = parse (digits, radix, &len); 
    101107        if (len < digits.length) 
    102             throw new IllegalArgumentException ("Integer.toLong :: invalid number"); 
     108            throw new IllegalArgumentException ("Integer.toLong :: invalid literal: "~digits); 
    103109        return x; 
    104110} 
     
    288294 
    289295        Parse an integer value from the provided 'digits' string.  
    290         The string is inspected for a sign and radix, where the 
    291         latter will override the default radix provided. 
     296 
     297        The string is inspected for a sign and an optional radix  
     298        prefix. A radix may be provided as an argument instead,  
     299        whereupon it must match the prefix (where present). When 
     300        radix is set to zero, conversion will default to decimal. 
    292301 
    293302        A non-null 'ate' will return the number of characters used 
     
    296305******************************************************************************/ 
    297306 
    298 long parse(T, U=uint) (T[] digits, U radix=10, uint* ate=null) 
     307long parse(T, U=uint) (T[] digits, U radix=0, uint* ate=null) 
    299308{return parse!(T)(digits, radix, ate);} 
    300309 
    301 long parse(T) (T[] digits, uint radix=10, uint* ate=null) 
     310long parse(T) (T[] digits, uint radix=0, uint* ate=null) 
    302311{ 
    303312        bool sign; 
     
    315324 
    316325        Convert the provided 'digits' into an integer value, 
    317         without looking for a sign or radix. 
    318  
    319         Returns the value and updates 'ate' with the number 
    320         of characters parsed. 
     326        without checking for a sign or radix. The radix defaults 
     327        to decimal (10). 
     328 
     329        Returns the value and updates 'ate' with the number of 
     330        characters consumed. 
    321331 
    322332******************************************************************************/ 
     
    362372 
    363373        Strip leading whitespace, extract an optional +/- sign, 
    364         and an optional radix prefix. This can be used as a 
    365         precursor to the conversion of digits into a number. 
    366  
    367         Returns the number of matching characters. 
     374        and an optional radix prefix. If the radix value matches 
     375        an optional prefix, or the radix is zero, the prefix will 
     376        be consumed and assigned. Where the radix is non zero and 
     377        does not match an explicit prefix, the latter will remain  
     378        unconsumed. Otherwise, radix will default to 10. 
     379 
     380        Returns the number of characters consumed. 
    368381 
    369382******************************************************************************/ 
     
    394407 
    395408           // strip off a radix specifier also? 
     409           auto r = radix; 
    396410           if (c is '0' && len > 1) 
    397411               switch (*++p) 
     
    399413                      case 'x': 
    400414                      case 'X': 
    401                            ++p; 
    402                            radix = 16; 
     415                           r = 16, ++p; 
    403416                           break; 
    404  
     417  
    405418                      case 'b': 
    406419                      case 'B': 
    407                            ++p; 
    408                            radix = 2; 
     420                           r = 2, ++p; 
    409421                           break; 
    410  
     422  
    411423                      case 'o': 
    412424                      case 'O': 
    413                            ++p; 
    414                            radix = 8; 
     425                           r = 8, ++p; 
    415426                           break; 
    416  
    417                       default: 
    418                            --p; 
     427  
     428                      default:  
    419429                           break; 
    420430                      }  
     431 
     432           // default the radix to 10 
     433           if (r is 0) 
     434               radix = 10; 
     435           else 
     436              // explicit radix must match (optional) prefix 
     437              if (radix != r) 
     438                  if (radix) 
     439                      --p; 
     440                  else 
     441                     radix = r; 
    421442           } 
    422443 
     
    481502        { 
    482503        char[64] tmp; 
    483  
     504         
     505        assert (toInt("1") is 1); 
     506        assert (toLong("1") is 1); 
    484507        assert (toInt("1", 10) is 1); 
    485         assert (toLong("1", 10U) is 1); 
     508        assert (toLong("1", 10) is 1); 
    486509 
    487510        assert (atoi ("12345") is 12345); 
     
    535558        assert(parse( "0B10000") == 0x10 ); 
    536559 
    537         // regression tests 
    538  
    539         // ticket #90 
     560        // prefix tests 
    540561        char[] str = "0x"; 
    541562        assert(parse( str[0..1] ) ==  0 ); 
    542  
     563        assert(parse("0x10", 10) == 0); 
     564        assert(parse("0b10", 10) == 0); 
     565        assert(parse("0o10", 10) == 0); 
     566        assert(parse("0b10") == 0b10); 
     567        assert(parse("0o10") == 010); 
     568        assert(parse("0b10", 2) == 0b10); 
     569        assert(parse("0o10", 8) == 010); 
     570 
     571        // format tests 
    543572        assert (format (tmp, 12345L) == "12345"); 
    544573        assert (format (tmp, 0) == "0"); 
     
    560589} 
    561590 
    562  
    563 debug (Integer) 
    564 { 
    565         void main()  
    566         { 
    567         } 
    568 } 
    569