File: StringCompatibility.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (273 lines) | stat: -rw-r--r-- 8,263 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-codesign %t/a.out4 && %target-run %t/a.out4

// REQUIRES: executable_test
// REQUIRES: reflection

import StdlibUnittest

//===--- MyString ---------------------------------------------------------===//
/// A simple StringProtocol with a wacky .description that proves
/// LosslessStringConvertible is not infecting ordinary constructions by using
/// .description as the content of a copied string.
struct MyString {
  var base: String
}

extension MyString : BidirectionalCollection {
  typealias Iterator = String.Iterator
  typealias Index = String.Index
  typealias SubSequence = MyString
  func makeIterator() -> Iterator { return base.makeIterator() }
  var startIndex: String.Index { return base.startIndex }
  var endIndex: String.Index { return base.startIndex }
  subscript(i: Index) -> Character { return base[i] }
  subscript(indices: Range<Index>) -> MyString {
    return MyString(base: String(self.base[indices]))
  }
  func index(after i: Index) -> Index { return base.index(after: i) }
  func index(before i: Index) -> Index { return base.index(before: i) }
  func index(_ i: Index, offsetBy n: Int) -> Index {
    return base.index(i, offsetBy: n)
  }
  func distance(from i: Index, to j: Index) -> Int {
    return base.distance(from: i, to: j)
  }
}

extension MyString : RangeReplaceableCollection {
  init() { base = "" }
  mutating func append<S: Sequence>(contentsOf s: S)
  where S.Element == Character {
    base.append(contentsOf: s)
  }
  mutating func replaceSubrange<C: Collection>(_ r: Range<Index>, with c: C)
  where C.Element == Character {
    base.replaceSubrange(r, with: c)
  }
}

extension MyString : CustomStringConvertible {
  var description: String { return "***MyString***" }
}

extension MyString : TextOutputStream {
  public mutating func write(_ other: String) {
    append(contentsOf: other)
  }
}

extension MyString : TextOutputStreamable {
  public func write<Target : TextOutputStream>(to target: inout Target) {
    target.write(base)
  }
}

extension MyString : ExpressibleByUnicodeScalarLiteral {
  public init(unicodeScalarLiteral value: String) {
    base = .init(unicodeScalarLiteral: value)
  }
}
extension MyString : ExpressibleByExtendedGraphemeClusterLiteral {
  public init(extendedGraphemeClusterLiteral value: String) {
    base = .init(extendedGraphemeClusterLiteral: value)
  }
}

extension MyString : ExpressibleByStringLiteral {
  public init(stringLiteral value: String) {
    base = .init(stringLiteral: value)
  }
}

extension MyString : CustomReflectable {
  public var customMirror: Mirror {
    return base.customMirror
  }
}

extension MyString : CustomPlaygroundQuickLookable {
  public var customPlaygroundQuickLook: PlaygroundQuickLook {
    return base.customPlaygroundQuickLook
  }
}

extension MyString : CustomDebugStringConvertible {
  public var debugDescription: String {
    return "(***MyString***)"
  }
}

extension MyString : Equatable {
  public static func ==(lhs: MyString, rhs: MyString) -> Bool {
    return lhs.base == rhs.base
  }
}

extension MyString : Comparable {
  public static func <(lhs: MyString, rhs: MyString) -> Bool {
    return lhs.base < rhs.base
  }
}

extension MyString : Hashable {
  public var hashValue : Int {
    return base.hashValue
  }
}

extension MyString {
  public func hasPrefix(_ prefix: String) -> Bool {
    return self.base.hasPrefix(prefix)
  }

  public func hasSuffix(_ suffix: String) -> Bool {
    return self.base.hasSuffix(suffix)
  }
}

extension MyString : StringProtocol {
  var utf8: String.UTF8View { return base.utf8 }
  var utf16: String.UTF16View { return base.utf16 }
  var unicodeScalars: String.UnicodeScalarView { return base.unicodeScalars }
  var characters: String.CharacterView { return base.characters }
  func lowercased() -> String {
    return base.lowercased()
  }
  func uppercased() -> String {
    return base.uppercased()
  }

  init<C: Collection, Encoding: Unicode.Encoding>(
    decoding codeUnits: C, as sourceEncoding: Encoding.Type
  )
  where C.Iterator.Element == Encoding.CodeUnit {
    base = .init(decoding: codeUnits, as: sourceEncoding)
  }

  init(cString nullTerminatedUTF8: UnsafePointer<CChar>) {
    base = .init(cString: nullTerminatedUTF8)
  }
  
  init<Encoding: Unicode.Encoding>(
    decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>,
    as sourceEncoding: Encoding.Type) {
    base = .init(decodingCString: nullTerminatedCodeUnits, as: sourceEncoding)
  }
  
  func withCString<Result>(
    _ body: (UnsafePointer<CChar>) throws -> Result) rethrows -> Result {
    return try base.withCString(body)
  }

  func withCString<Result, Encoding: Unicode.Encoding>(
    encodedAs targetEncoding: Encoding.Type,
    _ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result
  ) rethrows -> Result {
    return try base.withCString(encodedAs: targetEncoding, body)
  }
}
//===----------------------------------------------------------------------===//

public typealias ExpectedConcreteSlice = Substring
public typealias ExpectedStringFromString = String
let swift = 4

var Tests = TestSuite("StringCompatibility")

Tests.test("String/Range/Slice/ExpectedType/\(swift)") {
  var s = "hello world"
  var sub = s[s.startIndex ..< s.endIndex]
  var subsub = sub[s.startIndex ..< s.endIndex]

  expectType(String.self, &s)
  expectType(ExpectedConcreteSlice.self, &sub)
  expectType(ExpectedConcreteSlice.self, &subsub)
}

Tests.test("String/ClosedRange/Slice/ExpectedType/\(swift)") {
  var s = "hello world"
  let lastIndex = s.index(before:s.endIndex)
  var sub = s[s.startIndex ... lastIndex]
  var subsub = sub[s.startIndex ... lastIndex]

  expectType(String.self, &s)
  expectType(ExpectedConcreteSlice.self, &sub)
  expectType(ExpectedConcreteSlice.self, &subsub)
}

Tests.test("Substring/Range/Slice/ExpectedType/\(swift)") {
  let s = "hello world" as Substring
  var sub = s[s.startIndex ..< s.endIndex]
  var subsub = sub[s.startIndex ..< s.endIndex]

  // slicing a String in Swift 3 produces a String
  // but slicing a Substring should still produce a Substring
  expectType(Substring.self, &sub)
  expectType(Substring.self, &subsub)
}

Tests.test("Substring/ClosedRange/Slice/ExpectedType/\(swift)") {
  let s = "hello world" as Substring
  let lastIndex = s.index(before:s.endIndex)
  var sub = s[s.startIndex ... lastIndex]
  var subsub = sub[s.startIndex ... lastIndex]

  expectType(ExpectedConcreteSlice.self, &sub)
  expectType(ExpectedConcreteSlice.self, &subsub)
}

Tests.test("RangeReplaceable.init/generic/\(swift)") {
  func check<
    T: RangeReplaceableCollection, S: Collection
  >(_: T.Type, from source: S)
  where T.Element : Equatable, T.Element == S.Element
  {
    var r = T(source)
    expectType(T.self, &r)
    expectEqualSequence(Array(source), Array(r))
  }

  check(String.self, from: "a" as String)
  check(Substring.self, from: "b" as String)
  // FIXME: Why isn't this working?
  // check(MyString.self, from: "c" as String)
  
  check(String.self, from: "d" as Substring)
  check(Substring.self, from: "e" as Substring)
  // FIXME: Why isn't this working?
  // check(MyString.self, from: "f" as Substring)

  // FIXME: Why isn't this working?
  // check(String.self, from: "g" as MyString)
  // check(Substring.self, from: "h" as MyString)
  check(MyString.self, from: "i" as MyString)
}

Tests.test("String.init(someString)/default type/\(swift)") {
  var s = String("" as String)
  expectType(ExpectedStringFromString.self, &s)
}

Tests.test("Substring.init(someString)/default type/\(swift)") {
  var s = Substring("" as Substring)
  expectType(Substring.self, &s)
}

Tests.test("LosslessStringConvertible/generic/\(swift)") {
  func f<T : LosslessStringConvertible>(_ x: T.Type) {
    _ = T("")! // unwrapping optional should work in generic context
  }
  f(String.self)
}

public typealias ExpectedUTF8ViewSlice = String.UTF8View.SubSequence

Tests.test("UTF8ViewSlicing") {
  let s = "Hello, String.UTF8View slicing world!".utf8
  var slice = s[s.startIndex..<s.endIndex]
  expectType(ExpectedUTF8ViewSlice.self, &slice)
  _ = s[s.startIndex..<s.endIndex] as String.UTF8View.SubSequence
}

runAllTests()