Skip to content

Commit 141b484

Browse files
committed
📝 Update README.md
1 parent 7efa8c6 commit 141b484

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
# String Template in C++
22
[![Linux build status](https://github.com/acd1034/cpp-string-template/actions/workflows/linux-build.yml/badge.svg)](https://github.com/acd1034/cpp-string-template/actions/workflows/linux-build.yml)
3+
4+
## Quick example
5+
```cpp
6+
#include <iostream>
7+
#include <string_view>
8+
#include <unordered_map>
9+
#include <strtpl/string_template.hpp>
10+
11+
int main() {
12+
std::unordered_map<std::string_view, std::string_view> map{
13+
{"who", "Alice"},
14+
{"what", "bananas"},
15+
{"verb", "run"},
16+
};
17+
{ // '$' に続く単語を発見した場合、その単語を map の key として得られる値で置き換えます。
18+
std::cout << strtpl::substitute("$who likes $what.", map) << std::endl;
19+
// → Alice likes bananas.
20+
}
21+
{ // '$' と単語の後にアルファベットが続く場合は、単語を "{}" で囲みます。
22+
std::cout << strtpl::substitute("$who ${verb}s fast.", map) << std::endl;
23+
// → Alice runs fast.
24+
}
25+
// '$' に続く単語が map に含まれない場合、例外を送出します。
26+
try {
27+
std::cout << strtpl::substitute("$who likes $where.", map) << std::endl;
28+
} catch (const std::exception& e) {
29+
std::cout << e.what() << std::endl;
30+
// → strtpl::at: index out of range
31+
}
32+
// '$' に続く文字が無効な場合も、例外を送出します。エラーメッセージにはエラーが生じた文字の行番号と列番号を含みます。
33+
try {
34+
std::cout << strtpl::substitute("$who likes $.", map) << std::endl;
35+
} catch (const std::exception& e) {
36+
std::cout << e.what() << std::endl;
37+
// → Invalid placeholder in string: line 1, col 12
38+
}
39+
}
40+
```
41+
42+
## Note
43+
- [`include/strtpl/string_template.hpp`](https://github.com/acd1034/cpp-string-template/blob/main/include/strtpl/string_template.hpp)
44+
→ 文字列置換を行う関数 (`strtpl::substitute` および `wchar_t` に対応した `strtpl::wsubstitute`) が実装されています。
45+
- [`include/strtpl/regex.hpp`](https://github.com/acd1034/cpp-string-template/blob/main/include/strtpl/regex.hpp),
46+
[`include/strtpl/trailing_view.hpp`](https://github.com/acd1034/cpp-string-template/blob/main/include/strtpl/trailing_view.hpp)
47+
→ std の正規表現ライブラリを range のように扱うためのモジュールで、まだ試験実装です。

0 commit comments

Comments
 (0)