Skip to content

Commit ca576bb

Browse files
author
Dave Abrahams
committed
[stdlib] Add unconditional encode/transcode
1 parent f4610e1 commit ca576bb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

stdlib/public/core/UnicodeEncoding.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ extension _UnicodeEncoding {
5858
public static func transcodeIfRepresentable<FromEncoding : UnicodeEncoding>(
5959
_ content: FromEncoding.EncodedScalar, from _: FromEncoding.Type
6060
) -> EncodedScalar? {
61+
return encodeIfRepresentable(FromEncoding.decode(content))
62+
}
63+
64+
/// Converts from encoding-independent to encoded representation, returning
65+
/// `nil` if the scalar can't be represented in this encoding.
66+
static func encode(_ content: UnicodeScalar) -> EncodedScalar {
67+
return encodeIfRepresentable(content) ?? encodedReplacementCharacter
68+
}
69+
70+
/// Converts a scalar from another encoding's representation, returning
71+
/// `nil` if the scalar can't be represented in this encoding.
72+
static func transcode<FromEncoding : UnicodeEncoding>(
73+
_ content: FromEncoding.EncodedScalar, from _: FromEncoding.Type
74+
) -> EncodedScalar {
6175
return encode(FromEncoding.decode(content))
6276
}
6377
}

0 commit comments

Comments
 (0)