Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 56867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:36:06+00:00 2026-05-10T17:36:06+00:00

I am looking for a method to compare and sort UTF-8 strings in C++

  • 0

I am looking for a method to compare and sort UTF-8 strings in C++ in a case-insensitive manner to use it in a custom collation function in SQLite.

  1. The method should ideally be locale-independent. However I won’t be holding my breath, as far as I know, collation is very language-dependent, so anything that works on languages other than English will do, even if it means switching locales.
  2. Options include using standard C or C++ library or a small (suitable for embedded system) and non-GPL (suitable for a proprietary system) third-party library.

What I have so far:

  1. strcoll with C locales and std::collate/std::collate_byname are case-sensitive. (Are there case-insensitive versions of these?)
  2. I tried to use a POSIX strcasecmp, but it seems to be not defined for locales other than 'POSIX'

    In the POSIX locale, strcasecmp() and strncasecmp() do upper to lower conversions, then a byte comparison. The results are unspecified in other locales.

    And, indeed, the result of strcasecmp does not change between locales on Linux with GLIBC.

    #include <clocale> #include <cstdio> #include <cassert> #include <cstring>  const static char *s1 = 'Äaa'; const static char *s2 = 'äaa';  int main() {     printf('strcasecmp('%s', '%s') == %d\n', s1, s2, strcasecmp(s1, s2));     printf('strcoll('%s', '%s') == %d\n', s1, s2, strcoll(s1, s2));     assert(setlocale(LC_ALL, 'en_AU.UTF-8'));     printf('strcasecmp('%s', '%s') == %d\n', s1, s2, strcasecmp(s1, s2));     printf('strcoll('%s', '%s') == %d\n', s1, s2, strcoll(s1, s2));     assert(setlocale(LC_ALL, 'fi_FI.UTF-8'));     printf('strcasecmp('%s', '%s') == %d\n', s1, s2, strcasecmp(s1, s2));     printf('strcoll('%s', '%s') == %d\n', s1, s2, strcoll(s1, s2)); } 

    This is printed:

    strcasecmp('Äaa', 'äaa') == -32 strcoll('Äaa', 'äaa') == -32 strcasecmp('Äaa', 'äaa') == -32 strcoll('Äaa', 'äaa') == 7 strcasecmp('Äaa', 'äaa') == -32 strcoll('Äaa', 'äaa') == 7 

P. S.

And yes, I am aware about ICU, but we can’t use it on the embedded platform due to its enormous size.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:36:07+00:00Added an answer on May 10, 2026 at 5:36 pm

    What you really want is logically impossible. There is no locale-independent, case-insensitive way of sorting strings. The simple counter-example is ‘i’ <> ‘I’ ? The naive answer is no, but in Turkish these strings are unequal. ‘i’ is uppercased to ‘İ’ (U+130 Latin Capital I with dot above)

    UTF-8 strings add extra complexity to the question. They’re perfectly valid multi-byte char* strings, if you have an appropriate locale. But neither the C nor the C++ standard defines such a locale; check with your vendor (too many embedded vendors, sorry, no genearl answer here). So you HAVE to pick a locale whose multi-byte encoding is UTF-8, for the mbscmp function to work. This of course influences the sort order, which is locale dependent. And if you have NO locale in which const char* is UTF-8, you can’t use this trick at all. (As I understand it, Microsoft’s CRT suffers from this. Their multi-byte code only handles characters up to 2 bytes; UTF-8 needs 3)

    wchar_t is not the standard solution either. It supposedly is so wide that you don’t have to deal with multi-byte encodings, but your collation will still depend on locale (LC_COLLATE) . However, using wchar_t means you now choose locales that do not use UTF-8 for const char*.

    With this done, you can basically write your own ordering by converting strings to lowercase and comparing them. It’s not perfect. Do you expect L’ß’ == L’ss’ ? They’re not even the same length. Yet, for a German you have to consider them equal. Can you live with that?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Changing the delimiter is only needed when using the mysql… May 13, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer The Image object in your visual tree is referenced by… May 13, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer This may not sound proper but the best possible way… May 13, 2026 at 3:17 pm

Related Questions

Our nightly build process was broken for a long time, such that it generated
I managed to have each character stored in a bitmap and am looking for
I am looking for the fastest way to de/interleave a buffer. To be more
I am trying to create a small server type application and have a question

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.