Convert static std::set to constexpr array
Google C++ Style Guide disallows static variable with non-trivial
destructor. Use constexpr for static variable whenever possible.
std::array would make the code cleaner, but MSVC's implementation
of constexpr std::array is buggy.
Side-benefit: linear search is faster when array is small (<20).
Change-Id: Ic8244dcb868e27d02ceb5298ccec482e7d4254b7
PiperOrigin-RevId: 187451495
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 6ad7cc4..4e0c455 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -359,8 +359,8 @@
#if defined(COMPILER_MSVC)
static void PreprocessEnvString(string* env_str) {
- static std::set<string> vars_to_uppercase = {"PATH", "TMP", "TEMP", "TEMPDIR",
- "SYSTEMROOT"};
+ static constexpr const char* vars_to_uppercase[] = {"PATH", "SYSTEMROOT",
+ "TEMP", "TEMPDIR", "TMP"};
int pos = env_str->find_first_of('=');
if (pos == string::npos) return;
@@ -368,7 +368,8 @@
string name = env_str->substr(0, pos);
// We do not care about locale. All variable names are ASCII.
std::transform(name.begin(), name.end(), name.begin(), ::toupper);
- if (vars_to_uppercase.find(name) != vars_to_uppercase.end()) {
+ if (std::find(std::begin(vars_to_uppercase), std::end(vars_to_uppercase),
+ name) != std::end(vars_to_uppercase)) {
env_str->assign(name + "=" + env_str->substr(pos + 1));
}
}