Skip to content

Commit ccb6529

Browse files
fmtlib replaced with std::format (#22)
* fmtlib replaced with std::format * write join_with() * replace instances of print with println --------- Co-authored-by: Ren Tatsumoto <tatsu@autistici.org>
1 parent 01b5adc commit ccb6529

11 files changed

+108
-74
lines changed

src/anki_search.cpp

+25-25
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ auto make_find_cards_request_str(search_params const& params) -> std::string
167167
std::string query{ params.gd_word };
168168

169169
if (not params.field_name.empty()) {
170-
query = fmt::format("\"{}:*{}*\"", params.field_name, query);
170+
query = std::format("\"{}:*{}*\"", params.field_name, query);
171171
}
172172
if (not params.deck_name.empty()) {
173-
query = fmt::format("\"deck:{}\" {}", params.deck_name, query);
173+
query = std::format("\"deck:{}\" {}", params.deck_name, query);
174174
}
175175

176176
request["params"]["query"] = query;
@@ -261,20 +261,20 @@ auto get_note_tags(uint64_t const nid) -> std::string
261261
raise_if(not obj["error"].is_null(), "Error getting data from AnkiConnect.");
262262
std::string html;
263263
for (std::string const tag_name: obj["result"]) {
264-
html += fmt::format(R"EOF(<a class="gd-tag-link" href="ankisearch:tag:{}">{}</a>)EOF", tag_name, tag_name);
264+
html += std::format(R"EOF(<a class="gd-tag-link" href="ankisearch:tag:{}">{}</a>)EOF", tag_name, tag_name);
265265
}
266266
return html;
267267
}
268268

269269
void print_table_header(search_params const& params)
270270
{
271271
// Print the first row (header) that contains <th></th> tags, starting with Card ID.
272-
fmt::print("<tr>");
273-
fmt::print("<th>Card ID</th>");
274-
fmt::print("<th>Deck name</th>");
275-
for (auto const& field: params.show_fields) { fmt::print("<th>{}</th>", field); }
276-
fmt::print("<th>Tags</th>");
277-
fmt::print("</tr>\n");
272+
gd::print("<tr>");
273+
gd::print("<th>Card ID</th>");
274+
gd::print("<th>Deck name</th>");
275+
for (auto const& field: params.show_fields) { gd::print("<th>{}</th>", field); }
276+
gd::print("<th>Tags</th>");
277+
gd::println("</tr>");
278278
}
279279

280280
auto card_json_to_obj(nlohmann::json const& card_json) -> card_info
@@ -303,47 +303,47 @@ auto gd_format(std::string const& field_content, std::string const& media_dir_pa
303303
static std::regex const img_re{ "(<img[^<>]*src=\")" };
304304
static std::regex const any_undesirables{ R"EOF(\[sound:|\]|<[^<>]+>|["'.,!?]+|…|。|、|!|?| |・|~|\(|\))EOF" };
305305
auto const link_content = strtrim(std::regex_replace(field_content, any_undesirables, " "));
306-
auto const link_text = std::regex_replace(field_content, img_re, fmt::format("$1file://{}/", media_dir_path));
307-
return link_content.empty() ? link_text : fmt::format("<a href=\"ankisearch:{}\">{}</a>", link_content, link_text);
306+
auto const link_text = std::regex_replace(field_content, img_re, std::format("$1file://{}/", media_dir_path));
307+
return link_content.empty() ? link_text : std::format("<a href=\"ankisearch:{}\">{}</a>", link_content, link_text);
308308
}
309309

310310
void print_cards_info(search_params const& params)
311311
{
312312
auto const cids = find_cids(params);
313313
if (cids.empty()) {
314-
return fmt::print("No cards found.\n");
314+
return gd::println("No cards found.");
315315
}
316316
auto const media_dir_path = fetch_media_dir_path();
317-
fmt::print("<div class=\"gd-table-wrap\">");
318-
fmt::print("<table class=\"gd-ankisearch-table\">\n");
317+
gd::print("<div class=\"gd-table-wrap\">");
318+
gd::println("<table class=\"gd-ankisearch-table\">");
319319
print_table_header(params);
320320
for (auto const& card: get_cids_info(cids) | std::views::transform(card_json_to_obj)) {
321-
fmt::print("<tr class=\"{}\">", determine_card_class(card.queue, card.type));
322-
fmt::print("<td><a href=\"ankisearch:cid:{}\">{}</a></td>", card.id, card.id);
323-
fmt::print("<td>{}</td>", card.deck_name);
321+
gd::print("<tr class=\"{}\">", determine_card_class(card.queue, card.type));
322+
gd::print("<td><a href=\"ankisearch:cid:{}\">{}</a></td>", card.id, card.id);
323+
gd::print("<td>{}</td>", card.deck_name);
324324
for (auto const& field_name: params.show_fields) {
325-
fmt::print(
325+
gd::print(
326326
"<td>{}</td>",
327327
(card.fields.contains(field_name) and not card.fields.at(field_name).empty()
328328
? gd_format(card.fields.at(field_name), media_dir_path)
329329
: "Not present")
330330
);
331331
}
332-
fmt::print("<td>{}</td>\n", get_note_tags(card.nid));
333-
fmt::print("</tr>\n");
332+
gd::println("<td>{}</td>", get_note_tags(card.nid));
333+
gd::println("</tr>");
334334
}
335-
fmt::print("</table>");
336-
fmt::print("</div>\n"); // gd-table-wrap
337-
fmt::print("{}\n", css_style);
335+
gd::print("</table>");
336+
gd::println("</div>"); // gd-table-wrap
337+
gd::println("{}", css_style);
338338
}
339339

340340
void search_anki_cards(std::span<std::string_view const> const args)
341341
{
342342
try {
343343
print_cards_info(fill_args<search_params>(args));
344344
} catch (gd::help_requested const& ex) {
345-
fmt::print(help_text);
345+
gd::print(help_text);
346346
} catch (gd::runtime_error const& ex) {
347-
fmt::print("{}\n", ex.what());
347+
gd::println("{}", ex.what());
348348
}
349349
}

src/echo.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ void print_css(stroke_order_params const& params)
6969
}}
7070
</style>
7171
)EOF";
72-
fmt::print(css, this_pid, params.font_size, params.font_family);
72+
gd::print(css, this_pid, params.font_size, params.font_family);
7373
}
7474

7575
void print_with_stroke_order(stroke_order_params const& params)
7676
{
7777
if (params.gd_word.length() <= params.max_len) {
78-
fmt::print("<div class=\"gd_echo_{}\">{}</div>\n", this_pid, params.gd_word);
78+
gd::println("<div class=\"gd_echo_{}\">{}</div>", this_pid, params.gd_word);
7979
print_css(params);
8080
}
8181
}
@@ -85,8 +85,8 @@ void stroke_order(std::span<std::string_view const> const args)
8585
try {
8686
print_with_stroke_order(fill_args<stroke_order_params>(args));
8787
} catch (gd::help_requested const& ex) {
88-
fmt::print(help_text);
88+
gd::print(help_text);
8989
} catch (gd::runtime_error const& ex) {
90-
fmt::print("{}\n", ex.what());
90+
gd::println("{}", ex.what());
9191
}
9292
}

src/images.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ void fetch_images(images_params const& params)
8484
static std::regex const img_re("<img[^<>]*class=\"mimg[^<>]*>");
8585
auto images_begin = std::sregex_iterator(std::begin(r.text), std::end(r.text), img_re);
8686
auto images_end = std::sregex_iterator();
87-
fmt::print("<div class=\"gallery\">\n");
87+
gd::println("<div class=\"gallery\">");
8888
for (auto const& match: std::ranges::subrange(images_begin, images_end) | std::views::take(5)) {
89-
fmt::print("{}\n", match.str());
89+
gd::println("{}", match.str());
9090
}
91-
fmt::print("</div>\n");
92-
fmt::print("{}\n", css_style);
91+
gd::println("</div>");
92+
gd::println("{}", css_style);
9393
}
9494

9595
void images(std::span<std::string_view const> const args)
9696
{
9797
try {
9898
fetch_images(fill_args<images_params>(args));
9999
} catch (gd::help_requested const& ex) {
100-
fmt::print(help_text);
100+
gd::print(help_text);
101101
} catch (gd::runtime_error const& ex) {
102-
fmt::print("{}\n", ex.what());
102+
gd::println("{}", ex.what());
103103
}
104104
}

src/kana_conv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ auto unicode_char_byte_len(char const& ch) -> CharByteLen
3939
// Other Unicode
4040
return CharByteLen::FOUR;
4141
}
42-
throw gd::runtime_error{ fmt::format("Can't recognize byte: '{:x}'.", ch) };
42+
throw gd::runtime_error{ std::format("Can't recognize byte: '{:x}'.", ch) };
4343
}
4444

4545
auto create_map(std::string_view from, std::string_view to) -> KanaConvMap

src/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "massif.h"
2424
#include "mecab_split.h"
2525
#include "precompiled.h"
26+
#include "util.h"
2627

2728
static constexpr std::string_view help_text = R"EOF(usage: {} ACTION [OPTIONS]
2829
A set of helpful programs to enhance GoldenDict for immersion learning.
@@ -47,12 +48,12 @@ gd-ankisearch --deck-name Mining %GDWORD%
4748

4849
auto get_help_str(std::string_view program_name) -> std::string
4950
{
50-
return fmt::format(help_text, program_name);
51+
return std::format(help_text, program_name);
5152
}
5253

5354
auto print_help(std::string_view const program_name) -> void
5455
{
55-
fmt::print("{}", get_help_str(program_name));
56+
gd::print("{}", get_help_str(program_name));
5657
}
5758

5859
auto base_name(auto file_path) -> std::string

src/marisa_split.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ void lookup_words(marisa_params params)
181181
marisa::Agent agent;
182182

183183
std::ifstream file{ params.path_to_dic };
184-
raise_if(not file.good(), fmt::format(R"(Error. The dictionary file "{}" does not exist.)", params.path_to_dic));
184+
raise_if(not file.good(), std::format(R"(Error. The dictionary file "{}" does not exist.)", params.path_to_dic));
185185
trie.load(params.path_to_dic.c_str());
186186

187-
fmt::println(R"(<div class="gd-marisa">)");
187+
gd::println(R"(<div class="gd-marisa">)");
188188
std::ptrdiff_t pos_in_gd_word{ 0 };
189189
std::vector<JpSet> alternatives{};
190190
alternatives.reserve(20);
@@ -205,7 +205,7 @@ void lookup_words(marisa_params params)
205205
pos_in_gd_word -= static_cast<std::ptrdiff_t>(uni_char.length());
206206
}
207207

208-
fmt::print(
208+
gd::print(
209209
R"(<a class="{}" href="bword:{}">{}</a>)",
210210
(pos_in_gd_word > 0 ? "gd-headword" : "gd-word"),
211211
bword,
@@ -215,32 +215,32 @@ void lookup_words(marisa_params params)
215215
}
216216

217217
// Show available entries for other substrings.
218-
fmt::println(R"(<div class="alternatives">)");
218+
gd::println(R"(<div class="alternatives">)");
219219
for (auto const& group: alternatives | std::views::filter(&JpSet::size)) {
220-
fmt::println("<ul>");
220+
gd::println("<ul>");
221221
for (auto const& word: group) {
222-
fmt::println(
222+
gd::println(
223223
R"(<li><a class="{}" href="bword:{}">{}</a></li>)",
224224
(word == params.gd_word ? "gd-headword" : ""),
225225
word,
226226
word
227227
);
228228
}
229-
fmt::println("</ul>"); // close ul
229+
gd::println("</ul>"); // close ul
230230
}
231-
fmt::println("</div>"); // close div.alternatives
231+
gd::println("</div>"); // close div.alternatives
232232

233-
fmt::println("</div>"); // close div.gd-marisa
234-
fmt::println("{}", css_style);
233+
gd::println("</div>"); // close div.gd-marisa
234+
gd::println("{}", css_style);
235235
}
236236

237237
void marisa_split(std::span<std::string_view const> const args)
238238
{
239239
try {
240240
lookup_words(fill_args<marisa_params>(args));
241241
} catch (gd::help_requested const& ex) {
242-
fmt::println(help_text);
242+
gd::println(help_text);
243243
} catch (gd::runtime_error const& ex) {
244-
fmt::println("{}", ex.what());
244+
gd::println("{}", ex.what());
245245
}
246246
}

src/massif.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ struct massif_params
7474
void fetch_massif_examples(massif_params const& params)
7575
{
7676
cpr::Response const r = cpr::Get(
77-
cpr::Url{ fmt::format("https://massif.la/ja/search?q={}", params.gd_word) },
77+
cpr::Url{ std::format("https://massif.la/ja/search?q={}", params.gd_word) },
7878
cpr::Timeout{ params.max_time },
7979
cpr::VerifySsl{ false }
8080
);
8181
raise_if(r.status_code != 200, "Couldn't connect to Massif.");
82-
fmt::print("<ul class=\"gd-massif\">\n");
82+
gd::println("<ul class=\"gd-massif\">");
8383
for (auto const& line:
8484
r.text //
8585
| std::views::split('\n') //
@@ -88,19 +88,19 @@ void fetch_massif_examples(massif_params const& params)
8888
return not str_view.contains("<li class=\"text-japanese\">");
8989
})
9090
| std::views::take_while([](auto const str_view) { return not str_view.contains("</ul>"); })) {
91-
fmt::print("{}\n", line);
91+
gd::println("{}", line);
9292
}
93-
fmt::print("</ul>\n");
94-
fmt::print("{}\n", css_style);
93+
gd::println("</ul>");
94+
gd::println("{}", css_style);
9595
}
9696

9797
void massif(std::span<std::string_view const> const args)
9898
{
9999
try {
100100
fetch_massif_examples(fill_args<massif_params>(args));
101101
} catch (gd::help_requested const& ex) {
102-
fmt::print(help_text);
102+
gd::print(help_text);
103103
} catch (gd::runtime_error const& ex) {
104-
fmt::print("{}\n", ex.what());
104+
gd::println("{}", ex.what());
105105
}
106106
}

src/mecab_split.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct mecab_params
121121
} else if (key == "--user-dict") {
122122
user_dict = value;
123123
} else {
124-
throw gd::runtime_error(std::string(fmt::format("Unknown argument name: {}", key)));
124+
throw gd::runtime_error(std::string(std::format("Unknown argument name: {}", key)));
125125
}
126126
}
127127
};
@@ -174,25 +174,26 @@ void lookup_words(mecab_params params)
174174
}
175175

176176
std::string result = tagger->parse(params.gd_sentence.c_str());
177-
result = replace_all(result, fmt::format(">{}<", params.gd_word), fmt::format("><b>{}</b><", params.gd_word));
178-
fmt::println(R"EOF(<div class="gd-mecab">{}</div>)EOF", result);
179-
fmt::println("{}", css_style);
177+
result = replace_all(result, std::format(">{}<", params.gd_word), std::format("><b>{}</b><", params.gd_word));
178+
gd::println(R"EOF(<div class="gd-mecab">{}</div>)EOF", result);
179+
gd::println("{}", css_style);
180180

181181
// debug info, not shown in GD.
182-
fmt::println(R"EOF(<div style="display: none;">)EOF");
183-
fmt::println("dicdir: {}", params.dic_dir.string());
184-
fmt::println("userdic: {}", params.user_dict.string());
185-
fmt::println("mecab args: {}", args);
186-
fmt::println(R"EOF(</div>)EOF");
182+
gd::println(R"EOF(<div style="display: none;">)EOF");
183+
gd::println("dicdir: {}", params.dic_dir.string());
184+
gd::println("userdic: {}", params.user_dict.string());
185+
186+
gd::println("mecab args: [{}]", join_with(args, ", "));
187+
gd::println(R"EOF(</div>)EOF");
187188
}
188189

189190
void mecab_split(std::span<std::string_view const> const args)
190191
{
191192
try {
192193
lookup_words(fill_args<mecab_params>(args));
193194
} catch (gd::help_requested const& ex) {
194-
fmt::println(help_text);
195+
gd::println(help_text);
195196
} catch (gd::runtime_error const& ex) {
196-
fmt::println("{}", ex.what());
197+
gd::println("{}", ex.what());
197198
}
198199
}

src/precompiled.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chrono>
66
#include <concepts>
77
#include <filesystem>
8+
#include <format>
89
#include <iomanip>
910
#include <iostream>
1011
#include <iterator>
@@ -27,8 +28,7 @@
2728

2829
// Other
2930
#include <cpr/cpr.h>
30-
#include <fmt/core.h>
31-
#include <fmt/ranges.h>
31+
3232
#include <marisa/trie.h>
3333
#include <mecab.h>
3434
#include <nlohmann/json.hpp>

0 commit comments

Comments
 (0)