aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2021-04-20 10:40:37 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2021-04-20 10:40:37 +0200
commit37758a01761331f534768a95f1948637cb7149c6 (patch)
treec4dae631954185a136426e227f316c71a51559d2
parentfe7ff3cedadc33655d6a27b9410515e4e6153c0e (diff)
Optimize getenv() for const char* name
-rw-r--r--libbutl/utility.cxx12
-rw-r--r--libbutl/utility.mxx8
2 files changed, 13 insertions, 7 deletions
diff --git a/libbutl/utility.cxx b/libbutl/utility.cxx
index 0a8ba1e..a891fc2 100644
--- a/libbutl/utility.cxx
+++ b/libbutl/utility.cxx
@@ -16,7 +16,7 @@
#include <cstddef>
#include <utility>
-#include <cstring> // strncmp()
+#include <cstring> // strncmp(), strlen()
#include <ostream>
#include <type_traits> // enable_if, is_base_of
#include <system_error>
@@ -349,11 +349,11 @@ namespace butl
#endif
optional<std::string>
- getenv (const std::string& name)
+ getenv (const char* name)
{
if (const char* const* vs = thread_env_)
{
- size_t n (name.size ());
+ size_t n (strlen (name));
for (; *vs != nullptr; ++vs)
{
@@ -362,9 +362,9 @@ namespace butl
// Note that on Windows variable names are case-insensitive.
//
#ifdef _WIN32
- if (icasecmp (name.c_str (), v, n) == 0)
+ if (icasecmp (name, v, n) == 0)
#else
- if (strncmp (name.c_str (), v, n) == 0)
+ if (strncmp (name, v, n) == 0)
#endif
{
switch (v[n])
@@ -376,7 +376,7 @@ namespace butl
}
}
- if (const char* r = ::getenv (name.c_str ()))
+ if (const char* r = ::getenv (name))
return std::string (r);
return nullopt;
diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx
index 6965be4..bd24ffd 100644
--- a/libbutl/utility.mxx
+++ b/libbutl/utility.mxx
@@ -322,7 +322,13 @@ LIBBUTL_MODEXPORT namespace butl
// overrides (thread_env).
//
LIBBUTL_SYMEXPORT optional<std::string>
- getenv (const std::string&);
+ getenv (const char*);
+
+ inline optional<std::string>
+ getenv (const std::string& n)
+ {
+ return getenv (n.c_str ());
+ }
// Set the process environment variable. Best done before starting any
// threads (see thread_env). Throw system_error on failure.