aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/utility.ixx25
-rw-r--r--libbutl/utility.mxx9
2 files changed, 34 insertions, 0 deletions
diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx
index 04fb161..8f37242 100644
--- a/libbutl/utility.ixx
+++ b/libbutl/utility.ixx
@@ -4,6 +4,7 @@
#ifndef __cpp_lib_modules_ts
#include <cstdlib> // getenv()
+#include <algorithm>
#endif
namespace butl
@@ -192,6 +193,30 @@ namespace butl
return e - b;
}
+ inline std::string&
+ sanitize_identifier (std::string& s)
+ {
+ std::for_each (s.begin (), s.end (), [] (char& c)
+ {
+ if (!alnum (c) && c != '_')
+ c = '_';
+ });
+ return s;
+ }
+
+ inline std::string
+ sanitize_identifier (std::string&& s)
+ {
+ sanitize_identifier (s);
+ return std::move (s);
+ }
+
+ inline std::string
+ sanitize_identifier (const std::string& s)
+ {
+ return sanitize_identifier (std::string (s));
+ }
+
inline bool
eof (std::istream& is)
{
diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx
index 8085e79..03fb89e 100644
--- a/libbutl/utility.mxx
+++ b/libbutl/utility.mxx
@@ -186,6 +186,15 @@ LIBBUTL_MODEXPORT namespace butl
next_word (const std::string&, std::size_t n, std::size_t& b, std::size_t& e,
char d1 = ' ', char d2 = '\0');
+ // Sanitize a string to only contain characters valid in an identifier
+ // (ASCII alphanumeric plus `_`) replacing all others with `_`.
+ //
+ // Note that it doesn't make sure the first character is not a digit.
+ //
+ std::string& sanitize_identifier (std::string&);
+ std::string sanitize_identifier (std::string&&);
+ std::string sanitize_identifier (const std::string&);
+
// If an input stream is in a failed state, then return true if this is
// because of the eof and throw istream::failure otherwise. If the stream
// is not in a failed state, return false. This helper function is normally