类型方法
transcoded
transcodedLength(of:decodedAs:repairingIllFormedSequences:)
返回转码为 UTF-16 时给定代码单元序列所需的 UTF-16 代码单元数,以及指示是否发现该序列仅包含 ASCII 字符的布尔值。
声明
static func transcodedLength<Input, Encoding>(
of input: Input,
decodedAs sourceEncoding: Encoding.Type,
repairingIllFormedSequences: Bool) -> (count: Int, isASCII: Bool
)? where Input : IteratorProtocol, Encoding : _UnicodeEncoding, Input.Element == Encoding.CodeUnit
返回值
一个元组,包含编码 input
所需的 UTF-16 代码单元数和一个指示 input
是否仅包含 ASCII 字符的布尔值。如果 repairingIllFormedSequences
是 false
并且检测到 ill-formed 序列,则此方法返回 nil
。
参数
input
要翻译的代码单元的迭代器,编码为
sourceEncoding
。如果repairingIllFormedSequences
是true
,则整个迭代器将被耗尽。否则,如果检测到ill-formed 序列,迭代将停止。sourceEncoding
input
的 Unicode 编码。repairingIllFormedSequences
通过
true
来测量input
的长度,即使input
包含ill-formed 序列。每个 ill-formed 序列都被替换为 Unicode 替换字符 ("\u{FFFD}"
) 并按此进行测量。当遇到ill-formed 序列时,通过false
立即停止测量input
。
详述
以下示例查找字符串 "Fermata 𝄐"
的 UTF-16 编码的长度,从其 UTF-8 表示开始。
let fermata = "Fermata 𝄐"
let bytes = fermata.utf8
print(Array(bytes))
// Prints "[70, 101, 114, 109, 97, 116, 97, 32, 240, 157, 132, 144]"
let result = UTF16.transcodedLength(of: bytes.makeIterator(),
decodedAs: UTF8.self,
repairingIllFormedSequences: false)
print(result)
// Prints "Optional((count: 10, isASCII: false))"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift Unicode.UTF16 trailSurrogate(_:)用法及代码示例
- Swift Unicode.UTF16 encode(_:into:)用法及代码示例
- Swift Unicode.UTF16 isTrailSurrogate(_:)用法及代码示例
- Swift Unicode.UTF16 leadSurrogate(_:)用法及代码示例
- Swift Unicode.UTF16 isLeadSurrogate(_:)用法及代码示例
- Swift Unicode.UTF16 width(_:)用法及代码示例
- Swift Unicode.UTF16 decode(_:)用法及代码示例
- Swift Unicode.UTF32 encode(_:into:)用法及代码示例
- Swift Unicode.UTF8 width(_:)用法及代码示例
- Swift Unicode.UTF8 isContinuation(_:)用法及代码示例
- Swift Unicode.UTF8 decode(_:)用法及代码示例
- Swift Unicode.UTF32 decode(_:)用法及代码示例
- Swift Unicode.UTF8 encode(_:into:)用法及代码示例
- Swift Unicode.CanonicalCombiningClass ...(_:_:)用法及代码示例
- Swift Unicode.Scalar.UTF16View contains(where:)用法及代码示例
- Swift Unicode.Scalar.UTF8View dropFirst(_:)用法及代码示例
- Swift Unicode.Scalar.UTF16View firstIndex(of:)用法及代码示例
- Swift Unicode.Scalar.UTF16View randomElement()用法及代码示例
- Swift Unicode.Scalar.UTF8View contains(where:)用法及代码示例
- Swift Unicode.Scalar.UTF16View forEach(_:)用法及代码示例
- Swift Unicode.Scalar.UTF8View index(_:offsetBy:limitedBy:)用法及代码示例
- Swift Unicode.Scalar.UTF16View allSatisfy(_:)用法及代码示例
- Swift Unicode.Scalar.UTF8View elementsEqual(_:)用法及代码示例
- Swift Unicode.CanonicalCombiningClass ..<(_:)用法及代码示例
- Swift Unicode.Scalar.UTF16View prefix(through:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Unicode.UTF16 transcodedLength(of:decodedAs:repairingIllFormedSequences:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。