Skip to content

Commit

Permalink
Merge pull request #5648 from japplegame/patch-1
Browse files Browse the repository at this point in the history
formattedWrite should take output range by reference
merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Jul 23, 2017
2 parents d427c3a + d2a3d8a commit e8ef731
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ My friends are "John", "Nancy".
My friends are John, Nancy.
)
*/
uint formattedWrite(alias fmt, Writer, A...)(Writer w, A args)
uint formattedWrite(alias fmt, Writer, A...)(auto ref Writer w, A args)
if (isSomeString!(typeof(fmt)))
{
alias e = checkFormatException!(fmt, A);
Expand All @@ -463,7 +463,7 @@ if (isSomeString!(typeof(fmt)))
}

/// ditto
uint formattedWrite(Writer, Char, A...)(Writer w, in Char[] fmt, A args)
uint formattedWrite(Writer, Char, A...)(auto ref Writer w, in Char[] fmt, A args)
{
import std.conv : text;

Expand Down Expand Up @@ -1163,7 +1163,7 @@ if (is(Unqual!Char == Char))
trailing = fmt;
}

bool writeUpToNextSpec(OutputRange)(OutputRange writer)
bool writeUpToNextSpec(OutputRange)(ref OutputRange writer)
{
if (trailing.empty)
return false;
Expand Down Expand Up @@ -1724,7 +1724,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(BooleanTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
BooleanTypeOf!T val = obj;
Expand Down Expand Up @@ -1806,7 +1806,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(Unqual!T == typeof(null)) && !is(T == enum) && !hasToString!(T, Char))
{
enforceFmt(f.spec == 's',
Expand Down Expand Up @@ -1844,7 +1844,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(IntegralTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
alias U = IntegralTypeOf!T;
Expand Down Expand Up @@ -1900,7 +1900,7 @@ if (is(IntegralTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
assert(w.data == "1337");
}

private void formatIntegral(Writer, T, Char)(Writer w, const(T) val, const ref FormatSpec!Char fs,
private void formatIntegral(Writer, T, Char)(ref Writer w, const(T) val, const ref FormatSpec!Char fs,
uint base, ulong mask)
{
T arg = val;
Expand All @@ -1918,7 +1918,8 @@ private void formatIntegral(Writer, T, Char)(Writer w, const(T) val, const ref F
formatUnsigned(w, (cast(ulong) arg) & mask, fs, base, negative);
}

private void formatUnsigned(Writer, T, Char)(Writer w, T arg, const ref FormatSpec!Char fs, uint base, bool negative)
private void formatUnsigned(Writer, T, Char)
(ref Writer w, T arg, const ref FormatSpec!Char fs, uint base, bool negative)
{
/* Write string:
* leftpad prefix1 prefix2 zerofill digits rightpad
Expand Down Expand Up @@ -2120,7 +2121,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(FloatingPointTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
import std.algorithm.comparison : min;
Expand Down Expand Up @@ -2344,7 +2345,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(Unqual!T : creal) && !is(T == enum) && !hasToString!(T, Char))
{
immutable creal val = obj;
Expand Down Expand Up @@ -2400,7 +2401,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(Unqual!T : ireal) && !is(T == enum) && !hasToString!(T, Char))
{
immutable ireal val = obj;
Expand Down Expand Up @@ -2447,7 +2448,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(CharTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
CharTypeOf!T val = obj;
Expand Down Expand Up @@ -2520,7 +2521,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(StringTypeOf!T) && !is(StaticArrayTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
Unqual!(StringTypeOf!T) val = obj; // for `alias this`, see bug5371
Expand Down Expand Up @@ -2598,7 +2599,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, auto ref T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T obj, const ref FormatSpec!Char f)
if (is(StaticArrayTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
formatValue(w, obj[], f);
Expand Down Expand Up @@ -2641,7 +2642,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(DynamicArrayTypeOf!T) && !is(StringTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
static if (is(const(ArrayTypeOf!T) == const(void[])))
Expand Down Expand Up @@ -3024,7 +3025,7 @@ if (isInputRange!T)
}

// character formatting with ecaping
private void formatChar(Writer)(Writer w, in dchar c, in char quote)
private void formatChar(Writer)(ref Writer w, in dchar c, in char quote)
{
import std.uni : isGraphical;

Expand Down Expand Up @@ -3062,7 +3063,7 @@ private void formatChar(Writer)(Writer w, in dchar c, in char quote)

// undocumented because of deprecation
// string elements are formatted like UTF-8 string literals.
void formatElement(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatElement(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (is(StringTypeOf!T) && !is(T == enum))
{
import std.array : appender;
Expand Down Expand Up @@ -3147,7 +3148,7 @@ if (is(StringTypeOf!T) && !is(T == enum))

// undocumented because of deprecation
// Character elements are formatted like UTF-8 character literals.
void formatElement(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatElement(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (is(CharTypeOf!T) && !is(T == enum))
{
if (f.spec == 's')
Expand All @@ -3173,7 +3174,7 @@ if (is(CharTypeOf!T) && !is(T == enum))

// undocumented
// Maybe T is noncopyable struct, so receive it by 'auto ref'.
void formatElement(Writer, T, Char)(Writer w, auto ref T val, const ref FormatSpec!Char f)
void formatElement(Writer, T, Char)(auto ref Writer w, auto ref T val, const ref FormatSpec!Char f)
if (!is(StringTypeOf!T) && !is(CharTypeOf!T) || is(T == enum))
{
formatValue(w, val, f);
Expand All @@ -3188,7 +3189,7 @@ Params:
obj = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T obj, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(AssocArrayTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
{
AssocArrayTypeOf!T val = obj;
Expand Down Expand Up @@ -3433,7 +3434,7 @@ const string toString();
Otherwise, are formatted just as their type name.
*/
void formatValue(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (is(T == class) && !is(T == enum))
{
enforceValidFormatSpec!(T, Char)(f);
Expand Down Expand Up @@ -3582,7 +3583,7 @@ if (is(T == class) && !is(T == enum))
}

/// ditto
void formatValue(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (is(T == interface) && (hasToString!(T, Char) || !is(BuiltinTypeOf!T)) && !is(T == enum))
{
enforceValidFormatSpec!(T, Char)(f);
Expand Down Expand Up @@ -3662,7 +3663,7 @@ if (is(T == interface) && (hasToString!(T, Char) || !is(BuiltinTypeOf!T)) && !is

/// ditto
// Maybe T is noncopyable struct, so receive it by 'auto ref'.
void formatValue(Writer, T, Char)(Writer w, auto ref T val, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T val, const ref FormatSpec!Char f)
if ((is(T == struct) || is(T == union)) && (hasToString!(T, Char) || !is(BuiltinTypeOf!T)) && !is(T == enum))
{
enforceValidFormatSpec!(T, Char)(f);
Expand Down Expand Up @@ -3797,7 +3798,7 @@ Params:
val = The value to write.
f = The $(D FormatSpec) defining how to write the value.
*/
void formatValue(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (is(T == enum))
{
if (f.spec == 's')
Expand Down Expand Up @@ -3866,7 +3867,7 @@ if (is(T == enum))
/**
Pointers are formatted as hex integers.
*/
void formatValue(Writer, T, Char)(Writer w, T val, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, T val, const ref FormatSpec!Char f)
if (isPointer!T && !is(T == enum) && !hasToString!(T, Char))
{
static if (isInputRange!T)
Expand Down Expand Up @@ -3971,7 +3972,7 @@ if (isPointer!T && !is(T == enum) && !hasToString!(T, Char))
/**
Delegates are formatted by 'ReturnType delegate(Parameters) FunctionAttributes'
*/
void formatValue(Writer, T, Char)(Writer w, scope T, const ref FormatSpec!Char f)
void formatValue(Writer, T, Char)(auto ref Writer w, scope T, const ref FormatSpec!Char f)
if (isDelegate!T)
{
formatValue(w, T.stringof, f);
Expand Down

0 comments on commit e8ef731

Please sign in to comment.