This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Unicode width data inconsistent/outdated

On Aug  3 21:44, Thomas Wolff wrote:
> Am 28.07.2017 um 21:58 schrieb Corinna Vinschen:
> > On Jul 26 23:43, Thomas Wolff wrote:
> > > Am 26.07.2017 um 11:50 schrieb Corinna Vinschen:
> > > > On Jul 26 03:16, Yaakov Selkowitz wrote:
> > > > > On 2017-07-26 03:08, Corinna Vinschen wrote:
> > > > > > On Jul 26 08:49, Thomas Wolff wrote:
> > > > > > > It would be good to keep wcwidth/wcswidth in sync with the installed
> > > > > > > Unicode data version (package unicode-ucd).
> > > > > > > Currently it seems to be hard-coded (in newlib/libc/string/wcwidth.c);
> > > > > > > it refers to Unicode 5.0 while installed Unicode data suggest 9.0 would
> > > > > > > be used.
> > > > > > > I can provide some scripts to generate the respective tables if desired.
> > > > > > > Thomas
> > > > > > If you can update the newlib files this way and send matching patches
> > > > > > to the newlib list, this would be highly appreciated.
> > > > > Thomas, I just updated unicode-ucd to 10.0 for this purpose.
> > > Thanks.
> > > > Oh, and, btw, the comment in wcwidth.c isn't quite correct.  The
> > > > cwstate in newlib is on Unicode 5.2, see newlib/libc/ctype/towupper.c.
> > > Oh, a number of other embedded tables. To make the tow* and isw* functions
> > > more easily adaptable to Unicode updates, there will be some revisions to do
> > > here. And the to* and is* ones (without 'w') even refer to locales in a way
> > > I do not understand. Maybe I'll restrict my effort to wcwidth first...
> > The to* and is* ones (without 'w') don't matter at all and you don't
> > have to touch them.
> > 
> > The Unicode stuff only affects the tow and isw functions.
> > 
> > As for how to fetch the data, you may want to have a look into
> > newlib/libc/ctype/utf8alpha.h and newlib/libc/ctype/utf8print.h.  The
> > header comments contain the awk scripts used to collect the data.
> But there are no instructions to adapt the embedded conditional statements
> referring to those data...

Tables are scanned in-order.  Each table handles a range of 256
characters.  A table comprises the lower 8 bits of a character which
matches the condition.  A 0 character (except in array position 0) is
a continuation marker, which means, all chars between the previous and
the next value match the condition.

Here's an example from utf8alpha.h:

  static const unsigned char ua7[] = {
    0x17, 0x0, 0x1f, 0x22, 0x0, 0x88,
    0x8b, 0x8c,
    0xfb, 0x0, 0xff };

ua7 is the array handling the characters in the range 0xa700 up to 0xa7ff.
The first alpha character in this range is 0xa717.  The next char in the
array is a 0x0, followed by 0x1f.  That means, all character from 0xa717
up to 0xa71f are alphas.  Then we have a 0x22, a 0, and a 0x88.  So all
chars from 0xa722 up to 0xa788 are alphas.  Then we have two chars not
followed by a 0, so they just stand for themselves.  0xa78b and 0xa78c
are alpha chars.  The last group 0xfb, 0x0, 0xff of course means,
0x8afb up to 0xa7ff are alpha chars.

> My attempt would be to base the functions on a common table of character
> categories instead.

Keep in mind that the table is not loaded into memory on demand, as on
Linux.  Rather it will be part of the Cygwin DLL, and worse in case
newlib, any target using the wctype functions.

The idea here is that the tables take less space than a full-fledged
category table.  The tables in utf8print.h and utf8alpha.h and the code
in iswalpha and iswprint combined are 10K, code and data of the
tolower/toupper functions are 7K, wcwidth 3K, so a total of 20K,
covering Unicode 5.2 with 107K codepoints.

A category table would have to contain the category bits for the entire
Unicode codepoint range.  The number of potential bits is > 8 as far as I
know so it needs 2 bytes per char, but let's make that 1 byte for now.
For Unicode 5.2 only the table would be at least 107K, and that would
only cover the iswXXX functions.

> > All other isw* files like iswblank.c contain comments explaining
> > what Unicode character categories are covered.
> I'm comparing results based on Unicode 5.2 data. There will be some
> deviations and maybe some things to discuss.
> For example, I wonder why in the current implementation currency symbols are
> considered as punctuation (which can be easily reproduced).

  iswpunct (c) == !iswalnum (c) && iswgraph (c)

Linux man page claims:

  This function's name is a misnomer when dealing  with  Unicode
  characters,  because  the wide-character class "punct" contains both
  punctuation characters and symbol (math, currency, etc.) characters.

> Also, there are 3 other issues:
> Issue 1 is about handling non-BMP characters by wcwidth.
> This has been discussed before.
> [...]
> (
> Corinna Vinschen wrote:
> > And, please note the wording in SUSv4, for instance in
> >
> (not found)

oops, good one.  Just see the upstream SUSv4 iswalpha man page.

> >   The wc argument is a wint_t, the value of which the application shall
> >                        ^^^^^^                         ^^^^^^^^^^^
> >   ensure is a wide-character code corresponding to a valid character in
> > the current locale, or equal to the value of the macro WEOF. If the
> > argument has any other value, the behavior is undefined.
> > I don't see any words in that which would disallow to convert UTF-16
> > wchar_t surrogates to a wint_t UTF-32 value before calling one of the
> > wctype functions.  Just like you have to be careful not to call the
> > ctype functions with a signed char.
> While wcswidth works already (using internal __wcwidth), and the isw* and
> tow* functions work as well because they use wint_t, wcwidth is the only
> function (inconsistently insisting on wchar_t) that does not work.

Trying to be close to the standard here.

> But note which says
> > Note that glibc before 2.2.5 used the prototype
> > int wcwidth(wint_t c);
> Why not revert to wcwidth(wint_t)?
> I think for cygwin it is the only solution that makes wcwidth work for
> non-BMP characters and is also compatible (unlike some proposals discussed
> later in the quoted thread).

We can do this, but it may result in complaints from the other
newlib consumers.  If in doubt, use #ifdef __CYGWIN__

> Issue 2 is the handling of titlecase characters (e.g. "Nj" as one Unicode
> character U+01CB). The current implementation considers them to be both
> upper and lower (iswupper: return towlower (c) != c); I'd rather consider
> them as neither upper nor lower (iswalpha (c) && towupper (c) == c).
> allows both interpretations:
> > The wide-character class "upper" contains *at least* those characters wc
> > which are equal to towupper(wc) and different from towlower(wc).

Susv4 says "The iswupper() [...] functions shall test whether wc is a
wide-character code representing a character of class upper." Whatever
does that correctly with a low footprint is fine.

> Issue 3 is the special conversion jp2uc which seems to be half-bred; there
> is no such handling for Chinese or Korean.

This shouldn't matter to you, just keep it in place.  It's a historical,
low footprint conversion for japanese characters without pulling in the
unicode stuff.  Not used on Cygwin so just ignore.


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

Attachment: signature.asc
Description: PGP signature

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]