Static Methodswift 6.0.1Swift
width(_:)
Returns the number of code units required to encode the given Unicode scalar.
static func width(_ x: Unicode.Scalar) -> Int
Parameters
- x
A Unicode scalar value.
Returns
The width of x
when encoded in UTF-8, from 1
to 4
.
Because a Unicode scalar value can require up to 21 bits to store its value, some Unicode scalars are represented in UTF-8 by a sequence of up to 4 code units. The first code unit is designated a lead byte and the rest are continuation bytes.
let anA: Unicode.Scalar = "A"
print(anA.value)
// Prints "65"
print(UTF8.width(anA))
// Prints "1"
let anApple: Unicode.Scalar = "🍎"
print(anApple.value)
// Prints "127822"
print(UTF8.width(anApple))
// Prints "4"
Other members in extension
Types
Typealiases
Type members
init(
) Creates an instance of the UTF-8 codec.
static var encodedReplacementCharacter: Unicode.UTF8.EncodedScalar
static func decode(Unicode.UTF8.EncodedScalar
) -> Unicode.Scalar static func encode(Unicode.Scalar
) -> Unicode.UTF8.EncodedScalar? static func encode(Unicode.Scalar, into: (Unicode.UTF8.CodeUnit) -> Void
) Encodes a Unicode scalar as a series of code units by calling the given closure on each code unit.
static func isASCII(Unicode.UTF8.CodeUnit
) -> Bool Returns whether the given code unit represents an ASCII scalar
static func isContinuation(Unicode.UTF8.CodeUnit
) -> Bool Returns a Boolean value indicating whether the specified code unit is a UTF-8 continuation byte.
static func transcode<FromEncoding>(FromEncoding.EncodedScalar, from: FromEncoding.Type
) -> Unicode.UTF8.EncodedScalar?
Instance members
func decode<I>(inout I
) -> UnicodeDecodingResult Starts or continues decoding a UTF-8 sequence.