Codereview request for 7096080: UTF8 update and new CESU-8 charset

Xueming Shen xueming.shen at
Wed Sep 28 19:18:34 UTC 2011


[I combined the proposed charge for #7082884, in which no one appears to be
interested:-) into this one]

Unicode Standard added "Addition Constraints on conversion of ill-formed 
in version 5.1 [1] and updated in 6.0 again with further "clarification" 
[2] regarding
how a "conformance" implementation should handle ill-formed UTF-8 byte
sequence. Basically it says

  (1) the conversion process should not interpret any ill-formed code 
unit sequence
  (2) such process must not treat any adjacent well-formed code unit 
      as being part of those ill-formed code unit sequences
  (3) and recommend a "best practice" of "maximal valid sub-part" for 

The new UTF-8 charset implementation we put in JDK7 (and back-ported  to 
release since then) follows the new constraints in most cases, except

(1) The decoder still accepts "historical" 3 bytes surrogates and 6 
bytes surrogate
pair (the encoder never outputs such sequence). Unicode Standard 
"tightened" its
UTF-8 definition in ver 3.2 [3], as

     "Most notable among the corrigenda to the Standard is a further 
      of the definition of UTF-8, to eliminate irregular UTF-8 and to 
bring the
      Unicode specification of UTF-8 more completely into line with other
      specifications of UTF-8."

So the 3-byte/6-byte surrogates are now defined as "ill-formed" code unit
sequence, instead of "irregular" [5] in ver 3.1

(2) While no longer accepting the "historical" 5-byte, 6-byte UTF-8 byte 
the decoder treats these 5/6-byte sequence as ONE malformed unit. As a 
these bytes get replaced by one replacement character, when "replace for
malformed" is desirable (as in new String(bytes), for example). 
According the
latest Unicode standard [2], however, because the leading byte of these 
sequence is no longer an illegal appearance of the UTF-8, these bytes 
should be
treated as 5-6 individual ill-formed bytes.

(3)Corner case like ill-formed byte sequence ED 31 is not handled correctly/
consistently, as described in #7082884 [6]

The reason behind (1) and (2) is mostly the compatibility concern. As 
in TR#26 [4] (in which it defines CESU-8, a separate UTF encoding scheme 
uses 3-6-byte sequence for supplementary characters, instead of 4-byte 
in UTF-8), there are apps/data over there that do use surrogates pair in 
form. To change the UTF-8 charset to follow standard obviously will break
someone's code when they migrate/upgrade from JDK/JRE N to N+1, something
we try really hard to avoid.

That said, given almost decade has passed and we are now at Unicode 6, I 
the possibility of breaking someone's code/date of upgrading UTF-8 to do the
"right thing" is small/minor. So I proposed here

(1) to upgrade the JDK8 UTF-8 implementation to strictly follow the 
standard to
      a) reject 3-byte surrogate/6-byte surrogate pair
      b) treats 5/6-byte surrogate as individual ill-formed bytes.
      c) fix the corner case bug #7082884
(2) to add CESU-8 charset into JDK/JRE's charset repository (for those still
     prefer/work on 3-6 bytes surrogate, in "UTF-8" form)

Here is the webrev. The change will need to go through some "in-compatible
change" review process, but I think we can start the code review/discussion
here first. 



More information about the core-libs-dev mailing list