1010//
1111//===----------------------------------------------------------------------===//
1212
13- /// `Character` represents some Unicode grapheme cluster as
14- /// defined by a canonical, localized, or otherwise tailored
15- /// segmentation algorithm.
13+ /// A single extended grapheme cluster, which approximates a user-perceived
14+ /// character.
15+ ///
16+ /// The `Character` type represents a character made up of one or more Unicode
17+ /// scalar values, grouped by a Unicode boundary algorithm. Generally, a
18+ /// `Character` instance matches what the reader of a string will perceive as
19+ /// a single character. The number of visible characters is generally the most
20+ /// natural way to count the length of a string.
21+ ///
22+ /// let greeting = "Hello! 🐥"
23+ /// print("Character count: \(greeting.characters.count)")
24+ /// // Prints "Character count: 8"
25+ ///
26+ /// Because each character in a string can be made up of one or more Unicode
27+ /// code points, the number of characters in a string may not match the length
28+ /// of the Unicode code point representation or the length of the string in a
29+ /// particular binary representation.
30+ ///
31+ /// print("Unicode code point count: \(greeting.unicodeScalars.count)")
32+ /// // Prints "Unicode code point count: 15"
33+ ///
34+ /// print("UTF-8 representation count: \(greeting.utf8.count)")
35+ /// // Prints "UTF-8 representation count: 18"
36+ ///
37+ /// Every `Character` instance is composed of one or more Unicode code points
38+ /// that are grouped together as an *extended grapheme cluster*. The way these
39+ /// code points are grouped is defined by a canonical, localized, or otherwise
40+ /// tailored Unicode segmentation algorithm.
41+ ///
42+ /// For example, a country's Unicode flag character is made up of two regional
43+ /// indicator code points that correspond to that country's ISO 3166-1 alpha-2
44+ /// code. The alpha-2 code for The United States is "US", so its flag
45+ /// character is made up of the Unicode code points `"\u{1F1FA}"` (REGIONAL
46+ /// INDICATOR SYMBOL LETTER U) and `"\u{1F1F8}"` (REGIONAL INDICATOR SYMBOL
47+ /// LETTER S). When placed next to each other in a Swift string literal, these
48+ /// two code points are combined into a single grapheme cluster, represented
49+ /// by a `Character` instance in Swift.
50+ ///
51+ /// let usFlag: Character = "\u{1F1FA}\u{1F1F8}"
52+ /// print(usFlag)
53+ /// // Prints "🇺🇸"
54+ ///
55+ /// For more information about the Unicode terms used in this discussion, see
56+ /// the [Unicode.org glossary][glossary]. In particular, this discussion
57+ /// mentions [extended grapheme clusters][clusters] and [Unicode scalar
58+ /// values][scalars].
59+ ///
60+ /// [glossary]: http://www.unicode.org/glossary/
61+ /// [clusters]: http://www.unicode.org/glossary/#extended_grapheme_cluster
62+ /// [scalars]: http://www.unicode.org/glossary/#unicode_scalar_value
1663public struct Character :
1764 _BuiltinExtendedGraphemeClusterLiteralConvertible ,
1865 ExtendedGraphemeClusterLiteralConvertible , Equatable , Hashable , Comparable {
@@ -33,7 +80,9 @@ public struct Character :
3380 case small( Builtin . Int63 )
3481 }
3582
36- /// Construct a `Character` containing just the given `scalar`.
83+ /// Creates a character containing the given Unicode scalar value.
84+ ///
85+ /// - Parameter scalar: The Unicode scalar value to convert into a character.
3786 public init ( _ scalar: UnicodeScalar ) {
3887 var asInt : UInt64 = 0
3988 var shift : UInt64 = 0
@@ -55,7 +104,17 @@ public struct Character :
55104 UTF32 . self, input: CollectionOfOne ( UInt32 ( value) ) ) )
56105 }
57106
58- /// Create an instance initialized to `value`.
107+ /// Creates a character with the specified value.
108+ ///
109+ /// Don't call this initializer directly. It is used by the compiler when you
110+ /// use a string literal to initialize a `Character` instance. For example:
111+ ///
112+ /// let snowflake: Character = "❄︎"
113+ /// print(snowflake)
114+ /// // Prints "❄︎"
115+ ///
116+ /// The assignment to the `snowflake` constant calls this initializer behind
117+ /// the scenes.
59118 public init ( unicodeScalarLiteral value: Character ) {
60119 self = value
61120 }
@@ -73,14 +132,31 @@ public struct Character :
73132 isASCII: isASCII) )
74133 }
75134
76- /// Create an instance initialized to `value`.
135+ /// Creates a character with the specified value.
136+ ///
137+ /// Don't call this initializer directly. It is used by the compiler when you
138+ /// use a string literal to initialize a `Character` instance. For example:
139+ ///
140+ /// let oBreve: Character = "o\u{306}"
141+ /// print(oBreve)
142+ /// // Prints "ŏ"
143+ ///
144+ /// The assignment to the `oBreve` constant calls this initializer behind the
145+ /// scenes.
77146 public init ( extendedGraphemeClusterLiteral value: Character ) {
78147 self = value
79148 }
80149
81- /// Create an instance from a single-character `String`.
150+ /// Creates a character from a single-character string.
151+ ///
152+ /// The following example creates a new character from the uppercase version
153+ /// of a string that only holds one character.
154+ ///
155+ /// let a = "a"
156+ /// let capitalA = Character(a.uppercased())
82157 ///
83- /// - Precondition: `s` contains exactly one extended grapheme cluster.
158+ /// - Parameter s: The single-character string to convert to a `Character`
159+ /// instance. `s` must contain exactly one extended grapheme cluster.
84160 public init ( _ s: String ) {
85161 // The small representation can accept up to 8 code units as long
86162 // as the last one is a continuation. Since the high bit of the
@@ -260,13 +336,10 @@ public struct Character :
260336 var data : UInt64
261337 }
262338
263- /// The hash value.
339+ /// The character's hash value.
264340 ///
265- /// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`.
266- ///
267- /// - Note: The hash value is not guaranteed to be stable across
268- /// different invocations of the same program. Do not persist the
269- /// hash value across program runs.
341+ /// Hash values are not guaranteed to be equal across different executions of
342+ /// your program. Do not save hash values to use during a future execution.
270343 public var hashValue : Int {
271344 // FIXME(performance): constructing a temporary string is extremely
272345 // wasteful and inefficient.
@@ -283,14 +356,16 @@ public struct Character :
283356}
284357
285358extension Character : CustomDebugStringConvertible {
286- /// A textual representation of `self` , suitable for debugging.
359+ /// A textual representation of the character , suitable for debugging.
287360 public var debugDescription : String {
288361 return String ( self ) . debugDescription
289362 }
290363}
291364
292365extension String {
293- /// Construct an instance containing just the given `Character`.
366+ /// Creates a string containing the given character.
367+ ///
368+ /// - Parameter c: The character to convert to a string.
294369 public init ( _ c: Character ) {
295370 switch c. _representation {
296371 case let . small( _63bits) :
0 commit comments