From 61377c582e0f2675baa5f5e6e30a35d1a4164b33 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 1 May 2017 16:08:43 +0300 Subject: Add hxx extension for headers and lib prefix for library dir --- libbutl/utility.ixx | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 libbutl/utility.ixx (limited to 'libbutl/utility.ixx') diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx new file mode 100644 index 0000000..e45a729 --- /dev/null +++ b/libbutl/utility.ixx @@ -0,0 +1,136 @@ +// file : libbutl/utility.ixx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef _WIN32 +# include // strcasecmp(), strncasecmp() +#else +# include // _stricmp(), _strnicmp() +#endif + +#include // toupper(), tolower(), isalpha(), isdigit(), isalnum() + +namespace butl +{ + inline char + ucase (char c) + { + return std::toupper (c); + } + + inline void + ucase (char* s, std::size_t n) + { + for (const char* e (s + n); s != e; ++s) + *s = ucase (*s); + } + + inline std::string& + ucase (std::string& s) + { + if (size_t n = s.size ()) + { + s.front () = s.front (); // Force copy in CoW. + ucase (const_cast (s.data ()), n); + } + return s; + } + + inline std::string + ucase (const char* s, std::size_t n) + { + std::string r (s, n == std::string::npos ? std::strlen (s) : n); + return ucase (r); + } + + inline std::string + ucase (const std::string& s) + { + return ucase (s.c_str (), s.size ()); + } + + inline char + lcase (char c) + { + return std::tolower (c); + } + + inline void + lcase (char* s, std::size_t n) + { + for (const char* e (s + n); s != e; ++s) + *s = lcase (*s); + } + + inline std::string& + lcase (std::string& s) + { + if (size_t n = s.size ()) + { + s.front () = s.front (); // Force copy in CoW. + lcase (const_cast (s.data ()), n); + } + return s; + } + + inline std::string + lcase (const char* s, std::size_t n) + { + std::string r (s, n == std::string::npos ? std::strlen (s) : n); + return lcase (r); + } + + inline std::string + lcase (const std::string& s) + { + return lcase (s.c_str (), s.size ()); + } + + inline int + casecmp (char l, char r) + { + l = lcase (l); + r = lcase (r); + return l < r ? -1 : (l > r ? 1 : 0); + } + + inline int + casecmp (const char* l, const char* r, std::size_t n) + { +#ifndef _WIN32 + return n == std::string::npos ? strcasecmp (l, r) : strncasecmp (l, r, n); +#else + return n == std::string::npos ? _stricmp (l, r) : _strnicmp (l, r, n); +#endif + } + + inline int + casecmp (const std::string& l, const std::string& r, std::size_t n) + { + return casecmp (l.c_str (), r.c_str (), n); + } + + inline int + casecmp (const std::string& l, const char* r, std::size_t n) + { + return casecmp (l.c_str (), r, n); + } + + inline bool + alpha (char c) + { + return std::isalpha (c); + } + + inline bool + digit (char c) + { + return std::isdigit (c); + } + + inline bool + alnum (char c) + { + return std::isalnum (c); + } +} -- cgit v1.1