diff options
author | Sadie Powell <sadie@witchery.services> | 2024-03-11 13:53:05 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-03-11 19:17:29 +0000 |
commit | 29e7674e56bf2b829bba22def2760d034a76e788 (patch) | |
tree | f40049ba995b03dd7c510d88f9f19db2d2e65a2e /modules/rewrite.cpp | |
parent | e2df7d4d01f8fdb41c49ce8efc462cab005e7d5c (diff) |
Replace convertTo/stringify with non-throwing alternatives.
Having these throw is terrible for ergonomics and there are loads
of places where the exception was either silently ignored or not
handled at all. Having a function which returns an optional and
another that returns a default works a lot better imo.
Diffstat (limited to 'modules/rewrite.cpp')
-rw-r--r-- | modules/rewrite.cpp | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/modules/rewrite.cpp b/modules/rewrite.cpp index 3911b5ddf..008e3e722 100644 --- a/modules/rewrite.cpp +++ b/modules/rewrite.cpp @@ -38,28 +38,35 @@ struct Rewrite final else { int num = -1, end = -1; - try + Anope::string num_str = token.substr(1); + size_t hy = num_str.find('-'); + if (hy == Anope::string::npos) { - Anope::string num_str = token.substr(1); - size_t hy = num_str.find('-'); - if (hy == Anope::string::npos) - { - num = convertTo<int>(num_str); - end = num + 1; - } + auto n = Anope::TryConvert<int>(num_str); + if (!n.has_value()) + continue; + + num = n.value(); + end = num + 1; + } + else + { + auto n = Anope::TryConvert<int>(num_str.substr(0, hy)); + if (!n.has_value()) + continue; + + num = n.value(); + if (hy == num_str.length() - 1) + end = params.size(); else { - num = convertTo<int>(num_str.substr(0, hy)); - if (hy == num_str.length() - 1) - end = params.size(); - else - end = convertTo<int>(num_str.substr(hy + 1)) + 1; + n = Anope::TryConvert<int>(num_str.substr(hy + 1)); + if (!n.has_value()) + continue; + + end = n.value() + 1; } } - catch (const ConvertException &) - { - continue; - } for (int i = num; i < end && static_cast<unsigned>(i) < params.size(); ++i) message += " " + params[i]; |