mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
<rdar://20494686> String itsef should only expose Unicode-correct algorithms, like proper substring/prefix/suffix search, enumerating words/lines/paragraphs, case folding etc. Promoting sequence-centric algorithms to methods on String is not acceptable since it invites users to write wrong code. Thus, String has to lose its SequenceType conformance. Nevertheless, we recognize that sometimes it is useful to manipulate the String contents on lower levels (UTF-8, UTF-16, Unicode scalars, extended grapheme clusters), for example, when implementing high-level Unicode operations, so we can't remove low-level operations altogether. For this reason, String provides nested "views" for the first three low-level representations, but grapheme clusters were in a privileged position -- String itself is a collection of grapheme clusters. We propose to add a characters view that will represent the String as a collection of Character values. Swift SVN r28065
45 lines
1.2 KiB
Swift
45 lines
1.2 KiB
Swift
//===--- StringCharacterView.swift - String's Collection of Characters ----===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// String is-not-a SequenceType or CollectionType, but it exposes a
|
|
// collection of characters.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
extension String {
|
|
public struct CharacterView {
|
|
internal var _core: _StringCore
|
|
|
|
public init(_ s: String) {
|
|
self._core = s._core
|
|
}
|
|
|
|
public // @testable
|
|
init(_ _core: _StringCore) {
|
|
self._core = _core
|
|
}
|
|
}
|
|
|
|
public var characters: CharacterView {
|
|
return CharacterView(self)
|
|
}
|
|
|
|
public mutating func withMutableCharacters<R>(body: (inout CharacterView)->R) -> R {
|
|
var tmp = CharacterView("")
|
|
swap(&_core, &tmp._core)
|
|
let r = body(&tmp)
|
|
swap(&_core, &tmp._core)
|
|
return r
|
|
}
|
|
}
|
|
|