英語版
このページの英語版を見る
std.format.read
これは std.format.
フォーマットされた入力を読み込むための2つの関数を提供する: unformatValueと formattedRead.前者は単一の
の値を読み取る。後者は一度に複数の値を読み、書式指定子の間にある文字にマッチする。
文字にマッチする。
パラメータは無視される。
'*'.詳細は formattedReadを参照のこと。
フォーマット指定子の外側にあるスペースは特別な意味を持つ。
は、1つの空白文字だけでなく、空白文字の連続にもマッチする。
だけでなく、空白文字の連続にもマッチする。
フォーマット文字と型の組み合わせは以下の通りである。
利用できる:
これらの組み合わせがどのように使われるか、以下に例を示す。
である。 unformatValueただし formattedReadまた
s | c | d、u、b、o、x、X | e、E、f、g、G | r | 複合 | |
---|---|---|---|---|---|---|
bool | はい | はい | ||||
null | はい | |||||
整数 | はい | はい | はい | |||
浮動小数点 | はい | はい | はい | |||
性格 | はい | はい | はい | |||
文字列 | はい | はい | ||||
配列 | はい | はい | ||||
連想配列 | はい | はい |
License:
Authors:
Walter Bright, Andrei Alexandrescu, and Kenji Hara
Examples:
ブーリアン
import std.format.spec : singleSpec; auto str = "false"; auto spec = singleSpec("%s"); writeln(str.unformatValue!bool(spec)); // false str = "1"; spec = singleSpec("%d"); writeln(str.unformatValue!bool(spec)); // true
Examples:
ヌル値
import std.format.spec : singleSpec; auto str = "null"; auto spec = singleSpec("%s"); writeln(str.unformatValue!(typeof(null))(spec)); // null
Examples:
積分
import std.format.spec : singleSpec; // 符号付き10進数値 auto str = "123"; auto spec = singleSpec("%s"); writeln(str.unformatValue!int(spec)); // 123 // 16進数値 str = "ABC"; spec = singleSpec("%X"); writeln(str.unformatValue!int(spec)); // 2748 // 8進数値 str = "11610"; spec = singleSpec("%o"); writeln(str.unformatValue!int(spec)); // 5000 // 生読み、エンディアンに依存する str = "\x75\x01"; spec = singleSpec("%r"); auto result = str.unformatValue!short(spec); assert(result == 373 /* リトルエンディアン */ || result == 29953 /* ビッグエンディアン */ );
Examples:
浮動小数点数
import std.format.spec : singleSpec; import std.math.operations : isClose; // 自然表記法 auto str = "123.456"; auto spec = singleSpec("%s"); assert(str.unformatValue!double(spec).isClose(123.456)); // 科学的記数法 str = "1e17"; spec = singleSpec("%e"); assert(str.unformatValue!double(spec).isClose(1e17)); // 生読み、エンディアンに依存する str = "\x40\x00\x00\xBF"; spec = singleSpec("%r"); auto result = str.unformatValue!float(spec); assert(isClose(result, -0.5) /* リトルエンディアン */ || isClose(result, 2.0) /* ビッグエンディアン */ );
Examples:
登場人物
import std.format.spec : singleSpec; // 最初の文字だけが読み込まれる auto str = "abc"; auto spec = singleSpec("%s"); writeln(str.unformatValue!char(spec)); // 'a' // 数値フォーマット文字を使用すると、読み取った数値をユニコードのコードポイントとして扱う str = "65"; spec = singleSpec("%d"); writeln(str.unformatValue!char(spec)); // 'A' str = "41"; spec = singleSpec("%x"); writeln(str.unformatValue!char(spec)); // 'A' str = "10003"; spec = singleSpec("%d"); writeln(str.unformatValue!dchar(spec)); // '✓'
Examples:
配列
import std.format.spec : singleSpec; // 文字列値 string str = "aaa"; auto spec = singleSpec("%s"); writeln(str.unformatValue!(dchar[])(spec)); // "aaa"d // 文字の固定サイズ配列 str = "aaa"; spec = singleSpec("%s"); dchar[3] ret = ['a', 'a', 'a']; writeln(str.unformatValue!(dchar[3])(spec)); // ret // 動的配列 str = "[1, 2, 3, 4]"; spec = singleSpec("%s"); writeln(str.unformatValue!(int[])(spec)); // [1, 2, 3, 4] // 整数の固定サイズ配列 str = "[1, 2, 3, 4]"; spec = singleSpec("%s"); int[4] ret2 = [1, 2, 3, 4]; writeln(str.unformatValue!(int[4])(spec)); // ret2 // 複合指定子を使用すると、さらに制御できる str = "1,2,3"; spec = singleSpec("%(%s,%)"); writeln(str.unformatValue!(int[])(spec)); // [1, 2, 3] str = "cool"; spec = singleSpec("%(%c%)"); writeln(str.unformatValue!(char[])(spec)); // ['c', 'o', 'o', 'l']
Examples:
連想配列
import std.format.spec : singleSpec; // 単一値として auto str = `["one": 1, "two": 2]`; auto spec = singleSpec("%s"); writeln(str.unformatValue!(int[string])(spec)); // ["one":1, "two":2] // 複合指定子でよりコントロールする str = "1/1, 2/4, 3/9"; spec = singleSpec("%(%d/%d%|, %)"); writeln(str.unformatValue!(int[int])(spec)); // [1:1, 2:4, 3:9]
- uint
formattedRead
(Range, Char, Args...)(auto ref Ranger
, const(Char)[]fmt
, auto ref Argsargs
);
uintformattedRead
(alias fmt, Range, Args...)(auto ref Ranger
, auto ref Argsargs
)
if (isSomeString!(typeof(fmt))); - フォーマット文字列に従って入力範囲を読み取り、読み取った値を引数に格納する。 値を引数に格納する。書式文字'd' 、'u' 、'c' を持つ書式指定子は、値をスキップするためのパラメータ'*' を取ることができる。 の2番目のバージョンは
formattedRead
は、フォーマット文字列を テンプレート引数として受け取る。この場合、コンパイル時に整合性がチェックされる。 コンパイル時にチェックされる。Parameters:Returns:入力された変数の数。入力範囲r
が早く終わった場合 この数は、入力された変数の数より少なくなる。Throws:読み込みに成功しなかった場合はFormatException を返す。注釈: 下位互換性のために、引数
args
へのポインタとして与えることができる。 をその変数へのポインタとして与えることもできるが、そうすることは推奨されない。 オプションは将来削除される可能性があるからだ。Examples:string object; char cmp; int value; writeln(formattedRead("angle < 36", "%s %c %d", object, cmp, value)); // 3 writeln(object); // "angle" writeln(cmp); // '<' writeln(value); // 36 // 読み込みが早く終わる可能性がある: writeln(formattedRead("length >", "%s %c %d", object, cmp, value)); // 2 writeln(object); // "length" writeln(cmp); // '>' // 値は変更されない: writeln(value); // 36
Examples:フォーマット文字列はコンパイル時にチェックできる:string a; int b; double c; writeln("hello!124:34.5".formattedRead!"%s!%s:%s"(a, b, c)); // 3 writeln(a); // "hello" writeln(b); // 124 writeln(c); // 34.5
Examples:値をスキップするstring item; double amount; writeln("orange: (12%) 15.25".formattedRead("%s: (%*d%%) %f", item, amount)); // 2 writeln(item); // "orange" writeln(amount); // 15.25 // タプルでも使用できる import std.typecons : Tuple; Tuple!(int, float) t; char[] line = "1 7643 2.125".dup; formattedRead(line, "%s %*u %s", t); assert(t[0] == 1 && t[1] == 2.125);
- T
unformatValue
(T, Range, Char)(ref Rangeinput
, ref scope const FormatSpec!Charspec
); - 与えられた入力範囲から値を読み取り、フォーマット指定子に従って変換する。 フォーマット指定子に従って変換する。Parameters:Returns:の値である。
input
の値である。T 。Throws:読み込みに成功しなかった場合はFormatException を返す。See Also:Examples:import std.format.spec : singleSpec; string s = "42"; auto spec = singleSpec("%s"); writeln(unformatValue!int(s, spec)); // 42
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku