Terminal: _compareCommandNames should use locale-aware string comparisons

Created on 16 Jul 2020  路  9Comments  路  Source: microsoft/terminal

_compareCommandNames operates on strings that are presented to the end-user, so it should sort like a human, not by codepoint. I'm not sure whether this project prefers CompareStringEx or lstrcmp or some CRT routine.

Area-Localization Area-User Interface Help Wanted Issue-Bug Needs-Tag-Fix Product-Terminal

All 9 comments

If there's some STL wstring locale-sensitive compare, I'd index towards that one. Thanks for pointing this out!

@DHowett - is something like this STL enough?

static bool _compareCommandNames(const Command& lhs, const Command& rhs)
{
        const auto& f = std::use_facet<std::collate<wchar_t>>(std::locale());
        const auto lhsName = lhs.Name();
        const auto rhsName = rhs.Name();
        return f.compare(lhsName.data(), lhsName.data() + lhsName.size(), rhsName.data(), rhsName.data() + rhsName.size()) < 0;            
}

I am just confused what locale should we use.. just written commands in 3 languages and now wondering what should happen 馃槃

Based on what I see here, @Don-Vito 's way seems the way to go, would you mind making a PR with the change?

@PankajBhojwani - I can absolutely create a PR, but I am not sure what locale should be used. Is std::locale() good enough?

I think std::locale("") obtains the native locale (source), so that might be what we want actually. Will look into it more and let you know!

@Don-Vito at this point I want to say it probably is a fine implementation for now, and to go ahead and make the PR for it if you'd like! We can handle potential issues in post

@PankajBhojwani - great! I am creating one.

@PankajBhojwani - made it work by explicitly invoking winapi's GetUserDefaultLocaleName. Still need to cleanup the code but please take a look.

After some additional thought, we're going to try and solve this with #7039 instead. Thanks for all the investigation everyone!

Was this page helpful?
0 / 5 - 0 ratings