std.conv - D Programming Language - Digital Mars

D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Wed Sep 2 00:11:53 2009

# std.conv

A one-stop shop for converting values from one type to another.

Authors:
Walter Bright, Andrei Alexandrescu, Shin Fujishiro

class ConvError: object.Error;
  Thrown on conversion errors.

class ConvOverflowError: std.conv.ConvError;
  Thrown on conversion overflow errors.

T to(T, S)(S s);
  String to string conversion works for any two string types having (char, wchar, dchar) character widths and any combination of qualifiers (mutable, const, or immutable).

  Example:

      char[] a = "abc";
      auto b = to!(immutable(dchar)[])(a);
      assert(b == "abc"w);

T to(T, S)(S s, in T leftBracket = "[", in T separator = ", ", in T rightBracket = "]");
  Converts array (other than strings) to string. The left bracket, separator, and right bracket are configurable. Each element is converted by calling to!T.

T to(T, S)(S s, in T leftBracket = "[", in T keyval = ":", in T separator = ", ", in T rightBracket = "]");
  Associative array to string conversion. The left bracket, key-value separator, element separator, and right bracket are configurable. Each element is printed by calling to!T.

T to(T, S)(S s, in T nullstr = "null");
  Object to string conversion calls toString against the object or returns nullstr if the object is null.

T to(T, S)(S s);
  Struct to string conversion calls toString against the struct if it is defined.

T to(T, S)(S s, in T left = S.stringof ~ "(", in T separator = ", ", in T right = ")");
  For structs that do not define toString, the conversion to string produces the list of fields.

T to(T, S)(S s);
  Enumerated types are converted to strings as their symbolic names.

T to(T, S)(S s, in T left = S.stringof ~ "(", in T right = ")");
  A typedef Type Symbol is printed as Type(value).

Target to(Target, Source)(Source value);
  If the source type is implicitly convertible to the target type, to simply performs the implicit conversion.

T to(T, S)(S b);
  Boolean values are printed as "true" or "false".

T to(T, S)(S value);
  When the source is a wide string, it is first converted to a narrow string and then parsed.

T to(T, S)(S value);
  When the source is a narrow string, normal text parsing occurs.

T to(T, S)(S value);
  Object-to-object conversions throw exception when the source is non-null and the target is null.

T to(T, S)(S value);
  Object-to-non-object conversions look for a method "to" of the source object.

  Example:

      class Date
      {
          T to(T)() if(is(T == long))
          {
              return timestamp;
          }
          ...
      }

      unittest
      {
          auto d = new Date;
          auto ts = to!long(d); // same as d.to!long()
      }

T to(T, S)(S value);
  Narrowing numeric-numeric conversions throw when the value does not fit in the narrower type.

T to(T, S)(S src);
  Array-to-array conversion (except when target is a string type) converts each element in turn by using to.

T to(T : V2[K2], S : V1[K1], K1, V1, K2, V2)(S src);
  Associative array to associative array conversion converts each key and each value in turn.

template roundTo(Target)
  Rounded conversion from floating point to integral.

  Example:

        assert(roundTo!(int)(3.14) == 3);
        assert(roundTo!(int)(3.49) == 3);
        assert(roundTo!(int)(3.5) == 4);
        assert(roundTo!(int)(3.999) == 4);
        assert(roundTo!(int)(-3.14) == -3);
        assert(roundTo!(int)(-3.49) == -3);
        assert(roundTo!(int)(-3.5) == -4);
        assert(roundTo!(int)(-3.999) == -4);

  Rounded conversions do not work with non-integral target types.

Target parse(Target, Source)(ref Source s);
  The parse family of functions works quite like the to family, except that (1) it only works with strings as input, (2) takes the input string by reference and advances it to the position following the conversion, and (3) does not throw if it could not convert the entire string. It still throws if an overflow occurred during conversion or if no character of the input string was meaningfully converted.

  Example:

      string test = "123 \t  76.14";
      auto a = parse!(uint)(test);
      assert(a == 123);
      assert(test == " \t  76.14"); // parse bumps string
      munch(test, " \t\n\r"); // skip ws
      assert(test == "76.14");
      auto b = parse!(double)(test);
      assert(b == 76.14);
      assert(test == "");

T to(T, S)(S value);
  Small unsigned integers to strings.

T to(T, S)(S value);
  Small signed integers to strings.

T to(T, S)(S input);
  Unsigned integers (uint and ulong).

T to(T, S)(S c);
  char, wchar, dchar to a string type.

T to(T, S)(S value);
  Signed values (int and long).

T to(T, S)(S s);
  C-style strings

T to(T, S)(S f);
  float to all string types.

T to(T, S)(S d);
  double to all string types.

T to(T, S)(S r);
  real to all string types.

T to(T, S)(S f);
  ifloat to all string types.

T to(T, S)(S d);
  idouble to all string types.

T to(T, S)(S r);
  ireal to all string types.

T to(T, S)(S f);
  cfloat to all string types.

T to(T, S)(S d);
  cdouble to all string types.

T to(T, S)(S r);
  creal to all string types.

T to(T, S)(S value, uint radix);
T to(T, S)(S value, uint radix);
  Convert value to string in radix radix.

  radix must be a value from 2 to 36. value is treated as a signed value only if radix is 10. The characters A through Z are used to represent values 10 through 36.

T to(T, S)(S value);
  Pointer to string conversions prints the pointer as a size_t value.

string text(T...)(T args);
wstring wtext(T...)(T args);
dstring dtext(T...)(T args);
  Convenience functions for converting any number and types of arguments into text (the three character widths).

  Example:

         assert(text(42, ' ', 1.5, ": xyz") == "42 1.5: xyz");
         assert(wtext(42, ' ', 1.5, ": xyz") == "42 1.5: xyz"w);
         assert(dtext(42, ' ', 1.5, ": xyz") == "42 1.5: xyz"d);

Copyright © 1999-2009 by Digital Mars, All Rights Reserved | Page generated by Ddoc.