Skip to content

[stdlib] Allow Unicode Encoding to fail #9354

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 1 commit into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/UTF16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension _Unicode.UTF16 : UnicodeEncoding {
return UnicodeScalar(_unchecked: value)
}

public static func encode(_ source: UnicodeScalar) -> EncodedScalar {
public static func encode(_ source: UnicodeScalar) -> EncodedScalar? {
let x = source.value
if _fastPath(x < (1 << 16)) {
return EncodedScalar(_storage: x, _bitCount: 16)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/UTF32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension _Unicode.UTF32 : UnicodeEncoding {
}

@inline(__always)
public static func encode(_ source: UnicodeScalar) -> EncodedScalar {
public static func encode(_ source: UnicodeScalar) -> EncodedScalar? {
return EncodedScalar(source.value)
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/UTF8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension _Unicode.UTF8 : UnicodeEncoding {

@inline(__always)
@_inlineable
public static func encode(_ source: UnicodeScalar) -> EncodedScalar {
public static func encode(_ source: UnicodeScalar) -> EncodedScalar? {
var c = source.value
if _fastPath(c < (1&<<7)) {
return EncodedScalar(_storage: c, _bitCount: 8)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Unicode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ extension _Unicode.UTF8 : UnicodeCodec {
_ input: UnicodeScalar,
into processCodeUnit: (CodeUnit) -> Void
) {
var s = encode(input)._storage
var s = encode(input)!._storage
processCodeUnit(UInt8(extendingOrTruncating: s))
s &>>= 8
if _fastPath(s == 0) { return }
Expand Down Expand Up @@ -414,7 +414,7 @@ extension _Unicode.UTF16 : UnicodeCodec {
_ input: UnicodeScalar,
into processCodeUnit: (CodeUnit) -> Void
) {
var s = encode(input)._storage
var s = encode(input)!._storage
processCodeUnit(UInt16(extendingOrTruncating: s))
s &>>= 16
if _fastPath(s == 0) { return }
Expand Down
5 changes: 3 additions & 2 deletions stdlib/public/core/UnicodeEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public protocol _UnicodeEncoding {
/// Converts from encoded to encoding-independent representation
static func decode(_ content: EncodedScalar) -> UnicodeScalar

/// Converts from encoding-independent to encoded representation
static func encode(_ content: UnicodeScalar) -> EncodedScalar
/// Converts from encoding-independent to encoded representation, returning
/// `nil` if the scalar can't be represented in this encoding.
static func encode(_ content: UnicodeScalar) -> EncodedScalar?

associatedtype ForwardParser : UnicodeParser
associatedtype ReverseParser : UnicodeParser
Expand Down
4 changes: 2 additions & 2 deletions test/Prototypes/UnicodeDecoders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ func checkDecodeUTF<Codec : UnicodeCodec & UnicodeEncoding>(
decoded.append(scalar)
expectEqual(
UnicodeScalar(scalar),
Codec.decode(Codec.encode(UnicodeScalar(scalar)!)))
Codec.decode(Codec.encode(UnicodeScalar(scalar)!)!))
}

func output1(_ scalar: UnicodeScalar) {
decoded.append(scalar.value)
expectEqual(scalar, Codec.decode(Codec.encode(scalar)))
expectEqual(scalar, Codec.decode(Codec.encode(scalar)!))
}

var result = assertionSuccess()
Expand Down