Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic_format_parse_context: begin, end, advance_toを追加
- Loading branch information
1 parent
96e93fc
commit ad99718
Showing
7 changed files
with
227 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# advance_to | ||
* format[meta header] | ||
* function[meta id-type] | ||
* std[meta namespace] | ||
* basic_format_parse_context[meta class] | ||
* cpp20[meta cpp] | ||
|
||
```cpp | ||
constexpr void advance_to(const_iterator it); | ||
``` | ||
## 概要 | ||
指定したイテレータを先頭イテレータとして設定する。 | ||
この関数は、書式文字列の全体を解析する処理において、[`formatter`](/reference/format/formatter.md)`::`[`parse()`](/reference/format/formatter/parse.md)の戻り値を、次の解析の初期位置として設定するために使用する。 | ||
```cpp | ||
while (…) { | ||
next_parse_ctx.advance_to(f.parse(parse_ctx)); | ||
} | ||
``` | ||
* parse[link /reference/format/formatter/parse.md] | ||
|
||
|
||
## 事前条件 | ||
- `it`は[`end()`](end.md)に到達可能であること | ||
|
||
|
||
## 効果 | ||
メンバ変数として保持している、書式文字列の先頭を指すイテレータ`begin_`があるとして、以下と等価: | ||
|
||
```cpp | ||
begin_ = it; | ||
``` | ||
|
||
|
||
## バージョン | ||
### 言語 | ||
- C++20 | ||
|
||
### 処理系 | ||
- [Clang](/implementation.md#clang): ?? | ||
- [GCC](/implementation.md#gcc): 13 | ||
- [Visual C++](/implementation.md#visual_cpp): ?? | ||
|
||
## 参照 | ||
|
||
- [P0645R10 Text Formatting](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0645r10.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# begin | ||
* format[meta header] | ||
* function[meta id-type] | ||
* std[meta namespace] | ||
* basic_format_parse_context[meta class] | ||
* cpp20[meta cpp] | ||
|
||
```cpp | ||
constexpr const_iterator begin() const noexcept; | ||
``` | ||
|
||
## 概要 | ||
書式文字列の先頭を指すイテレータを取得する。 | ||
|
||
|
||
## 効果 | ||
メンバ変数として保持している、書式文字列の先頭を指すイテレータを返す。 | ||
|
||
|
||
## 例 | ||
### 基本的な使い方 | ||
```cpp example | ||
#include <iostream> | ||
#include <format> | ||
|
||
enum color { red, green, blue }; | ||
|
||
const char* color_names[] = { "red", "green", "blue" }; | ||
|
||
template<> | ||
class std::formatter<color> { | ||
bool enable_quote = false; | ||
public: | ||
constexpr auto parse(std::format_parse_context& ctx) { | ||
auto it = ctx.begin(); | ||
if (it == ctx.end()) { | ||
return it; | ||
} | ||
|
||
if (*it == '%') { | ||
enable_quote = true; | ||
++it; | ||
} | ||
return it; | ||
} | ||
|
||
auto format(color c, std::format_context& ctx) const { | ||
if (enable_quote) { | ||
return std::format_to(ctx.out(), "\"{}\"", color_names[c]); | ||
} | ||
else { | ||
return std::format_to(ctx.out(), "{}", color_names[c]); | ||
} | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
std::cout << std::format("{:%}", red) << std::endl; | ||
} | ||
``` | ||
* ctx.begin()[color ff0000] | ||
* ctx.end()[link end.md] | ||
* std::format[link /reference/format/format.md] | ||
* std::format_to[link /reference/format/format_to.md] | ||
* std::format_context[link /reference/format/basic_format_context.md] | ||
* ctx.out()[link /reference/format/basic_format_context/out.md] | ||
|
||
#### 出力 | ||
``` | ||
"red" | ||
``` | ||
|
||
|
||
## バージョン | ||
### 言語 | ||
- C++20 | ||
|
||
### 処理系 | ||
- [Clang](/implementation.md#clang): ?? | ||
- [GCC](/implementation.md#gcc): 13 | ||
- [Visual C++](/implementation.md#visual_cpp): ?? | ||
|
||
## 参照 | ||
|
||
- [P0645R10 Text Formatting](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0645r10.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# end | ||
* format[meta header] | ||
* function[meta id-type] | ||
* std[meta namespace] | ||
* basic_format_parse_context[meta class] | ||
* cpp20[meta cpp] | ||
|
||
```cpp | ||
constexpr const_iterator end() const noexcept; | ||
``` | ||
|
||
## 概要 | ||
書式文字列の末尾を指すイテレータを取得する。 | ||
|
||
|
||
## 効果 | ||
メンバ変数として保持している、書式文字列の末尾を指すイテレータを返す。 | ||
|
||
|
||
## 例 | ||
### 基本的な使い方 | ||
```cpp example | ||
#include <iostream> | ||
#include <format> | ||
|
||
enum color { red, green, blue }; | ||
|
||
const char* color_names[] = { "red", "green", "blue" }; | ||
|
||
template<> | ||
class std::formatter<color> { | ||
bool enable_quote = false; | ||
public: | ||
constexpr auto parse(std::format_parse_context& ctx) { | ||
auto it = ctx.begin(); | ||
if (it == ctx.end()) { | ||
return it; | ||
} | ||
|
||
if (*it == '%') { | ||
enable_quote = true; | ||
++it; | ||
} | ||
return it; | ||
} | ||
|
||
auto format(color c, std::format_context& ctx) const { | ||
if (enable_quote) { | ||
return std::format_to(ctx.out(), "\"{}\"", color_names[c]); | ||
} | ||
else { | ||
return std::format_to(ctx.out(), "{}", color_names[c]); | ||
} | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
std::cout << std::format("{:%}", red) << std::endl; | ||
} | ||
``` | ||
* ctx.end()[color ff0000] | ||
* ctx.begin()[link begin.md] | ||
* std::format[link /reference/format/format.md] | ||
* std::format_to[link /reference/format/format_to.md] | ||
* std::format_context[link /reference/format/basic_format_context.md] | ||
* ctx.out()[link /reference/format/basic_format_context/out.md] | ||
|
||
#### 出力 | ||
``` | ||
"red" | ||
``` | ||
|
||
|
||
## バージョン | ||
### 言語 | ||
- C++20 | ||
|
||
### 処理系 | ||
- [Clang](/implementation.md#clang): ?? | ||
- [GCC](/implementation.md#gcc): 13 | ||
- [Visual C++](/implementation.md#visual_cpp): ?? | ||
|
||
## 参照 | ||
|
||
- [P0645R10 Text Formatting](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0645r10.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters