Skip to content

[stdlib] Add Character.unicodeScalars #9664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(SWIFTLIB_ESSENTIAL
Builtin.swift
BuiltinMath.swift.gyb
Character.swift
CharacterUnicodeScalars.swift
CocoaArray.swift
Codable.swift
Collection.swift
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,3 @@ extension Character : Comparable {
}
}
}

83 changes: 83 additions & 0 deletions stdlib/public/core/CharacterUnicodeScalars.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//===--- CharacterUnicodeScalars.swift ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension Character {
public struct UnicodeScalarView {
internal let _base: String.UnicodeScalarView
}

public var unicodeScalars : UnicodeScalarView {
return UnicodeScalarView(_base: String(self).unicodeScalars)
}
}

extension Character.UnicodeScalarView {
public struct Iterator {
internal var _base: String.UnicodeScalarView.Iterator
}
}

extension Character.UnicodeScalarView.Iterator : IteratorProtocol {
public mutating func next() -> UnicodeScalar? {
return _base.next()
}
}

extension Character.UnicodeScalarView : Sequence {
public func makeIterator() -> Iterator {
return Iterator(_base: _base.makeIterator())
}
}

extension Character.UnicodeScalarView {
public struct Index {
internal let _base: String.UnicodeScalarView.Index
}
}

extension Character.UnicodeScalarView.Index : Equatable {
public static func == (
lhs: Character.UnicodeScalarView.Index,
rhs: Character.UnicodeScalarView.Index
) -> Bool {
return lhs._base == rhs._base
}
}

extension Character.UnicodeScalarView.Index : Comparable {
public static func < (
lhs: Character.UnicodeScalarView.Index,
rhs: Character.UnicodeScalarView.Index
) -> Bool {
return lhs._base < rhs._base
}
}

extension Character.UnicodeScalarView : Collection {
public var startIndex: Index {
return Index(_base: _base.startIndex)
}
public var endIndex: Index {
return Index(_base: _base.endIndex)
}
public func index(after i: Index) -> Index {
return Index(_base: _base.index(after: i._base))
}
public subscript(_ i: Index) -> UnicodeScalar {
return _base[i._base]
}
}

extension Character.UnicodeScalarView : BidirectionalCollection {
public func index(before i: Index) -> Index {
return Index(_base: _base.index(before: i._base))
}
}
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"ASCII.swift",
"CString.swift",
"Character.swift",
"CharacterUnicodeScalars.swift",
"StaticString.swift",
"String.swift",
"StringBridge.swift",
Expand Down
25 changes: 22 additions & 3 deletions test/stdlib/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ func isSmallRepresentation(_ s: String) -> Bool {
}
}

func checkUnicodeScalars(_ s: String) {
let c = s.first!
expectEqualSequence(s.unicodeScalars, c.unicodeScalars)

expectEqualSequence(
s.unicodeScalars, c.unicodeScalars.indices.map { c.unicodeScalars[$0] })

expectEqualSequence(
s.unicodeScalars.reversed(), c.unicodeScalars.reversed())

expectEqualSequence(
s.unicodeScalars.reversed(), c.unicodeScalars.indices.reversed().map {
c.unicodeScalars[$0]
})
}

func checkRepresentation(_ s: String) {
let expectSmall = s.utf8.count <= 8
let isSmall = isSmallRepresentation(s)
Expand All @@ -231,13 +247,15 @@ func checkRepresentation(_ s: String) {

CharacterTests.test("RoundTripping") {
// Single Unicode Scalar Value tests
for s in baseScalars {
checkRepresentation(String(s))
checkRoundTripThroughCharacter(String(s))
for s in baseScalars.lazy.map(String.init) {
checkUnicodeScalars(s)
checkRepresentation(s)
checkRoundTripThroughCharacter(s)
}

// Edge case tests
for s in testCharacters {
checkUnicodeScalars(s)
checkRepresentation(s)
checkRoundTripThroughCharacter(s)
}
Expand All @@ -249,6 +267,7 @@ CharacterTests.test("RoundTripping/Random") {
// Character's small representation variant has 63 bits. Making
// the maximum length 9 scalars tests both sides of the limit.
var s = randomGraphemeCluster(1, 9)
checkUnicodeScalars(s)
checkRepresentation(s)
checkRoundTripThroughCharacter(s)
}
Expand Down