_compareCommandNames should use locale-aware string comparisons #9656

Closed
opened 2026-01-31 02:00:19 +00:00 by claunia · 9 comments
Owner

Originally created by @jtippet on GitHub (Jul 16, 2020).

_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.

Originally created by @jtippet on GitHub (Jul 16, 2020). [_compareCommandNames ](https://github.com/microsoft/terminal/blob/934ad98786e33407e5549c36e2288601a3a083d8/src/cascadia/TerminalApp/CommandPalette.cpp#L253)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.
Author
Owner

@DHowett commented on GitHub (Jul 16, 2020):

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

@DHowett commented on GitHub (Jul 16, 2020): If there's some STL wstring locale-sensitive compare, I'd index towards that one. Thanks for pointing this out!
Author
Owner

@Don-Vito commented on GitHub (Oct 15, 2020):

@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 😄

@Don-Vito commented on GitHub (Oct 15, 2020): @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 😄
Author
Owner

@PankajBhojwani commented on GitHub (Oct 16, 2020):

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 commented on GitHub (Oct 16, 2020): Based on what I see [here](https://www.oreilly.com/library/view/c-cookbook/0596007612/ch13s06.html), @Don-Vito 's way seems the way to go, would you mind making a PR with the change?
Author
Owner

@Don-Vito commented on GitHub (Oct 16, 2020):

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

@Don-Vito commented on GitHub (Oct 16, 2020): @PankajBhojwani - I can absolutely create a PR, but I am not sure what locale should be used. Is std::locale() good enough?
Author
Owner

@PankajBhojwani commented on GitHub (Oct 20, 2020):

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!

@PankajBhojwani commented on GitHub (Oct 20, 2020): I think `std::locale("")` obtains the native locale ([source](https://stdcxx.apache.org/doc/stdlibug/24-3.html)), so that might be what we want actually. Will look into it more and let you know!
Author
Owner

@PankajBhojwani commented on GitHub (Oct 22, 2020):

@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 commented on GitHub (Oct 22, 2020): @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
Author
Owner

@Don-Vito commented on GitHub (Oct 22, 2020):

@PankajBhojwani - great! I am creating one.

@Don-Vito commented on GitHub (Oct 22, 2020): @PankajBhojwani - great! I am creating one.
Author
Owner

@Don-Vito commented on GitHub (Oct 23, 2020):

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

@Don-Vito commented on GitHub (Oct 23, 2020): @PankajBhojwani - made it work by explicitly invoking winapi's GetUserDefaultLocaleName. Still need to cleanup the code but please take a look.
Author
Owner

@zadjii-msft commented on GitHub (Nov 19, 2020):

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

@zadjii-msft commented on GitHub (Nov 19, 2020): After some additional thought, we're going to try and solve this with #7039 instead. Thanks for all the investigation everyone!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#9656