mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] String and range expressions
* finish string documentation revisions * revise examples throughout to use range expressions instead of e.g. prefix(upTo: _)
This commit is contained in:
@@ -34,10 +34,10 @@ extension String {
|
||||
///
|
||||
/// Some characters that are visible in a string are made up of more than one
|
||||
/// Unicode scalar value. In that case, a string's `unicodeScalars` view
|
||||
/// contains more values than its `characters` view.
|
||||
/// contains more elements than the string itself.
|
||||
///
|
||||
/// let flag = "🇵🇷"
|
||||
/// for c in flag.characters {
|
||||
/// for c in flag {
|
||||
/// print(c)
|
||||
/// }
|
||||
/// // 🇵🇷
|
||||
@@ -53,7 +53,7 @@ extension String {
|
||||
///
|
||||
/// let favemoji = "My favorite emoji is 🎉"
|
||||
/// if let i = favemoji.unicodeScalars.index(where: { $0.value >= 128 }) {
|
||||
/// let asciiPrefix = String(favemoji.unicodeScalars.prefix(upTo: i))
|
||||
/// let asciiPrefix = String(favemoji.unicodeScalars[..<i])
|
||||
/// print(asciiPrefix)
|
||||
/// }
|
||||
/// // Prints "My favorite emoji is "
|
||||
@@ -94,10 +94,10 @@ extension String {
|
||||
/// position in the Unicode scalars view:
|
||||
///
|
||||
/// let hearts = "Hearts <3 ♥︎ 💘"
|
||||
/// let i = hearts.characters.index(of: "♥︎")!
|
||||
/// let i = hearts.index(of: "♥︎")!
|
||||
///
|
||||
/// let j = i.samePosition(in: hearts.unicodeScalars)
|
||||
/// print(hearts.unicodeScalars.suffix(from: j))
|
||||
/// print(hearts.unicodeScalars[j...])
|
||||
/// // Prints "♥︎ 💘"
|
||||
/// print(hearts.unicodeScalars[j].value)
|
||||
/// // Prints "9829"
|
||||
@@ -309,7 +309,7 @@ extension String {
|
||||
///
|
||||
/// let picnicGuest = "Deserving porcupine"
|
||||
/// if let i = picnicGuest.unicodeScalars.index(of: " ") {
|
||||
/// let adjective = String(picnicGuest.unicodeScalars.prefix(upTo: i))
|
||||
/// let adjective = String(picnicGuest.unicodeScalars[..<i])
|
||||
/// print(adjective)
|
||||
/// }
|
||||
/// // Prints "Deserving"
|
||||
@@ -433,7 +433,7 @@ extension String.UnicodeScalarIndex {
|
||||
/// let utf16Index = cafe.utf16.index(of: 32)!
|
||||
/// let scalarIndex = String.UnicodeScalarView.Index(utf16Index, within: cafe.unicodeScalars)!
|
||||
///
|
||||
/// print(String(cafe.unicodeScalars.prefix(upTo: scalarIndex)))
|
||||
/// print(String(cafe.unicodeScalars[..<scalarIndex]))
|
||||
/// // Prints "Café"
|
||||
///
|
||||
/// If the position passed in `utf16Index` doesn't have an exact
|
||||
@@ -442,10 +442,10 @@ extension String.UnicodeScalarIndex {
|
||||
/// the trailing surrogate of a UTF-16 surrogate pair fails.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - utf16Index: A position in the `utf16` view of the `characters`
|
||||
/// parameter.
|
||||
/// - unicodeScalars: The `UnicodeScalarView` instance referenced by both
|
||||
/// `utf16Index` and the resulting index.
|
||||
/// - utf16Index: A position in the `utf16` view of a string. `utf16Index`
|
||||
/// must be an element of `String(unicodeScalars).utf16.indices`.
|
||||
/// - unicodeScalars: The `UnicodeScalarView` in which to find the new
|
||||
/// position.
|
||||
public init?(
|
||||
_ utf16Index: String.UTF16Index,
|
||||
within unicodeScalars: String.UnicodeScalarView
|
||||
@@ -480,10 +480,10 @@ extension String.UnicodeScalarIndex {
|
||||
/// byte returns `nil`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - utf8Index: A position in the `utf8` view of the `characters`
|
||||
/// parameter.
|
||||
/// - unicodeScalars: The `UnicodeScalarView` instance referenced by both
|
||||
/// `utf8Index` and the resulting index.
|
||||
/// - utf8Index: A position in the `utf8` view of a string. `utf8Index`
|
||||
/// must be an element of `String(unicodeScalars).utf8.indices`.
|
||||
/// - unicodeScalars: The `UnicodeScalarView` in which to find the new
|
||||
/// position.
|
||||
public init?(
|
||||
_ utf8Index: String.UTF8Index,
|
||||
within unicodeScalars: String.UnicodeScalarView
|
||||
@@ -508,22 +508,22 @@ extension String.UnicodeScalarIndex {
|
||||
/// into its corresponding position in the string's `unicodeScalars` view.
|
||||
///
|
||||
/// let cafe = "Café 🍵"
|
||||
/// let characterIndex = cafe.characters.index(of: "🍵")!
|
||||
/// let scalarIndex = String.UnicodeScalarView.Index(characterIndex, within: cafe.unicodeScalars)
|
||||
/// let stringIndex = cafe.index(of: "🍵")!
|
||||
/// let scalarIndex = String.UnicodeScalarView.Index(stringIndex, within: cafe.unicodeScalars)
|
||||
///
|
||||
/// print(cafe.unicodeScalars.suffix(from: scalarIndex))
|
||||
/// print(cafe.unicodeScalars[scalarIndex...])
|
||||
/// // Prints "🍵"
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - characterIndex: A position in a `CharacterView` instance.
|
||||
/// `characterIndex` must be an element of
|
||||
/// `String(utf8).characters.indices`.
|
||||
/// - utf8: The `UTF8View` in which to find the new position.
|
||||
/// - index: A position in a string. `index` must be an element of
|
||||
/// `String(unicodeScalars).indices`.
|
||||
/// - unicodeScalars: The `UnicodeScalarView` in which to find the new
|
||||
/// position.
|
||||
public init(
|
||||
_ characterIndex: String.Index,
|
||||
_ index: String.Index,
|
||||
within unicodeScalars: String.UnicodeScalarView
|
||||
) {
|
||||
self.init(_position: characterIndex._base._position)
|
||||
self.init(_position: index._base._position)
|
||||
}
|
||||
|
||||
/// Returns the position in the given UTF-8 view that corresponds exactly to
|
||||
@@ -537,7 +537,7 @@ extension String.UnicodeScalarIndex {
|
||||
/// let cafe = "Café"
|
||||
/// if let i = cafe.unicodeScalars.index(of: "é") {
|
||||
/// let j = i.samePosition(in: cafe.utf8)
|
||||
/// print(Array(cafe.utf8.suffix(from: j)))
|
||||
/// print(Array(cafe.utf8[j...]))
|
||||
/// }
|
||||
/// // Prints "[195, 169]"
|
||||
///
|
||||
@@ -556,7 +556,7 @@ extension String.UnicodeScalarIndex {
|
||||
/// this method find the same position in the string's `utf16` view.
|
||||
///
|
||||
/// let cafe = "Café"
|
||||
/// if let i = cafe.characters.index(of: "é") {
|
||||
/// if let i = cafe.unicodeScalars.index(of: "é") {
|
||||
/// let j = i.samePosition(in: cafe.utf16)
|
||||
/// print(cafe.utf16[j])
|
||||
/// }
|
||||
@@ -582,7 +582,7 @@ extension String.UnicodeScalarIndex {
|
||||
/// let cafe = "Café 🍵"
|
||||
/// let i = cafe.unicodeScalars.index(of: "🍵")
|
||||
/// let j = i.samePosition(in: cafe)!
|
||||
/// print(cafe.suffix(from: j))
|
||||
/// print(cafe[j...])
|
||||
/// // Prints "🍵"
|
||||
///
|
||||
/// - Parameter characters: The string to use for the index conversion.
|
||||
|
||||
Reference in New Issue
Block a user