[dart2js] Take care not to split surrogate pairs when writing output
[dart2js] Take care not to split surrogate pairs when writing output

dart2js chunks the writing of the JavaScript file.  With `--utf8`, the
output can contain code points that are represented as two surrogate
pairs code units in the UTF-16 string. If the chunking splits the the
surrogate pair then the lower level UTF8 encoding does not see the
other element of the pair, so encodes the illegal unpaired surrogate
as U+FFFD � (REPLACEMENT CHARACTER).

The code point U+10000 ("𐀀") is encoded as the surrogate pair U+D800
followed by U+DC00, which normally is emitted as the UTF8 bytes

    ... F0 90 80 80 ...

If split, each surrogate is converted to U+FFFD, resulting in bytes

    ... EF BF BD EF BF BD ...

This change avoids splitting the surrogate pair by adjusting the index
at which the string is split.

Change-Id: I9629bf07d391005934c99d1dd649c55c4c58c3bc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249066
Reviewed-by: Mayank Patke <e0355b78204b67a55a952941c68c8f1b40235938@google.com>
Commit-Queue: Stephen Adams <60cb5b73b7e0cb7dcda53302da4fab6ecfc7fa8b@google.com>
Reviewed-by: Joshua Litt <9f8daed1ab51574c9a48ffacb03e4a1adb4f880c@google.com>

dart-lang/sdk,dart-lang/sdk,dart-lang/sdk,dart-lang/sdk,dart-lang/sdk,dart-lang/sdk,dart-lang/sdk,dart-lang/sdk