Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIs to read and write code points. #145

Merged
merged 1 commit into from
Apr 26, 2015

Conversation

swankjesse
Copy link
Member

The String APIs transcode UTF-16 to UTF-8 and back.

These APIs avoid the UTF-16 intermediate form altogether, and
go right from UTF-8 to a codepoint and back.


} else if (codePoint < 0x10000) {
if (codePoint >= 0xd800 && codePoint <= 0xdfff) {
throw new IllegalArgumentException(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting design decision here. What to do when the input's not quite right:

  • we could throw
  • we could encode it (it's still recoverable on the other end, though encoders might return the replacement char)
  • we could encode the replacement character ourselves

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tough. Option 2 doesn't seem reasonable to me. I'm inclined to lean towards 1 (as implemented), but I see the value in 3. I think it's hard to be too forgiving at this level. A higher-level API could handle the replacement character writing, but when you're at this level I think throwing is correct.

@swankjesse
Copy link
Member Author

HttpUrl wants this. It's defined in terms of code points, and wants to parse codepoint-by-codepoint. I'm currently doing it Java char-by-char, but it's awkward.
square/okhttp#1486

* Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
*
* <p>If this source is exhausted before a complete code point can be read, this throws an {@link
* java.io.EOFException} and consumes no input.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice doc. I don't think we're very good about documenting the behavior when methods throw (related to the contents of the underlying buffer).

@JakeWharton
Copy link
Member

LGTM

The String APIs transcode UTF-16 to UTF-8 and back.

These APIs avoid the UTF-16 intermediate form altogether, and
go right from UTF-8 to a codepoint and back.
@swankjesse swankjesse force-pushed the jwilson_0425_utf8_code_points branch from 6529430 to 85e9f94 Compare April 26, 2015 12:30
swankjesse added a commit that referenced this pull request Apr 26, 2015
@swankjesse swankjesse merged commit 3c61fdb into master Apr 26, 2015
@swankjesse swankjesse deleted the jwilson_0425_utf8_code_points branch May 16, 2015 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants