Skip to content

Commit 98c71c6

Browse files
committed
アライメントの詳細を記載
1 parent 89d821a commit 98c71c6

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

reference/format/format.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ string s3 = format("{} {1}", "a", "b"); // コンパイルエラー
126126

127127
イテレータ範囲`[first, last)`[`to_chars`](/reference/charconv/to_chars.md)の結果を格納するのに十分な範囲、`value`をフォーマットする値、`charT``char`または`wchar_t`とする。
128128

129-
* 以下の表の通りに[`to_chars`](/reference/charconv/to_chars.md)を呼び出したあと、その結果を出力へコピーするかのような振る舞いをする。ただし、実際に[`to_chars`](/reference/charconv/to_chars.md)を呼び出すかどうかは規定されていない。
130-
* 実際には、出力へコピーする際にパディングなども行われる。
129+
* ロケールを考慮しない場合、数値を文字列化する部分は以下の表の通りに[`to_chars`](/reference/charconv/to_chars.md)を呼び出した結果と等しくなる。
131130

132131
| type | 意味 | 効果 | 対応バージョン |
133132
|:-------|:---------------------------|:--------------------------------------------------------------------------|----------------|
@@ -164,6 +163,51 @@ string s3 = format("{} {1}", "a", "b"); // コンパイルエラー
164163

165164
ポインタは`void*`のみサポートする。それ以外のポインタは、`void*`にキャストしなければ出力できない。
166165

166+
#### アライメントの詳細
167+
168+
指定した幅が必要な幅より小さい場合や、幅を省略している場合はアライメントは無効となる。
169+
170+
アライメントにおいて、一部の文字は大きな幅を持っているとみなされる。
171+
172+
UTF-8・UTF-16・UTF-32の文字のシーケンスの場合、その幅はUnicode標準 UAX #29 で定める拡張書記素クラスタの幅の合計となる。
173+
174+
以下の文字は2文字分の幅を持つ。
175+
176+
- UAX #44 の規定に従って、`East_Asian_Width``W` (Wide)または `F` (Fullwidth)であるコードポイント
177+
- `U+4DC0` - `U+4DFF` (Yijing Hexagram Symbols)
178+
- `U+1F300` - `U+1F5FF` (Miscellaneous Symbols and Pictographs)
179+
- `U+1F900` - `U+1F9FF` (Supplemental Symbols and Pictographs)
180+
181+
それ以外のコードポイントの幅は1である。
182+
183+
UTF-8・UTF-16・UTF-32以外の文字のシーケンスの幅は未規定。
184+
185+
アライメントに使用する文字は、常に幅1として扱われる。
186+
187+
```cpp
188+
std::format("{:*>6}", "あ"); // "****あ"
189+
std::format("{:あ>6}", 'x'); // "あああああx"
190+
```
191+
192+
##### 0埋めとの関係
193+
194+
アライメント方向が指定されていると、0埋めは無効となる。
195+
196+
```cpp
197+
// 0埋めは符号を考慮する
198+
std::format("{:05}", 42); // "00042"
199+
std::format("{:+05}", 42); // "+0042"
200+
std::format("{: 05}", 42); // " 0042"
201+
202+
// アライメントは符号を考慮しない
203+
std::format("{:0>5}", 42); // "00042"
204+
std::format("{:0>+5}", 42); // "00+42"
205+
std::format("{:0> 5}", 42); // "00 42"
206+
207+
// 0埋めも指定しているが、アライメント方向が指定されているのでアライメントが優先される
208+
std::format("{:0>05}", -42); // "00-42"
209+
```
210+
167211
#### <a id="range-format-options" href="#range-format-options">Range・シーケンスコンテナの書式 (C++23)</a>
168212

169213
Range・シーケンスコンテナに対して使用できる標準のオプション書式は次の通り(`[]`は省略可の意味)。

0 commit comments

Comments
 (0)