Unicode and Character Indices
Lesson 23 of 48 in Coddy's Swift for GDScript Developers course.
GDScript String length counts Unicode code points, so an e followed by a combining acute accent has length two. Equality compares the text sequence without automatically treating that decomposed spelling as the same sequence as precomposed é. UTF-8 encoding has three bytes for the decomposed text.
Swift String.count counts Character values, which are extended grapheme clusters, and String equality recognizes canonical equivalence. The target comparison therefore reports one Character, equality true and three UTF-8 bytes. Use String.Index for positions rather than an integer offset into characters.
Grapheme segmentation and normalization are distinct concepts. Neither code-point count nor encoded byte length generally equals the number of visible characters a user perceives.
Complete GDScript comparison program:
extends SceneTree
func _init():
var text = "e\u0301"
print(text.length())
print(text == "é")
print(text.to_utf8_buffer().size())
quit()
Complete Swift comparison program, compiled from these files:
main.swift
let text = "e\u{301}"
print(text.count)
print(text == "é")
print(text.utf8.count)Challenge
EasyImplement uniqueCharacters(_:) using Swift Character equality: count distinct extended grapheme clusters. Input is one line of at most 50 Characters without line breaks. It may include composed accents or emoji. Output the count. Canonically equivalent Characters compare equal.
Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.
Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
import Foundation
func apiChecks(_ input: String) -> Bool {
return uniqueCharacters("🇨🇦🇨🇦x") == 2 && uniqueCharacters("éé") == 1 && uniqueCharacters("") == 0
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for GDScript Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry Rules5Collection Tools
Dictionaries and Missing KeysSets and MembershipUnicode and Character IndicesClosures and TransformationsRecap: Catalog MergePractice on your own: Swift playground