Skip to content

Commit e1892bf

Browse files
committed
Replace std::stoi with more descriptive wrapper
Exceptions from GCC's std::stoi are not very descriptive. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78710
1 parent 0d06c0e commit e1892bf

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/issues.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ auto parse_date(std::istream & temp) -> std::chrono::year_month_day {
5757
auto report_date_file_last_modified(std::filesystem::path const & filename, lwg::metadata const& meta) -> std::chrono::year_month_day {
5858
using namespace std::chrono;
5959
system_clock::time_point t;
60-
int id = std::stoi(filename.filename().stem().native().substr(5));
60+
int id = lwg::stoi(filename.filename().stem().native().substr(5));
6161
// Use the Git commit date of the file if available.
6262
if (auto it = meta.git_commit_times.find(id); it != meta.git_commit_times.end())
6363
t = system_clock::from_time_t(it->second);
@@ -161,7 +161,7 @@ auto lwg::parse_issue_from_file(std::string tx, std::string const & filename,
161161
auto num = tx.substr(k, l-k);
162162
if (!filename.ends_with(std::format("issue{:0>4}.xml", num)))
163163
std::cerr << "warning: issue number " << num << " in " << filename << " does not match issue number\n";
164-
is.num = std::stoi(num);
164+
is.num = lwg::stoi(num);
165165

166166
// Get issue status
167167
match = "status=\"";
@@ -270,7 +270,7 @@ auto lwg::parse_issue_from_file(std::string tx, std::string const & filename,
270270
if (l == std::string::npos) {
271271
throw bad_issue_file{filename, "Corrupt 'priority' element: no closing tag"};
272272
}
273-
is.priority = std::stoi(tx.substr(k, l-k));
273+
is.priority = lwg::stoi(tx.substr(k, l-k));
274274
}
275275

276276
// Trim text to <discussion>

src/issues.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ auto parse_issue_from_file(std::string file_contents, std::string const & filena
4848
//
4949
// The filename is passed only to improve diagnostics.
5050

51+
52+
inline int stoi(const std::string& s)
53+
{
54+
std::size_t idx = 0;
55+
try
56+
{
57+
return std::stoi(s, &idx);
58+
}
59+
catch (const std::out_of_range&)
60+
{
61+
throw std::out_of_range("std::stoi: out of range: \"" + s + '"');
62+
}
63+
catch (const std::invalid_argument&)
64+
{
65+
throw std::invalid_argument("std::stoi: invalid argument: \"" + s + '"');
66+
}
67+
}
68+
5169
} // close namespace lwg
5270

5371
#endif // INCLUDE_LWG_ISSUES_H

src/lists.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ auto read_issues_from_toc(std::string const & s) -> std::vector<std::tuple<int,
132132
if (i == std::string::npos) {
133133
break;
134134
}
135-
int num = std::stoi(extract_link_text("number"));
135+
int num = lwg::stoi(extract_link_text("number"));
136136
i += 4;
137137
std::string status = extract_link_text("status");
138138
if (status == "(i)") {

0 commit comments

Comments
 (0)