diff --git a/elisp/process.cc b/elisp/process.cc index ad1f1586..35c6fc7f 100644 --- a/elisp/process.cc +++ b/elisp/process.cc @@ -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 @@ -491,4 +495,20 @@ absl::StatusOr 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(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 diff --git a/elisp/process.h b/elisp/process.h index 5151d9f0..80cee00b 100644 --- a/elisp/process.h +++ b/elisp/process.h @@ -19,8 +19,10 @@ # error this file requires at least C++17 #endif +#include #include #include +#include #include #ifdef __GNUC__ @@ -48,7 +50,13 @@ namespace rules_elisp { -using Environment = std::map; +#ifdef _WIN32 +struct NativeComp; +#else +using NativeComp = std::less; +#endif + +using Environment = std::map; class Runfiles final { public: