aboutsummaryrefslogtreecommitdiff
path: root/butl/utility.ixx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2016-08-08 00:49:04 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2016-08-10 14:53:04 +0300
commitaa0370b08ea8a1ad679a746c7be21a874f264fb6 (patch)
tree653f6b1d8ed888691f5af5a1e57516050d71d3cb /butl/utility.ixx
parentde07f993a54f7443db685798ae9225bbd49f0231 (diff)
Add ucase(), lcase(), casecmp(), alpha(), digit(), alnum()
Diffstat (limited to 'butl/utility.ixx')
-rw-r--r--butl/utility.ixx136
1 files changed, 136 insertions, 0 deletions
diff --git a/butl/utility.ixx b/butl/utility.ixx
new file mode 100644
index 0000000..b0d83c9
--- /dev/null
+++ b/butl/utility.ixx
@@ -0,0 +1,136 @@
+// file : butl/utility.ixx -*- C++ -*-
+// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef _WIN32
+# include <strings.h> // strcasecmp(), strncasecmp()
+#else
+# include <string.h> // _stricmp(), _strnicmp()
+#endif
+
+#include <cctype> // toupper(), tolower(), isalpha(), isdigit(), isalnum()
+
+namespace butl
+{
+ inline char
+ ucase (char c)
+ {
+ return std::toupper (c);
+ }
+
+ 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 std::string&
+ ucase (std::string& s)
+ {
+ if (size_t n = s.size ())
+ {
+ s.front () = s.front (); // Force copy in CoW.
+ ucase (const_cast<char*> (s.data ()), n);
+ }
+ return s;
+ }
+
+ inline void
+ ucase (char* s, std::size_t n)
+ {
+ for (const char* e (s + n); s != e; ++s)
+ *s = ucase (*s);
+ }
+
+ inline char
+ lcase (char c)
+ {
+ return std::tolower (c);
+ }
+
+ 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 std::string&
+ lcase (std::string& s)
+ {
+ if (size_t n = s.size ())
+ {
+ s.front () = s.front (); // Force copy in CoW.
+ lcase (const_cast<char*> (s.data ()), n);
+ }
+ return s;
+ }
+
+ inline void
+ lcase (char* s, std::size_t n)
+ {
+ for (const char* e (s + n); s != e; ++s)
+ *s = lcase (*s);
+ }
+
+ 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 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 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 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);
+ }
+}