Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Issue 9233041: String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. (Closed)

Created:
8 years, 10 months ago by dcarlson
Modified:
8 years, 10 months ago
CC:
reviews_dartlang.org
Visibility:
Public.

Description

String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. BUG= TEST= Committed: https://code.google.com/p/dart/source/detail?r=3814

Patch Set 1 #

Patch Set 2 : String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. #

Total comments: 79

Patch Set 3 : updates from initial review. #

Total comments: 4

Patch Set 4 : updates based on comments. #

Patch Set 5 : remove notes to self #

Total comments: 30

Patch Set 6 : updates to comments. #

Unified diffs Side-by-side diffs Delta from patch set Stats (+1997 lines, -0 lines) Patch
A utils/string_encoding/Unicode.dart View 1 2 3 4 5 1 chunk +37 lines, -0 lines 0 comments Download
A utils/string_encoding/Utf16.dart View 1 2 3 4 5 1 chunk +273 lines, -0 lines 0 comments Download
A utils/string_encoding/Utf32.dart View 1 2 3 4 5 1 chunk +273 lines, -0 lines 0 comments Download
A utils/string_encoding/Utf8.dart View 1 2 3 4 5 1 chunk +8 lines, -0 lines 0 comments Download
A utils/string_encoding/Utf8_impl.dart View 1 2 3 4 5 1 chunk +221 lines, -0 lines 0 comments Download
A utils/string_encoding/unicode_core.dart View 1 2 3 4 5 1 chunk +181 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/DUnit.dart View 1 2 3 4 5 1 chunk +117 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/run_tests.dart View 1 2 3 4 5 1 chunk +26 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/unicode_core_tests.dart View 1 2 3 4 5 1 chunk +103 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/unicode_tests.dart View 1 2 3 4 5 1 chunk +37 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/utf16_tests.dart View 1 2 3 4 5 1 chunk +106 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/utf32_tests.dart View 1 2 3 4 5 1 chunk +158 lines, -0 lines 0 comments Download
A utils/tests/string_encoding/utf8_tests.dart View 1 2 3 4 5 1 chunk +457 lines, -0 lines 0 comments Download

Messages

Total messages: 10 (0 generated)
dcarlson
Initial stab at (sharable) Unicode/UTF library.
8 years, 10 months ago (2012-01-31 14:15:54 UTC) #1
ahe
Initial comments. Don't wait for my approval, it'll take me some time to get through ...
8 years, 10 months ago (2012-01-31 14:46:52 UTC) #2
dcarlson
Thanks. https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart File utils/string_encoding/Unicode.dart (right): https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart#newcode13 utils/string_encoding/Unicode.dart:13: if (is16BitCodeUnit()) { On 2012/01/31 14:46:53, ahe wrote: ...
8 years, 10 months ago (2012-01-31 15:07:34 UTC) #3
jat
Generally looks good, with thorough tests. https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart File utils/string_encoding/Unicode.dart (right): https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart#newcode1 utils/string_encoding/Unicode.dart:1: // Copyright (c) ...
8 years, 10 months ago (2012-01-31 15:19:21 UTC) #4
Dan Rice
https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart File utils/string_encoding/Unicode.dart (right): https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart#newcode13 utils/string_encoding/Unicode.dart:13: if (is16BitCodeUnit()) { +1 https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Unicode.dart#newcode26 utils/string_encoding/Unicode.dart:26: return (new String.fromCharCodes( ...
8 years, 10 months ago (2012-01-31 15:59:54 UTC) #5
Dan Rice
https://chromiumcodereview.appspot.com/9233041/diff/2014/utils/tests/string_encoding/Utf8Tests.dart File utils/tests/string_encoding/Utf8Tests.dart (right): https://chromiumcodereview.appspot.com/9233041/diff/2014/utils/tests/string_encoding/Utf8Tests.dart#newcode410 utils/tests/string_encoding/Utf8Tests.dart:410: Expect.stringEquals("? דג סקרן שט בים מאוכזב ולפתע מצא לו ...
8 years, 10 months ago (2012-01-31 16:01:10 UTC) #6
dcarlson
Thanks for all the feedback. I think most concerns are addressed. Please holler if I ...
8 years, 10 months ago (2012-01-31 22:11:38 UTC) #7
jat
LGTM https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Utf16.dart File utils/string_encoding/Utf16.dart (right): https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encoding/Utf16.dart#newcode55 utils/string_encoding/Utf16.dart:55: List<int> encoding = <int>[0xfe, 0xff]; On 2012/01/31 22:11:38, ...
8 years, 10 months ago (2012-01-31 22:46:12 UTC) #8
Søren Gjesse
ltgm Most of my comments are suggestions that you can consider. https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encoding/Unicode.dart File utils/string_encoding/Unicode.dart (right): ...
8 years, 10 months ago (2012-02-01 11:25:25 UTC) #9
dcarlson
8 years, 10 months ago (2012-02-01 22:18:46 UTC) #10
I think we are up to date here.

https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encodin...
File utils/string_encoding/Utf16.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/1014/utils/string_encodin...
utils/string_encoding/Utf16.dart:55: List<int> encoding = <int>[0xfe, 0xff];
On 2012/01/31 22:46:12, jat wrote:
> On 2012/01/31 22:11:38, dcarlson wrote:
> > It is the BOM --  not required, but currently stripping if provided (e.g. it
> has
> > a BOM).
> 
> My recollection is some libraries treat the presence of BOM in the case of
> UTF16-LE/BE as an error, since you are supposed to already know what it is --
> otherwise, there is no value of having UTF16-LE/BE, just use UTF16 if you are
> going to have BOM anyway.
> 
> I am assuming this isn't an issue for what you are interoperating with, so if
> you want to leave it the way it is I am ok with that.

So, I'm switching things up a little. I want to provide flexibility to the user,
because I don't necessarily know about the context (e.g. the protocol). I don't
throw an exception, but include a parameter that can specify whether to strip
the BOM.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
File utils/string_encoding/Unicode.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Unicode.dart:5: #library("Unicode");
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> I think our current naming scheme for libraries is lowercase only.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Unicode.dart:6: #import("UnicodeCore.dart");
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Same for file names. All Dart files in e.g. corelib/src uses all lowercase and
> underscores.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
File utils/string_encoding/UnicodeCore.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/UnicodeCore.dart:45: * Encode code points as UTF16 code
units.
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Please be consistent with UTF16/utf16 in comments. Actually the "official"
name
> seems to be UTF-16.

Done. I went ahead and made sure all the variants match the spec.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/UnicodeCore.dart:86: [int start = 0, int length = null]) {
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Normally we don't split argument lists like this. Won't
> 
> List<int> utf16CodeUnitsToCodepoints(
>     List<int> utf16CodeUnits, [int start = 0, int length = null]) {
> 
> fit?
Not always (other lines), but done with a simple line wrap.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
File utils/string_encoding/Utf16.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf16.dart:5: #library("UTF16");
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Maybe utf16?

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
File utils/string_encoding/Utf8_impl.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:35: String decodeFromUtf8(List<int> bytes)
=>
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Should we perhaps have an optional offset and length field here. We do in a
lot
> of places where byte Lists are passed. In some cases you might like to decode
> parts of a network buffer received.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:35: String decodeFromUtf8(List<int> bytes)
=>
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Should we perhaps have an optional offset and length field here. We do in a
lot
> of places where byte Lists are passed. In some cases you might like to decode
> parts of a network buffer received.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:36:
codepointsToString(_utf8ToCodepoints(bytes, 0, bytes.length));
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Consider having both _utf8ToCodepoints and _utf8ToString where _utf8ToString
> fills characters directly into a StringBuffer and returns a string. That
avoids
> allocating the code points array.

I don't think this buys us much. The StringBuffer is a List (of strings, but we
are adding a character at a time, so no real win). If we can figure the length
ahead of time, we could allocate a ByteArray to perhaps reduce the # of objects
allocated at any one time.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:114: [int start = 0, int length = null]) {
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> I think corelib normally uses the name offset instead of start.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:127: List<int> codepointBuffer = <int>[];
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Have you thought about having an initial scan for calculating the number of
> decoded codepoints first (like you calculate encodedLength above)?

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/string_encodin...
utils/string_encoding/Utf8_impl.dart:147: additionalBytes = 3;
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Do we ever want to decode more than 4 bytes. There are no defined codepoints
> that require more AFAIK.

No valid codepoints, but the spec defines decodings out to 6 bytes -- I wanted
to keep these because although will treat it as an invalid character, we don't
want to get out of sync.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
File utils/tests/string_encoding/UnicodeCoreTests.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
utils/tests/string_encoding/UnicodeCoreTests.dart:29: // boundary conditions
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> How about having a shared list of the "sane" test data:
> 
> ... = [[0x0], "0"],
>        [0xd800, 0xdc00], "10000",
>        ...];
> 
> And reuse then in both tests.

Thanks for the suggestion -- I think this is clearer for the number of
datapoints included here.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
File utils/tests/string_encoding/Utf16Tests.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
utils/tests/string_encoding/Utf16Tests.dart:22: void testEncodeToUtf16() {
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Same here, have duplicated test data in a shared list.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
File utils/tests/string_encoding/Utf32Tests.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
utils/tests/string_encoding/Utf32Tests.dart:22: void testEncodeToUtf32() {
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Again, share duplicate test data.

Done.

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
File utils/tests/string_encoding/Utf8Tests.dart (right):

https://chromiumcodereview.appspot.com/9233041/diff/9001/utils/tests/string_e...
utils/tests/string_encoding/Utf8Tests.dart:192:
matchingReplacementChars.addAll([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]);
On 2012/02/01 11:25:25, Søren Gjesse wrote:
> Long line here and some more places below.

Done.

Powered by Google App Engine
This is Rietveld 408576698