aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'libbutl/utility.cxx')
-rw-r--r--libbutl/utility.cxx119
1 files changed, 88 insertions, 31 deletions
diff --git a/libbutl/utility.cxx b/libbutl/utility.cxx
index bbeafd2..b03a8f8 100644
--- a/libbutl/utility.cxx
+++ b/libbutl/utility.cxx
@@ -1,44 +1,23 @@
// file : libbutl/utility.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
-#ifndef __cpp_modules_ts
-#include <libbutl/utility.mxx>
-#endif
+#include <libbutl/utility.hxx>
#ifdef _WIN32
#include <libbutl/win32-utility.hxx>
#endif
-#include <stdlib.h> // setenv(), unsetenv(), _putenv()
-
-#ifndef __cpp_lib_modules_ts
-#include <string>
-#include <cstddef>
-#include <utility>
+#include <stdlib.h> // getenv(), setenv(), unsetenv(), _putenv()
+#include <cstring> // strncmp(), strlen()
#include <ostream>
#include <type_traits> // enable_if, is_base_of
#include <system_error>
-#endif
#include <libbutl/ft/lang.hxx>
#include <libbutl/ft/exception.hxx>
-#ifdef __cpp_modules_ts
-module butl.utility;
-
-// Only imports additional to interface.
-#ifdef __clang__
-#ifdef __cpp_lib_modules_ts
-import std.core;
-import std.io;
-#endif
-#endif
-
-import butl.utf8;
-#else
-#include <libbutl/utf8.mxx>
-#endif
+#include <libbutl/utf8.hxx>
namespace butl
{
@@ -192,13 +171,42 @@ namespace butl
for (; i != n && ws (l[i]); ++i) ;
for (; n != i && ws (l[n - 1]); --n) ;
- if (i != 0)
+ if (n != l.size ()) l.resize (n);
+ if (i != 0) l.erase (0, i);
+
+ return l;
+ }
+
+ string&
+ trim_left (string& l)
+ {
+ auto ws = [] (char c )
{
- string s (l, i, n - i);
- l.swap (s);
- }
- else if (n != l.size ())
- l.resize (n);
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r';
+ };
+
+ size_t i (0), n (l.size ());
+
+ for (; i != n && ws (l[i]); ++i) ;
+
+ if (i != 0) l.erase (0, i);
+
+ return l;
+ }
+
+ string&
+ trim_right (string& l)
+ {
+ auto ws = [] (char c )
+ {
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r';
+ };
+
+ size_t i (0), n (l.size ());
+
+ for (; n != i && ws (l[n - 1]); --n) ;
+
+ if (n != l.size ()) l.resize (n);
return l;
}
@@ -332,6 +340,55 @@ namespace butl
s.resize (d - s.begin ());
}
+#ifdef __cpp_thread_local
+ thread_local
+#else
+ __thread
+#endif
+ const char* const* thread_env_ = nullptr;
+
+#ifdef _WIN32
+ const char* const*
+ thread_env () {return thread_env_;}
+
+ void
+ thread_env (const char* const* v) {thread_env_ = v;}
+#endif
+
+ optional<std::string>
+ getenv (const char* name)
+ {
+ if (const char* const* vs = thread_env_)
+ {
+ size_t n (strlen (name));
+
+ for (; *vs != nullptr; ++vs)
+ {
+ const char* v (*vs);
+
+ // Note that on Windows variable names are case-insensitive.
+ //
+#ifdef _WIN32
+ if (icasecmp (name, v, n) == 0)
+#else
+ if (strncmp (name, v, n) == 0)
+#endif
+ {
+ switch (v[n])
+ {
+ case '=': return string (v + n + 1);
+ case '\0': return nullopt;
+ }
+ }
+ }
+ }
+
+ if (const char* r = ::getenv (name))
+ return std::string (r);
+
+ return nullopt;
+ }
+
void
setenv (const string& name, const string& value)
{