Make format() public

Make format() public. Deprecate to!String.
This commit is contained in:
Eugen Wissner 2018-08-01 16:58:23 +02:00
parent fe0576a2d6
commit 900a7172bf
2 changed files with 38 additions and 26 deletions

View File

@ -847,35 +847,13 @@ if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
assert(false.to!int == 0); assert(false.to!int == 0);
} }
/** deprecated("Use tanya.format.format instead")
* Converts $(D_PARAM From) to a $(D_PSYMBOL String).
*
* Params:
* From = Source type.
* To = Target type.
* from = Source value.
*
* Returns: $(D_PARAM from) converted to $(D_PSYMBOL String).
*/
To to(To, From)(auto ref From from) To to(To, From)(auto ref From from)
if (is(Unqual!To == String)) if (is(Unqual!To == String))
{ {
return format!"{}"(from); return format!"{}"(from);
} }
///
@nogc nothrow pure @safe unittest
{
assert(true.to!String == "true");
assert(false.to!String == "false");
}
@nogc nothrow pure @safe unittest
{
static assert(is(typeof((const String("true")).to!bool)));
static assert(is(typeof(false.to!(const String) == "false")));
}
/** /**
* Converts a stringish range to an integral value. * Converts a stringish range to an integral value.
* *

View File

@ -3,8 +3,32 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** /**
* This package doesn't yet contain public symbols. Refer to * This module provides $(D_PSYMBOL format) function that can convert different
* $(D_PSYMBOL tanya.conv) for basic formatting and conversion functionality. * data types to a $(D_PSYMBOL String) according to a specified format.
*
* Format string is a $(D_PSYMBOL string) which can contain placeholders for
* arguments. Placeholder marker is `{}`, i.e. all occurrences of `{}` are
* replaced by the arguments passed to $(D_PSYMBOL format). An argument will be
* first converted to a string, then inserted into the resulting string instead
* of the corresponding placeholder. The number of the placeholders and
* arguments must match. The placeholders are replaced with the arguments in
* the order arguments are passed to $(D_PSYMBOL format).
*
* To escape `{` or `}`, use `{{` and `}}` respectively. `{{` will be outputted
* as a single `{`, `}}` - as a single `}`.
*
* If a custom data type (like $(D_KEYWORD struct) or $(D_KEYWORD class))
* defines a `stringify()` function that is callable without arguments and
* returns a $(D_PSYMBOL String), this function is used to produce a string
* representation for the value. String conversions for the most built-in
* data types a also available.
*
* $(D_KEYWORD char), $(D_KEYWORD wchar) and $(D_KEYWORD dchar) ranges are
* outputted as plain strings (without any delimiters between their elements).
*
* All floating point numbers are handled as $(D_KEYWORD double)s.
*
* More advanced formatting is currently not implemented.
* *
* Copyright: Eugene Wissner 2017-2018. * Copyright: Eugene Wissner 2017-2018.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
@ -2314,7 +2338,17 @@ private ref String printToString(string fmt, Args...)(return ref String result,
return result; return result;
} }
package(tanya) String format(string fmt, Args...)(auto ref Args args) /**
* Produces a string according to the specified format.
*
* Params:
* fmt = Format.
* Args = Types of the arguments.
* args = Arguments.
*
* Returns: Formatted string.
*/
String format(string fmt, Args...)(auto ref Args args)
{ {
String formatted; String formatted;