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

Changes between Version 1 and Version 2 of TutCSharpFormatterComments

Show
Ignore:
Author:
kris (IP: 68.122.70.98)
Timestamp:
03/16/07 18:33:02 (17 years ago)
Comment:

updated to beta2

Legend:

Unmodified
Added
Removed
Modified
  • TutCSharpFormatterComments

    v1 v2  
    33The [wiki:TutCSharpFormatter existing CSharp Formatter tutorial] doesn't make it obvious that string formatting is available in any other context than the console. But it is!! Anywhere you use strings, you can also create formatted strings!! 
    44 
    5 The interesting code is in the tango.text.convert.Format module, in a templated class called Format(T). You can instantiate the template with char, dchar, or wchar types, and use the !OpCall syntax to format your strings, like this: 
    6  
     5The interesting code is in the tango.text.convert.Layout module, in a templated class called Layout(T). You can instantiate the template with char, dchar, or wchar types, and use the !OpCall syntax to format your strings, like this: 
    76{{{ 
    8 import tango.text.convert.Format; 
     7import tango.text.convert.Layout; 
    98 
    109// ... 
    1110 
    12 auto charFormatter = new Format!(char)(); 
    13 char[] formattedString = charFormatter("This is the thing: '{0}'", thing); 
     11auto charFormatter = new Layout!(char); 
     12char[] formattedString = charFormatter("This is the thing: '{}'", thing); 
    1413 
    15 auto wcharFormatter = new Format!(wchar)(); 
    16 wchar[] formattedWString = wcharFormatter ("This is the thing: '{0}'", thing); 
     14auto wcharFormatter = new Layout!(wchar); 
     15wchar[] formattedWString = wcharFormatter ("This is the thing: '{}'", thing); 
    1716 
    18 auto dcharFormatter = new Format!(dchar)(); 
    19 dchar[] formattedDString = dcharFormatter ("This is the thing: '{0}'", thing); 
     17auto dcharFormatter = new Layout!(dchar); 
     18dchar[] formattedDString = dcharFormatter ("This is the thing: '{}'", thing); 
    2019}}} 
     20 
     21You might consider the sprint() method also, which avoids heap activity. Also, if you're already using Stdout or Stderr, it can be simpler to apply the Layout instance exposed there instead:  
     22{{{ 
     23char[256] out = void; 
     24auto content = Stdout.layout.sprint (out, "This is the thing: '{}'", thing); 
     25}}} 
     26 
     27One might also utilize the locale enabled extensions in a similar manner, by using tango.text.locale.Locale instead of Layout. Locale is a derivative of Layout, so all formatting methods are common with Locale adding support for more sophisticated formatting options along with culture-specific currency, time and date consideration: 
     28{{{ 
     29import tango.text.locale.Locale; 
     30 
     31auto layout = new Locale (Culture.getCulture ("fr-FR")); 
     32auto formattedString = layout ("This is the date: {}", DateTime.now); 
     33 
     34}}} 
     35 
     36It's also possible to make Stdout and Stderr locale-aware, by replacing the (shared) layout instance: 
     37{{{ 
     38Stdout.layout = new Locale (Culture.getCulture ("fr-FR")); 
     39}}} 
     40