https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113318

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---

After sending that I realised that text_encoding::environment_is<id> can be
optimized like so:

    template<id _Id>
      static bool
      environment_is()
      { return text_encoding(_Id)._M_is_environment(); }

Where that calls an extern function in the library:

bool
std::text_encoding::_M_is_environment() const
{
  bool matched = false;
  if (locale_t loc = ::newlocale(LC_ALL_MASK, "", (locale_t)0))
    {
      if (const char* codeset = ::nl_langinfo_l(CODESET, loc))
        matched = ranges::contains(aliases(), string_view(codeset));
      ::freelocale(loc);
    }
  return matched;
}

That way we do a fast binary search for the ID in the static array, and only do
the slower string comparisons with the actual aliases of the specified id,
instead of searching the entire array doing string comparisons.

Reply via email to