Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
a local clone.
mir.format
@nogc Formatting Utilities
License:
Authors:
Ilya Yaroshenko
Examples:
mir.conv: to extension.
import mir.conv: to; import mir.small_string; alias S = SmallString!32; // Floating-point numbers are formatted to // the shortest precise exponential notation. assert(123.0.to!S == "123.0"); assert(123.to!(immutable S) == "123"); assert(true.to!S == "true"); assert(true.to!string == "true"); assert((cast(S)"str")[] == "str");
Examples:
mir.conv: to extension.
import mir.conv: to; import mir.small_string; alias S = SmallString!32; auto str = S("str"); assert(str.to!(const(char)[]) == "str"); // GC allocated result assert(str.to!(char[]) == "str"); // GC allocated result
Examples:
ditto
import mir.conv: to; import mir.small_string; alias S = SmallString!32; // Floating-point numbers are formatted to // the shortest precise exponential notation. assert(123.0.to!string == "123.0"); assert(123.to!(char[]) == "123"); assert(S("str").to!string == "str"); // GC allocated result
- string
text
(string separator = "", A...)(auto ref Aargs
)
if (A.length > 0); - Concatenated string resultsExamples:
assert(text("str ", true, " ", 100, " ", 124.1) == "str true 100 124.1", text("str ", true, " ", 100, " ", 124.1)); assert(text!" "("str", true, 100, 124.1) == "str true 100 124.1");
- struct
GetData
; - enum GetData
getData
; - struct
_stringBuf
(C);
aliasstringBuf
= _stringBuf!char._stringBuf;
aliaswstringBuf
= _stringBuf!wchar._stringBuf;
aliasdstringBuf
= _stringBuf!dchar._stringBuf; -
- ScopedBuffer!C
buffer
;
- template
StreamFormatOp
(C) - Examples:
auto name = "D"; auto ver = 2.0; assert(stringBuf() << "Hi " << name << ver << "!\n" << getData == "Hi D2.0!\n");
Examples:auto name = "D"w; auto ver = 2; assert(wstringBuf() << "Hi "w << name << ver << "!\n"w << getData == "Hi D2!\n"w);
Examples:auto name = "D"d; auto ver = 2UL; assert(dstringBuf() << "Hi "d << name << ver << "!\n"d << getData == "Hi D2!\n");
- ref scope typeof(this)
opBinary
(string op : "<<", T)(ref const Tc
); - ref scope typeof(this)
opBinary
(string op : "<<", T)(const Tc
);
scope const(C)[]opBinary
(string op : "<<", T : GetData)(const Tc
);
- struct
NumericSpec
; - Mir's numeric format specification
Note the specification isn't complete an may be extended in the future.
- enum
Format
: int; -
human
- Human-frindly precise output.Examples:0.000001, 600000.0, but 1e-7 and 6e7.
exponent
- Precise output with explicit exponent.Examples:1e-6, 6e6, 1.23456789e-100.
- Format
format
; - enum NumericSpec
exponent
; - Precise output with explicit exponent.Examples:1e-6, 6e6, 1.23456789e-100.
- enum NumericSpec
human
; - Precise output with explicit exponent.Examples:1e-6, 6e6, 1.23456789e-100.
- struct
FormatSpec
; - C's compatible format specifier.
- bool
dash
; - bool
plus
; - bool
space
; - bool
hash
; - bool
zero
; - char
format
; - char
separator
; - ubyte
unitSize
; - int
width
; - int
precision
;
- enum
SwitchLU
: bool; -
lower
upper
- struct
FormattedFloating
(T) if (is(T == float) || is(T == double) || is(T == real));
FormattedFloating!TwithFormat
(T)(const Tvalue
, FormatSpecspec
); - Wrapper to format floating point numbers using C's library.
- T
value
; - FormatSpec
spec
; - const scope void
toString
(C = char, W)(ref scope Ww
);
- struct
FormattedNumeric
(T) if (is(T == float) || is(T == double) || is(T == real));
FormattedNumeric!TwithFormat
(T)(const Tvalue
, NumericSpecspec
); - Wrapper to format floating point numbers using C's library.
- T
value
; - NumericSpec
spec
; - const scope void
toString
(C = char, W)(ref scope Ww
);
- struct
HexAddress
(T) if (isUnsigned!T && !is(T == enum)); -
- T
value
; - SwitchLU
switchLU
; - const scope void
toString
(C = char, W)(ref scope Ww
);
- enum
EscapeFormat
: int; - ref W
printEscaped
(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(return ref scope Ww
, scope const(C)[]str
); - Examples:
import mir.appender: ScopedBuffer; ScopedBuffer!char w; assert(w.printEscaped("Hi \f\t\b \\\r\n" ~ `"@nogc"`).data == "Hi \f\t\b \\\\\\r\\n\\\"@nogc\\\"", w.data); w.reset; assert(w.printEscaped("\x03").data == `\x03`, w.data);
- ref W
put_xXX
(C = char, W)(return ref scope Ww
, charc
); - Decodes char
c
to the form u00XX, where XX is 2 hexadecimal characters. - ref W
put_uXXXX
(C = char, W)(return ref scope Ww
, charc
); - Decodes char
c
to the form u00XX, where XX is 2 hexadecimal characters. - ref W
put_uXXXX
(C = char, W)(return ref scope Ww
, ushortc
); - Decodes ushort
c
to the form uXXXX, where XXXX is 2 hexadecimal characters. - ref W
printElement
(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(return ref scope Ww
, scope const(C)[]c
)
if (isSomeChar!C); - ref W
printElement
(C = char, EscapeFormat escapeFormat = EscapeFormat.ion, W, T)(return ref scope Ww
, auto ref scope const Tc
)
if (!isSomeString!T); - ref W
print
(C = char, W, Args...)(return ref scope Ww
, auto ref scope const Argsargs
)
if (Args.length > 1); - Multiargument overload.
- ref W
print
(C = char, W, T)(return ref scope Ww
, const Tc
)
if (is(T == enum)); - Prints enumsExamples:
enum Flag { no, yes, } import mir.appender: ScopedBuffer; ScopedBuffer!char w; w.print(Flag.yes); assert(w.data == "yes", w.data);
- ref W
print
(C = char, W)(return ref scope Ww
, boolc
); - Prints booleanExamples:
import mir.appender: ScopedBuffer; ScopedBuffer!char w; assert(w.print(true).data == `true`, w.data); w.reset; assert(w.print(false).data == `false`, w.data);
- ref W
print
(C = char, W, V, K)(return ref scope Ww
, scope const V[K]c
); - Prints associative arrayExamples:
import mir.appender: ScopedBuffer; ScopedBuffer!char w; w.print(["a": 1, "b": 2]); assert(w.data == `["a": 1, "b": 2]` || w.data == `["b": 2, "a": 1]`, w.data);
- ref W
print
(C = char, W, T)(return ref scope Ww
, scope const(T)[]c
)
if (!isSomeChar!T); - Prints arrayExamples:
import mir.appender: ScopedBuffer; ScopedBuffer!char w; string[2] array = ["a\na", "b"]; assert(w.print(array[]).data == `["a\na", "b"]`, w.data);
- ref W
print
(C = char, W)(return ref scope Ww
, charc
); - Prints escaped character in the form '
c
'.Examples:import mir.appender: ScopedBuffer; ScopedBuffer!char w; assert(w .print('\n') .print('\'') .print('a') .print('\xF4') .data == `'\n''\'''a''\xF4'`);
- ref W
print
(C = char, W)(return ref scope Ww
, scope const(C)[]c
)
if (isSomeChar!C); - Prints some string
- ref W
print
(C = char, W, I)(return ref scope Ww
, const Ic
)
if (isIntegral!I && !is(I == enum)); - Prints integers
- ref W
print
(C = char, W, T)(return ref scope Ww
, const Tc
, NumericSpecspec
= NumericSpec.init)
if (is(T == float) || is(T == double) || is(T == real)); - Prints floating point numbersExamples:
assert(stringBuf() << -0.0 << getData == "-0.0"); assert(stringBuf() << 0.0 << getData == "0.0"); assert(stringBuf() << -0.01 << getData == "-0.01"); assert(stringBuf() << 0.0125 << getData == "0.0125"); assert(stringBuf() << 0.000003 << getData == "0.000003"); assert(stringBuf() << -3e-7 << getData == "-3e-7"); assert(stringBuf() << 123456.0 << getData == "123456.0"); assert(stringBuf() << 12.3456 << getData == "12.3456"); assert(stringBuf() << -0.123456 << getData == "-0.123456"); assert(stringBuf() << 0.1234567 << getData == "1.234567e-1"); assert(stringBuf() << -1234567.0 << getData == "-1.234567e6"); assert(stringBuf() << 1234567890123.0 << getData == "1.234567890123e12"); assert(stringBuf() << +double.nan << getData == "nan"); assert(stringBuf() << -double.nan << getData == "nan"); assert(stringBuf() << +double.infinity << getData == "+inf"); assert(stringBuf() << -double.infinity << getData == "-inf");
- ref W
print
(C = char, W, T)(return ref scope Ww
, ref const Tc
)
if (is(T == struct) || is(T == union) && !is(T : NumericSpec));
ref Wprint
(C = char, W, T)(return ref scope Ww
, scope const Tc
)
if (is(T == struct) || is(T == union)); - Prints structs and unionsExamples:
static struct A { scope void toString(C, W)(scope ref W w) const { w.put(C('a')); } } static struct S { scope void toString(W)(scope ref W w) const { w.put("s"); } } static struct D { scope void toString(Dg)(scope Dg sink) const { sink("d"); } } static struct F { scope const(char)[] toString()() const return { return "f"; } } static struct G { const(char)[] s = "g"; alias s this; } import mir.appender: ScopedBuffer; ScopedBuffer!char w; assert(stringBuf() << A() << S() << D() << F() << G() << getData == "asdfg");
- ref W
print
(C = char, W, T)(return ref scope Ww
, scope const Tc
)
if (is(T == class) || is(T == interface)); - Prints classes and interfacesExamples:
static class A { void toString(C, W)(scope ref W w) const { w.put(C('a')); } } static class S { void toString(W)(scope ref W w) const { w.put("s"); } } static class D { void toString(Dg)(scope Dg sink) const { sink("d"); } } static class F { const(char)[] toString()() const return { return "f"; } } static class G { const(char)[] s = "g"; alias s this; } import mir.appender: ScopedBuffer; ScopedBuffer!char w; assert(stringBuf() << new A() << new S() << new D() << new F() << new G() << getData == "asdfg");
- ref W
printStaticString
(C, size_t N, W)(return ref scope Ww
, ref scope const C[N]c
)
if (is(C == char) || is(C == wchar) || is(C == dchar)); - ref W
printZeroPad
(C = char, W, I)(return ref scope Ww
, const Ic
, size_tminimalLength
)
if (isIntegral!I && !is(I == enum)); - Examples:
import mir.appender; ScopedBuffer!char w; w.printZeroPad(-123, 5); w.put(' '); w.printZeroPad(123, 5); assert(w.data == "-0123 00123", w.data);
- size_t
printBoolean
(C)(boolc
, ref C[5]buf
)
if (is(C == char) || is(C == wchar) || is(C == dchar));
Copyright © 2016-2021 by Ilya Yaroshenko | Page generated by
Ddoc on Fri Feb 26 05:58:33 2021