aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-30 15:21:47 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-30 15:21:47 +0000
commit7de2869db982a34456318d78a815f74fe1c4a4e3 (patch)
tree29c8dbf03621c0e290d0e81506495a5d0c67aa65
parent3596a3262e802c59f9c9f0702cc2e26e8ee927ce (diff)
downloadpaludis-7de2869db982a34456318d78a815f74fe1c4a4e3.tar.gz
paludis-7de2869db982a34456318d78a815f74fe1c4a4e3.tar.xz
Don't explode horribly on strip_trailing_string("", "blah")
-rw-r--r--paludis/strip.cc71
1 files changed, 53 insertions, 18 deletions
diff --git a/paludis/strip.cc b/paludis/strip.cc
index d7aa8b4..92fb24c 100644
--- a/paludis/strip.cc
+++ b/paludis/strip.cc
@@ -18,41 +18,76 @@
*/
#include "strip.hh"
+#include "exception.hh"
namespace paludis
{
std::string strip_leading_string(const std::string & s, const std::string & prefix)
{
- if (0 == s.compare(0, prefix.length(), prefix))
- return s.substr(prefix.length());
- else
- return s;
+ try
+ {
+ if (0 == s.compare(0, prefix.length(), prefix))
+ return s.substr(prefix.length());
+ else
+ return s;
+ }
+ catch (const std::exception & e)
+ {
+ throw InternalError(PALUDIS_HERE, "Caught unexpected exception " +
+ stringify(e.what()));
+ }
}
std::string strip_leading(const std::string & s, const std::string & remove)
{
- std::string::size_type p(s.find_first_not_of(remove));
- if (std::string::npos == p)
- return std::string();
- else
- return s.substr(p);
+ try
+ {
+ std::string::size_type p(s.find_first_not_of(remove));
+ if (std::string::npos == p)
+ return std::string();
+ else
+ return s.substr(p);
+ }
+ catch (const std::exception & e)
+ {
+ throw InternalError(PALUDIS_HERE, "Caught unexpected exception " +
+ stringify(e.what()));
+ }
}
std::string strip_trailing_string(const std::string & s, const std::string & suffix)
{
- if (0 == s.compare(s.length() - suffix.length(), suffix.length(), suffix))
- return s.substr(0, s.length() - suffix.length());
- else
- return s;
+ try
+ {
+ if (suffix.length() < s.length())
+ return s;
+ else if (0 == s.compare(s.length() - suffix.length(), suffix.length(), suffix))
+ return s.substr(0, s.length() - suffix.length());
+ else
+ return s;
+ }
+ catch (const std::exception & e)
+ {
+ throw InternalError(PALUDIS_HERE, "Caught unexpected exception " +
+ stringify(e.what()) + " with s='" + s + "', suffix='" + suffix + "'");
+ }
}
std::string strip_trailing(const std::string & s, const std::string & remove)
{
- std::string::size_type p(s.find_last_not_of(remove));
- if (std::string::npos == p)
- return std::string();
- else
- return s.substr(0, p + 1);
+ try
+ {
+ std::string::size_type p(s.find_last_not_of(remove));
+ if (std::string::npos == p)
+ return std::string();
+ else
+ return s.substr(0, p + 1);
+ }
+ catch (const std::exception & e)
+ {
+ throw InternalError(PALUDIS_HERE, "Caught unexpected exception " +
+ stringify(e.what()));
+ }
}
}