Skip to content

Commit

Permalink
Use case-insensitive comparison for environment variable keys on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
phst committed Feb 20, 2024
1 parent bd9a994 commit 1dd1f8d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
20 changes: 20 additions & 0 deletions elisp/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
namespace rules_elisp {

#ifdef _WIN32
struct NativeComp {
bool operator()(std::wstring_view a, std::wstring_view b) const;
};

// Build a command line that follows the Windows conventions. See
// https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-170#parsing-c-command-line-arguments
// and
Expand Down Expand Up @@ -491,4 +495,20 @@ absl::StatusOr<int> Run(const std::string_view binary,
#endif
}

#ifdef _WIN32
static int CastToIntOrDie(const std::wstring_view::size_type n) {
if (n > kMaxInt) LOG(FATAL) << "Number " << n << " doesn’t fit in an int";
return static_cast<int>(n);
}

bool NativeComp::operator()(const std::wstring_view a,
const std::wstring_view b) const {
const int result = ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE,
a.data(), CastToIntOrDie(a.length()),
b.data(), CastToIntOrDie(b.length()));
if (result == 0) LOG(FATAL) << WindowsStatus("CompareStringW");
return result == CSTR_LESS_THAN;
}
#endif

} // namespace rules_elisp
10 changes: 9 additions & 1 deletion elisp/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
# error this file requires at least C++17
#endif

#include <functional>
#include <map>
#include <memory>
#include <string>
#include <string_view>

#ifdef __GNUC__
Expand Down Expand Up @@ -48,7 +50,13 @@

namespace rules_elisp {

using Environment = std::map<NativeString, NativeString>;
#ifdef _WIN32
struct NativeComp;
#else
using NativeComp = std::less<std::string>;
#endif

using Environment = std::map<NativeString, NativeString, NativeComp>;

class Runfiles final {
public:
Expand Down

0 comments on commit 1dd1f8d

Please sign in to comment.