diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2017-06-21 13:03:56 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2017-06-21 13:03:56 +0300 |
commit | 5d50c0499b30650deafc291a3872a386d08a3200 (patch) | |
tree | 4e0936d2d51fa7d92b3fb7c4b05bfc099bbf0a58 /libbutl/regex.cxx | |
parent | 273ce32a9a9c89410d4ab396c1bbdfb9a5024fa8 (diff) |
Add regex utility functions
Diffstat (limited to 'libbutl/regex.cxx')
-rw-r--r-- | libbutl/regex.cxx | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libbutl/regex.cxx b/libbutl/regex.cxx new file mode 100644 index 0000000..4e2e26f --- /dev/null +++ b/libbutl/regex.cxx @@ -0,0 +1,45 @@ +// file : libbutl/regex.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include <libbutl/regex.hxx> + +#if defined(_MSC_VER) && _MSC_VER <= 1910 +# include <cstring> // strstr() +#endif + +#include <ostream> +#include <sstream> +#include <stdexcept> // runtime_error + +#include <libbutl/utility.hxx> // operator<<(ostream, exception) + +namespace std +{ + // Currently libstdc++ just returns the name of the exception (bug #67361). + // So we check that the description contains at least one space character. + // + // While VC's description is meaningful, it has an undesired prefix that + // resembles the following: 'regex_error(error_badrepeat): '. So we skip it. + // + ostream& + operator<< (ostream& o, const regex_error& e) + { + const char* d (e.what ()); + +#if defined(_MSC_VER) && _MSC_VER <= 1910 + const char* rd (strstr (d, "): ")); + if (rd != nullptr) + d = rd + 3; +#endif + + ostringstream os; + os << runtime_error (d); // Sanitize the description. + + string s (os.str ()); + if (s.find (' ') != string::npos) + o << ": " << s; + + return o; + } +} |