-
Notifications
You must be signed in to change notification settings - Fork 64
Make functions to read and write code points public #308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,14 +122,23 @@ internal fun String.utf8Size(startIndex: Int = 0, endIndex: Int = length): Long | |
/** | ||
* Encodes [codePoint] in UTF-8 and writes it to this sink. | ||
* | ||
* Note that in general, a value retrieved from [Char.code] could not be written directly | ||
* as it may be a part of a [surrogate pair](https://www.unicode.org/faq/utf_bom.html#utf16-2) (that could be | ||
* detected using [Char.isSurrogate], or [Char.isHighSurrogate] and [Char.isLowSurrogate]). | ||
* Such a pair of characters needs to be manually converted back to a single code point | ||
* which then could be written to a [Sink]. | ||
* Without such a conversion, data written to a [Sink] can not be converted back | ||
* to a string from which a surrogate pair was retrieved. | ||
* | ||
* @param codePoint the codePoint to be written. | ||
* | ||
* @throws IllegalStateException when the sink is closed. | ||
* | ||
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.utf8CodePointSample | ||
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeUtf8CodePointSample | ||
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeSurrogatePair | ||
*/ | ||
@OptIn(DelicateIoApi::class) | ||
internal fun Sink.writeUtf8CodePoint(codePoint: Int): Unit = | ||
public fun Sink.writeCodePointValue(codePoint: Int): Unit = | ||
writeToInternalBuffer { it.commonWriteUtf8CodePoint(codePoint) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it handle negative values of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not. Opened #317 |
||
|
||
/** | ||
|
@@ -196,24 +205,31 @@ public fun Source.readString(byteCount: Long): String { | |
} | ||
|
||
/** | ||
* Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary. | ||
* Decodes a single code point value from UTF-8 code units, reading between 1 and 4 bytes as necessary. | ||
* | ||
* If this source is exhausted before a complete code point can be read, this throws an | ||
* [EOFException] and consumes no input. | ||
* | ||
* If this source doesn't start with a properly-encoded UTF-8 code point, this method will remove | ||
* 1 or more non-UTF-8 bytes and return the replacement character (`U+fffd`). This covers encoding | ||
* problems (the input is not properly-encoded UTF-8), characters out of range (beyond the | ||
* `0x10ffff` limit of Unicode), code points for UTF-16 surrogates (`U+d800`..`U+dfff`) and overlong | ||
* encodings (such as `0xc080` for the NUL character in modified UTF-8). | ||
* If this source starts with an ill-formed UTF-8 code units sequence, this method will remove | ||
* 1 or more non-UTF-8 bytes and return the replacement character (`U+fffd`). | ||
* | ||
* The replacement character (`U+fffd`) will be also returned if the source starts with a well-formed | ||
shanshin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* code units sequences, but a decoded value does not pass further validation, such as | ||
* the value is out of range (beyond the `0x10ffff` limit of Unicode), maps to UTF-16 surrogates (`U+d800`..`U+dfff`), | ||
* or an overlong encoding is detected (such as `0xc080` for the NUL character in modified UTF-8). | ||
* | ||
* Note that in general, returned value may not be directly converted to [Char] as it may be out | ||
* of [Char]'s values range and should be manually converted to a | ||
* [surrogate pair](https://www.unicode.org/faq/utf_bom.html#utf16-2). | ||
* | ||
* @throws EOFException when the source is exhausted before a complete code point can be read. | ||
* @throws IllegalStateException when the source is closed. | ||
* | ||
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.readUtf8CodePointSample | ||
* @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.surrogatePairs | ||
*/ | ||
@OptIn(InternalIoApi::class) | ||
internal fun Source.readUtf8CodePoint(): Int { | ||
public fun Source.readCodePointValue(): Int { | ||
require(1) | ||
|
||
val b0 = buffer[0].toInt() | ||
|
@@ -226,13 +242,6 @@ internal fun Source.readUtf8CodePoint(): Int { | |
return buffer.commonReadUtf8CodePoint() | ||
} | ||
|
||
/** | ||
* @see Source.readUtf8CodePoint | ||
*/ | ||
internal fun Buffer.readUtf8CodePoint(): Int { | ||
return this.commonReadUtf8CodePoint() | ||
} | ||
|
||
/** | ||
* Removes and returns UTF-8 encoded characters up to but not including the next line break. A line break is | ||
* either `"\n"` or `"\r\n"`; these characters are not included in the result. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation does not say anything about what will be written if we pass a code point with the surrogate value (
U+d800
..U+dfff
).E.g. on the JVM will be written single byte with value 63 (
?
)Also we should add test on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, we always writing '?'.
Utf8Test::writeSurrogateCodePoint
covers that scenario.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is not mentioned in the KDoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing to that! Opened #314