From 44b929bb1611ce98e926b2f4846565338f344d48 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 21 Aug 2019 12:10:34 +0200 Subject: Add sanitize_identifier() utility function --- libbutl/utility.ixx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'libbutl/utility.ixx') 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 // getenv() +#include #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) { -- cgit v1.1