10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
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
16
63
public struct Character :
17
64
_BuiltinExtendedGraphemeClusterLiteralConvertible ,
18
65
ExtendedGraphemeClusterLiteralConvertible , Equatable , Hashable , Comparable {
@@ -33,7 +80,9 @@ public struct Character :
33
80
case small( Builtin . Int63 )
34
81
}
35
82
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.
37
86
public init ( _ scalar: UnicodeScalar ) {
38
87
var asInt : UInt64 = 0
39
88
var shift : UInt64 = 0
@@ -55,7 +104,17 @@ public struct Character :
55
104
UTF32 . self, input: CollectionOfOne ( UInt32 ( value) ) ) )
56
105
}
57
106
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.
59
118
public init ( unicodeScalarLiteral value: Character ) {
60
119
self = value
61
120
}
@@ -73,14 +132,31 @@ public struct Character :
73
132
isASCII: isASCII) )
74
133
}
75
134
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.
77
146
public init ( extendedGraphemeClusterLiteral value: Character ) {
78
147
self = value
79
148
}
80
149
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())
82
157
///
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.
84
160
public init ( _ s: String ) {
85
161
// The small representation can accept up to 8 code units as long
86
162
// as the last one is a continuation. Since the high bit of the
@@ -258,13 +334,10 @@ public struct Character :
258
334
var data : UInt64
259
335
}
260
336
261
- /// The hash value.
337
+ /// The character's hash value.
262
338
///
263
- /// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`.
264
- ///
265
- /// - Note: The hash value is not guaranteed to be stable across
266
- /// different invocations of the same program. Do not persist the
267
- /// hash value across program runs.
339
+ /// Hash values are not guaranteed to be equal across different executions of
340
+ /// your program. Do not save hash values to use during a future execution.
268
341
public var hashValue : Int {
269
342
// FIXME(performance): constructing a temporary string is extremely
270
343
// wasteful and inefficient.
@@ -281,14 +354,16 @@ public struct Character :
281
354
}
282
355
283
356
extension Character : CustomDebugStringConvertible {
284
- /// A textual representation of `self` , suitable for debugging.
357
+ /// A textual representation of the character , suitable for debugging.
285
358
public var debugDescription : String {
286
359
return String ( self ) . debugDescription
287
360
}
288
361
}
289
362
290
363
extension String {
291
- /// Construct an instance containing just the given `Character`.
364
+ /// Creates a string containing the given character.
365
+ ///
366
+ /// - Parameter c: The character to convert to a string.
292
367
public init ( _ c: Character ) {
293
368
switch c. _representation {
294
369
case let . small( _63bits) :
0 commit comments