java.lang.CharSequence#compare - lexicographical ordering of custom Comparable CharSequence(s)

Florian Weimer fw at deneb.enyo.de
Wed Mar 20 18:37:54 UTC 2019


* Jason Mehrens:

> The initial implementation was only optimized to call into
> String.compare if the arguments were string [1].  I proposed the
> current code a general form to catch java.nio.CharBuffer and any new
> JDK implementations of CharSequence + Comparable.
>
> Naively, I lean towards "- CharSequence interface specification
> should be extended to require Comparable CharSeqeunces to implement
> lexicographical ordering".

There are natural lexicographical orderings, unfortunately.  One is
according to UCS-2 (currently implemented by String and specified by
CharSequence.compare), and the other one according UTF-16, for
compatibility with the lexicographical ordering according to Unicode
codepoints (which is also compatible with lexicographical ordering of
bytes after UTF-8 encoding).

There must be implementations of CharSequence out there which
implement UTF-16 ordering.


More information about the core-libs-dev mailing list