emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: case-insensitive string comparison


From: Eli Zaretskii
Subject: Re: case-insensitive string comparison
Date: Wed, 20 Jul 2022 20:12:26 +0300

> From: Roland Winkler <winkler@gnu.org>
> Cc: emacs-devel@gnu.org
> Date: Wed, 20 Jul 2022 11:24:38 -0500
> 
> On Tue, Jul 19 2022, Stefan Monnier wrote:
> >> PS. Actually, compare-strings/ignore_case is broken because it does,
> >> essentially, upcase both arguments, see
> >> https://stackoverflow.com/q/319426/850781
> >
> > Hmm... `string-collate-equalp`?
> 
> It would be nice if the node in the elisp manual on "comparison of
> characters and strings" included some discussion on what usage cases
> with case-folding can / should preferentially be covered by the
> locale-dependent function string-collate-equalp versus something like
> compare-strings.

I hear you, but your request is impossible to fulfill in practice.
That's because the collation rules used by this function are
implemented in the C library, and even if we know the locale,
different implementations of libc use different collation rules (in
addition, collation rules for some locales change with time).

The answer to the question "what comparison function should I use in a
specific use case" depends on the details of the use case, on the
locale, and on the libc against which Emacs was linked.

That is why the ELisp manual and the doc strings are intentionally
vague regarding what exactly should you expect as result: we simply
cannot say there anything that is accurate enough and general enough.

compare-strings, by contrast, doesn't use any collation rules, only
the current buffer's value of the case table.  So its results are more
predictable.

> - bibtex-mode needs to compare BibTeX keywords that are ascii strings
>   for which case is insignificant.  So bibtex-string= is exactly what
>   Sam suggests to put into subr.el, and I believe that's good enough
>   (just as almost any other approach I can think of for this particular
>   problem).
> 
> - BBDB needs to know whether a name is already present in the database
>   or not, ignoring case.  The function bbdb-string= is again what Sam
>   suggests to put into subr.el.  The function string-collate-equalp
>   might be better suited for this.  But which locale should it use?  The
>   records in my BBDB cover larger parts of the world and I do not even
>   know which locale(s) might work best for each of them, not to mention
>   that BBDB needs to loop over all records.  Is there a "univeral
>   default locale"?

That "universal default locale" is what Emacs uses, modulo the few
problematic characters like the dotless I etc.  For 100% predictable
results, build your own case table, bind the buffer's case table to
it, and then call case-insensitive comparison.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]