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

Ticket #695: Integer-unicode.patch

File Integer-unicode.patch, 2.4 kB (added by Deewiant, 16 years ago)

patch for Integer

  • Integer.d

    old new  
    2424module tango.text.convert.Integer; 
    2525 
    2626private import tango.core.Exception; 
     27private import tango.text.convert.Utf : toString, toString16; 
    2728 
    2829/****************************************************************************** 
    2930 
     
    3940        Hex = 'x',                      /// lowercase hexadecimal 
    4041        HexUpper = 'X',                 /// uppercase hexadecimal 
    4142        Unsigned = 'u',                 /// unsigned integer 
     43        Unicode = 'c',                  /// Unicode code point 
    4244} 
    4345 
    4446/****************************************************************************** 
     
    255257                           prefix = "0X"; 
    256258                       break; 
    257259 
     260                  case 'c': 
     261                       if (i > dchar.max) 
     262                          return error(cast(T[])"{code point outside valid range}"); 
     263 
     264                       auto codepoint = cast(dchar)i; 
     265                       dchar[1] str; 
     266                       str[0] = codepoint; 
     267 
     268                       static if (is(T == dchar)) { 
     269                          dst[0] = codepoint; 
     270                          return dst[0..1]; 
     271                       } else { 
     272                          uint eaten = 0; 
     273                          T[] result = void; 
     274 
     275                          static if (is(T == wchar)) 
     276                             result = toString16(str, dst, &eaten); 
     277                          else static if (is(T == char)) 
     278                             result = toString(str, dst, &eaten); 
     279                          else 
     280                             static assert (false, "won't write Unicode to unknown buffer type"); 
     281 
     282                          if (eaten) 
     283                             return result; 
     284                          else 
     285                             goto noroom; 
     286                       } 
     287 
    258288                  default: 
    259289                        return error (cast(T[])"{unknown format '"~cast(T)fmt~"'}"); 
    260290                  } 
     
    292322           // write optional prefix string ... 
    293323           dst [len .. len + prefix.length] = prefix[]; 
    294324           } 
    295         else 
     325        else noroom: 
    296326           return error ("{output width too small}"); 
    297327 
    298328        // return slice of provided output buffer