aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.ixx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2019-08-21 12:10:34 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2019-08-21 12:10:34 +0200
commit44b929bb1611ce98e926b2f4846565338f344d48 (patch)
tree73f9a7acc91b569dfa8ae70710b1ea4af93bac9f /libbutl/utility.ixx
parent94e98b452130115fd76aea12b978d9e0bfd77a88 (diff)
Add sanitize_identifier() utility function
Diffstat (limited to 'libbutl/utility.ixx')
-rw-r--r--libbutl/utility.ixx25
1 files changed, 25 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)
{