From 37758a01761331f534768a95f1948637cb7149c6 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 20 Apr 2021 10:40:37 +0200 Subject: Optimize getenv() for const char* name --- libbutl/utility.cxx | 12 ++++++------ libbutl/utility.mxx | 8 +++++++- 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 #include -#include // strncmp() +#include // strncmp(), strlen() #include #include // enable_if, is_base_of #include @@ -349,11 +349,11 @@ namespace butl #endif optional - 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 - getenv (const std::string&); + getenv (const char*); + + inline optional + 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. -- cgit v1.1