英語版
このページの英語版を見る

std.format.write

これは std.format.
フォーマットされた出力を書くための2つの関数を提供する: formatValueformattedWrite.前者は単一の 値を書き込む。後者は、書式なしテキストを挟んで一度に複数の値を書き出す。 を挟む。
フォーマット文字と型の組み合わせは以下の通りである。 利用できる:
s c d, u, b, o x, X e、E、f、F、g、G、a、A r 複合
bool はい - はい はい - はい -
null はい - - - - - -
整数 はい - はい はい はい はい -
浮動小数点 はい - - - はい はい -
性格 はい はい はい はい - はい -
文字列 はい - - - - イエス はい
配列 はい - - - - イエス はい
連想配列 はい - - - - - はい
ポインター はい - - はい - - -
SIMDベクトル はい - - - - イエス イエス
代表団 はい - - - - イエス はい
列挙型は、基本型のすべてのフォーマット文字で使用できる。

構造体、共用体、クラス、インターフェース

集約型は様々なtoString 関数を定義することができる。この この関数が引数としてFormatSpecまたは書式文字列を取る場合、関数はどの書式文字を受け入れるかを決定する。 を決定する。toString が定義されておらず すなわち、's''r' および複合指定子が受け入れられる。それ以外の場合 集約型は's' のみを受け付ける。
toString は、以下のシグネチャのいずれかを持たなければならない:
void toString(Writer, Char)(ref Writer w, const ref FormatSpec!Char fmt)
void toString(Writer)(ref Writer w)
string toString();
ここで、Writer は文字(最初のバージョンでは「型」)を受け付ける出力範囲である。 Char 最初のバージョンでは)テンプレート型は と呼ばれる必要はない。 をWriter と呼ぶ必要はない。
テンプレートを使用できない場合もある。 toString Object.toString をオーバーライドする場合などである。この場合、次のような (より遅く、柔軟性に欠ける)関数を使うことができる:
void toString(void delegate(const(char)[]) sink, const ref FormatSpec!char fmt);
void toString(void delegate(const(char)[]) sink, string fmt);
void toString(void delegate(const(char)[]) sink);
上記のtoString バージョンが複数ある場合は、 の付いたバージョンが優先される。 Writer がついているバージョンが優先される。 sink string toString() が最も優先順位が低い。
上記のtoString バージョンが1つも利用できない場合 集計は、以下の順序で、他の方法でフォーマットされる。 の順になる:
集約が入力範囲である場合、それは入力範囲のようにフォーマットされる。
集約が組み込み型の場合(alias this を使用)、組み込み型のようにフォーマットされる。 のようにフォーマットされる。
構造体は、Type(field1, field2, ...) のようにフォーマットされる、 クラスとインターフェースはその完全修飾名でフォーマットされる。 そして共用体はそのベース名でフォーマットされる。
Authors:
Examples:
boolの書式は、"true" または"false"%s を加えたものである。 bytes 1と0は他のすべてのフォーマット文字と同じである。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
formatValue(w1, true, spec1);

writeln(w1.data); // "true"

auto w2 = appender!string();
auto spec2 = singleSpec("%#x");
formatValue(w2, true, spec2);

writeln(w2.data); // "0x1"
Examples:
null リテラルの書式は"null" である。
import std.array : appender;
import std.format.spec : singleSpec;

auto w = appender!string();
auto spec = singleSpec("%s");
formatValue(w, null, spec);

writeln(w.data); // "null"
Examples:
積分は、(符号あり)毎日表記で、%s%d で、基礎となるビット表現の(符号なし)イメージとしてフォーマットされている。 %b (2進数)、%u (10進数)、%o (8進数)、%x (16進数)。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%d");
formatValue(w1, -1337, spec1);

writeln(w1.data); // "-1337"

auto w2 = appender!string();
auto spec2 = singleSpec("%x");
formatValue(w2, -1337, spec2);

writeln(w2.data); // "fffffac7"
Examples:
浮動小数点値は、自然記法では%f で、科学記法では で、短縮記法では でフォーマットされる。 科学的記数法では%e で、Short記数法では%g で、16進科学的記数法では でフォーマットされる。 16進科学表記法では%a でフォーマットされる。 丸めモードが利用可能な場合は、その丸めモードに従って丸められる。 最も近い値に丸められ、偶数に結ばれる。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%.3f");
formatValue(w1, 1337.7779, spec1);

writeln(w1.data); // "1337.778"

auto w2 = appender!string();
auto spec2 = singleSpec("%.3e");
formatValue(w2, 1337.7779, spec2);

writeln(w2.data); // "1.338e+03"

auto w3 = appender!string();
auto spec3 = singleSpec("%.3g");
formatValue(w3, 1337.7779, spec3);

writeln(w3.data); // "1.34e+03"

auto w4 = appender!string();
auto spec4 = singleSpec("%.3a");
formatValue(w4, 1337.7779, spec4);

writeln(w4.data); // "0x1.4e7p+10"
Examples:
個々の文字 (char,wchar, またはdchar) は、次のようにフォーマットされる。 %s%c では Unicode 文字として、その他のすべての書式文字では整数 (ubyteushort uint) となる。複合指定子 複合指定子では は異なる扱いを受ける。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%c");
formatValue(w1, 'ì', spec1);

writeln(w1.data); // "ì"

auto w2 = appender!string();
auto spec2 = singleSpec("%#x");
formatValue(w2, 'ì', spec2);

writeln(w2.data); // "0xec"
Examples:
文字列は、%s を持つ文字のシーケンスとしてフォーマットされる。 印字不可能な文字はエスケープされない。複合指定子 を指定すると、文字列は文字の範囲のように扱われる。複合指定子では、文字列は異なる扱いを受ける。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
formatValue(w1, "hello", spec1);

writeln(w1.data); // "hello"

auto w2 = appender!string();
auto spec2 = singleSpec("%(%#x%|/%)");
formatValue(w2, "hello", spec2);

writeln(w2.data); // "0x68/0x65/0x6c/0x6c/0x6f"
Examples:
静的配列は動的配列としてフォーマットされる。
import std.array : appender;
import std.format.spec : singleSpec;

auto w = appender!string();
auto spec = singleSpec("%s");
int[2] two = [1, 2];
formatValue(w, two, spec);

writeln(w.data); // "[1, 2]"
Examples:
動的配列は入力範囲としてフォーマットされる。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
auto two = [1, 2];
formatValue(w1, two, spec1);

writeln(w1.data); // "[1, 2]"

auto w2 = appender!string();
auto spec2 = singleSpec("%(%g%|, %)");
auto consts = [3.1415926, 299792458, 6.67430e-11];
formatValue(w2, consts, spec2);

writeln(w2.data); // "3.14159, 2.99792e+08, 6.6743e-11"

// void[]はubyte[]のように扱われる
auto w3 = appender!string();
auto spec3 = singleSpec("%s");
void[] val = cast(void[]) cast(ubyte[])[1, 2, 3];
formatValue(w3, val, spec3);

writeln(w3.data); // "[1, 2, 3]"
Examples:
連想配列は、':'", " をセパレータとして使ってフォーマットされる。 '['%s と併用する場合は']' で囲む。 また、複合指定子を使用して、より優れた制御を行うことも可能である。
要素の順序は定義されていない。 この関数の結果は異なる可能性がある。
import std.array : appender;
import std.format.spec : singleSpec;

auto aa = [10:17.5, 20:9.99];

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
formatValue(w1, aa, spec1);

assert(w1.data == "[10:17.5, 20:9.99]" || w1.data == "[20:9.99, 10:17.5]");

auto w2 = appender!string();
auto spec2 = singleSpec("%(%x = %.0e%| # %)");
formatValue(w2, aa, spec2);

assert(w2.data == "a = 2e+01 # 14 = 1e+01" || w2.data == "14 = 1e+01 # a = 2e+01");
Examples:
enumは、%s で使用される場合はその名前として、それ以外の場合はそのベース値としてフォーマットされる。 のようにフォーマットされる。
import std.array : appender;
import std.format.spec : singleSpec;

enum A { first, second, third }

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
formatValue(w1, A.second, spec1);

writeln(w1.data); // "second"

auto w2 = appender!string();
auto spec2 = singleSpec("%d");
formatValue(w2, A.second, spec2);

writeln(w2.data); // "1"

// 名前を持たない列挙型の値は、キャストを使って%sでフォーマットされる
A a = A.third;
a++;

auto w3 = appender!string();
auto spec3 = singleSpec("%s");
formatValue(w3, a, spec3);

writeln(w3.data); // "cast(A)3"
Examples:
structs unions 、 、 は、いくつかの異なる方法でフォーマットすることができる。 はいくつかの異なる方法でフォーマットできる。以下の例では、その書式に焦点をあてている。 の書式を強調しているが、これは他の集計にも適用できる。 classes interfaces struct
import std.array : appender;
import std.format.spec : FormatSpec, singleSpec;

// writerで`toString`を使う
static struct Point1
{
    import std.range.primitives : isOutputRange, put;

    int x, y;

    void toString(W)(ref W writer, scope const ref FormatSpec!char f)
    if (isOutputRange!(W, char))
    {
        put(writer, "(");
        formatValue(writer, x, f);
        put(writer, ",");
        formatValue(writer, y, f);
        put(writer, ")");
    }
}

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
auto p1 = Point1(16, 11);

formatValue(w1, p1, spec1);
writeln(w1.data); // "(16,11)"

// sinkで`toString`を使う
static struct Point2
{
    int x, y;

    void toString(scope void delegate(scope const(char)[]) @safe sink,
                  scope const FormatSpec!char fmt) const
    {
        sink("(");
        sink.formatValue(x, fmt);
        sink(",");
        sink.formatValue(y, fmt);
        sink(")");
    }
}

auto w2 = appender!string();
auto spec2 = singleSpec("%03d");
auto p2 = Point2(16,11);

formatValue(w2, p2, spec2);
writeln(w2.data); // "(016,011)"

// `string toString()`を使う
static struct Point3
{
    int x, y;

    string toString()
    {
        import std.conv : to;

        return "(" ~ to!string(x) ~ "," ~ to!string(y) ~ ")";
    }
}

auto w3 = appender!string();
auto spec3 = singleSpec("%s"); // %sでなければならない
auto p3 = Point3(16,11);

formatValue(w3, p3, spec3);
writeln(w3.data); // "(16,11)"

// without `toString`
static struct Point4
{
    int x, y;
}

auto w4 = appender!string();
auto spec4 = singleSpec("%s"); // %sでなければならない
auto p4 = Point4(16,11);

formatValue(w4, p4, spec3);
writeln(w4.data); // "Point4(16, 11)"
Examples:
ポインターは16進整数でフォーマットされる。
import std.array : appender;
import std.format.spec : singleSpec;

auto w1 = appender!string();
auto spec1 = singleSpec("%s");
auto p1 = () @trusted { return cast(void*) 0xFFEECCAA; } ();
formatValue(w1, p1, spec1);

writeln(w1.data); // "FFEECCAA"

// nullポインタは、`%s`と一緒に使われた場合は`"null"`として、それ以外の場合は16進整数として表示される
auto w2 = appender!string();
auto spec2 = singleSpec("%s");
auto p2 = () @trusted { return cast(void*) 0x00000000; } ();
formatValue(w2, p2, spec2);

writeln(w2.data); // "null"

auto w3 = appender!string();
auto spec3 = singleSpec("%x");
formatValue(w3, p2, spec3);

writeln(w3.data); // "0"
Examples:
SIMDベクトルは配列としてフォーマットされる。
import core.simd; // float4が定義されていない可能性があるため、選択できない
import std.array : appender;
import std.format.spec : singleSpec;

auto w = appender!string();
auto spec = singleSpec("%s");

static if (is(float4))
{
    version (X86) {}
    else
    {
        float4 f4;
        f4.array[0] = 1;
        f4.array[1] = 2;
        f4.array[2] = 3;
        f4.array[3] = 4;

        formatValue(w, f4, spec);
        writeln(w.data); // "[1, 2, 3, 4]"
    }
}
uint formattedWrite(Writer, Char, Args...)(auto ref Writer w, scope const Char[] fmt, Args args);

uint formattedWrite(alias fmt, Writer, Args...)(auto ref Writer w, Args args)
if (isSomeString!(typeof(fmt)));
引数をフォーマット文字列に従って変換し、結果を出力範囲に書き込む。 結果を出力範囲に書き出す。
の2番目のバージョンは formattedWriteの第2バージョンは、フォーマット文字列を テンプレート引数として受け取る。この場合、コンパイル時に整合性がチェックされる。 コンパイル時にチェックされる。
Parameters:
Writer w 出力範囲を指定する、 フォーマットされた結果は
Char[] fmt フォーマット文字列
Args args フォーマットされる引数の可変長引数リスト
Writer ライターの型 w
Char の文字型 fmt
Args 引数の可変長引数リスト
Returns:
フォーマットされた最後の引数のインデックス。位置引数 引数が使われていない場合、これはフォーマットされた引数の数である。
Throws:

注釈:理論的には、この関数は であるべきである。 理論的には、この関数は@nogc 。しかし現在の の実装では、アロケーションが発生するケースがある。 詳細は sformatを参照のこと。

Examples:
import std.array : appender;

auto writer1 = appender!string();
formattedWrite(writer1, "%s is the ultimate %s.", 42, "answer");
writeln(writer1[]); // "42 is the ultimate answer."("42が究極の答えだ。")

auto writer2 = appender!string();
formattedWrite(writer2, "Increase: %7.2f %%", 17.4285);
writeln(writer2[]); // "Increase:   17.43 %"
Examples:
フォーマット文字列はコンパイル時にチェックできる:
import std.array : appender;

auto writer = appender!string();
writer.formattedWrite!"%d is the ultimate %s."(42, "answer");
writeln(writer[]); // "42 is the ultimate answer."("42が究極の答えだ。")

// 3.14は%dでフォーマットできないので、この行はコンパイルされない:
// writer.formattedWrite!"%d is the ultimate %s."(3.14, "answer");
void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T val, ref scope const FormatSpec!Char f);
任意の型の値をフォーマット指定子に従ってフォーマットし、その結果を出力範囲に書き込む。 結果を出力範囲に書き出す。
型がどのようにフォーマットされるのか、そしてフォーマット指定子がどのように結果に影響するのか、詳細は 指定子が結果にどのような影響を与えるかについての詳細は 定義にある。
Parameters:
Writer w 出力範囲 フォーマットされた値が書き込まれる。
T val 書き込む値
FormatSpec!Char f 書式指定子を定義する フォーマット指定子
Writer 出力範囲の型 w
T 値の型 val
Char 出力に使用する文字型 f
Throws:
A FormatExceptionに使われた文字タイプである。

注釈:理論的には、この関数は であるべきである。 理論的には、この関数は@nogc 。しかし、現在の の実装では、アロケーションが発生するケースがある。 詳細は sformatを参照のこと。

See Also:
formattedWriteこれは一度に複数の値をフォーマットする。
Examples:
import std.array : appender;
import std.format.spec : singleSpec;

auto writer = appender!string();
auto spec = singleSpec("%08b");
writer.formatValue(42, spec);
writeln(writer.data); // "00101010"

spec = singleSpec("%2s");
writer.formatValue('=', spec);
writeln(writer.data); // "00101010 ="

spec = singleSpec("%+14.6e");
writer.formatValue(42.0, spec);
writeln(writer.data); // "00101010 = +4.200000e+01"