示例工程 03-strings-unicode
另有 1 个文件未随站点发布(平台工程、二进制或超过 200KB)。
03-strings-unicode/Sources/Ch03Kit/GraphemeLab.swift
/// 字素簇(grapheme cluster):`count` 数的是「用户看到的字符」,不是字节、码元或码点。
public enum GraphemeLab {
public static let flag = "🇨🇳"
public static let family = "👨👩👧👦"
public static let skinTone = "👍🏽"
public static let combined = "e\u{301}"
/// 形如 `count/utf8/utf16/scalars`,四项分别对应四种计数口径。
public static func metrics(_ text: String) -> String {
"\(text.count)/\(text.utf8.count)/\(text.utf16.count)/\(text.unicodeScalars.count)"
}
}
/// 组合等价:预组合的 `é` 与 `e` + 组合重音在 `==` 下相等,但字节数不同。
public enum CanonicalEquivalence {
public static let composed = "é"
public static let decomposed = "e\u{301}"
public static let equalUnderComparison: Bool = composed == decomposed
public static let composedUTF8Count: Int = composed.utf8.count
public static let decomposedUTF8Count: Int = decomposed.utf8.count
}