File: StringSplitting.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 (307 lines) | stat: -rw-r--r-- 9,487 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//===--- StringSplitting.swift --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
//
//===----------------------------------------------------------------------===//

import TestsUtils

public let benchmarks = [
  BenchmarkInfo(
	  name: "LineSink.bytes.alpha",
	  runFunction: run_LinkSink_bytes_alpha,
	  tags: [.validation, .String],
	  setUpFunction: setup),
  BenchmarkInfo(
	  name: "LineSink.bytes.complex",
	  runFunction: run_LinkSink_bytes_complex,
	  tags: [.validation, .String],
	  setUpFunction: setup),
  BenchmarkInfo(
	  name: "LineSink.scalars.alpha",
	  runFunction: run_LinkSink_scalars_alpha,
	  tags: [.validation, .String],
	  setUpFunction: setup),
  BenchmarkInfo(
	  name: "LineSink.scalars.complex",
	  runFunction: run_LinkSink_scalars_complex,
	  tags: [.validation, .String],
	  setUpFunction: setup),
	BenchmarkInfo(
	  name: "LineSink.characters.alpha",
	  runFunction: run_LinkSink_characters_alpha,
	  tags: [.validation, .String],
	  setUpFunction: setup),
  BenchmarkInfo(
	  name: "LineSink.characters.complex",
	  runFunction: run_LinkSink_characters_complex,
	  tags: [.validation, .String],
	  setUpFunction: setup),
]


// Line-sink benchmarks: Implement `lines`-like functionality
enum View {
  case character
  case scalar
  case utf8
}

@inline(__always) // Constant fold the switch away, inline closures
fileprivate func lineSink(_ workload: String, view: View, sink: (String) -> ()) {
  switch view {
    case .character:
      var iter = workload.makeIterator()
      _linesByCharacters(source: { iter.next() }, sink: sink)
    case .scalar:
      var iter = workload.unicodeScalars.makeIterator()
      _linesByScalars(source: { iter.next() }, sink: sink)
    case .utf8:
      var iter = workload.utf8.makeIterator()
      _linesByBytes(source: { iter.next() }, sink: sink)
  }
}


// Inline always to try to ignore any closure stuff
@inline(__always)
fileprivate func _linesByCharacters(
  source characterSource: () throws -> Character?,
  sink lineSink: (String) -> ()
) rethrows {
  var buffer = ""
  func yield() {
    lineSink(buffer)
    buffer.removeAll(keepingCapacity: true)
  }
  while let c = try characterSource() {
    if c.isNewline {
      yield()
    } else {
      buffer.append(c)
    }
  }

  // Don't emit an empty newline when there is no more content (e.g. end of file)
  if !buffer.isEmpty {
    yield()
  }
}

// Inline always to try to ignore any closure stuff
@inline(__always)
fileprivate func _linesByScalars(
  source scalarSource: () throws -> Unicode.Scalar?,
  sink lineSink: (String) -> ()
) rethrows {
  guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
    fatalError("unavailable")
  }

  var buffer: Array<UInt8> = []
  func yield() {
    lineSink(String(decoding: buffer, as: UTF8.self))
    buffer.removeAll(keepingCapacity: true)
  }

  let _CR = Unicode.Scalar(0x0D)!
  let _LF = Unicode.Scalar(0x0A)!

  while let first = try scalarSource() {
    switch first.value {
    case _CR.value:
      yield()
      guard let second = try scalarSource() else { return }
      if second != _LF { buffer.append(contentsOf: second.utf8) }
    case 0x000A..<0x000D /* LF ..< CR */: yield()
    case 0x0085 /* NEXT LINE (NEL) */: yield()
    case 0x2028 /* LINE SEPARATOR */: yield()
    case 0x2029 /* PARAGRAPH SEPARATOR */: yield()
    default: buffer.append(contentsOf: first.utf8)
    }
  }

  // Don't emit an empty newline when there is no more content (e.g. end of file)
  if !buffer.isEmpty {
    yield()
  }
}


@inline(__always)
fileprivate func _linesByBytes(
  source byteSource: () throws -> UInt8?,
  sink lineSink: (String) -> ()
) rethrows {

  let _CR: UInt8 = 0x0D
  let _LF: UInt8 = 0x0A

  var buffer: Array<UInt8> = []
  func yield() {
    lineSink(String(decoding: buffer, as: UTF8.self))
    buffer.removeAll(keepingCapacity: true)
  }

  /*
    0D 0A: CR-LF
    0A | 0B | 0C | 0D: LF, VT, FF, CR
    E2 80 A8:  U+2028 (LINE SEPARATOR)
    E2 80 A9:  U+2029 (PARAGRAPH SEPARATOR)
  */
  while let first = try byteSource() {
    switch first {
    case _CR:
      yield()
      // Swallow up any subsequent LF
      guard let next = try byteSource() else { return }
      if next != _LF { buffer.append(next) }
    case 0x0A..<0x0D: yield()

    case 0xE2:
      // Try to read: 80 [A8 | A9].
      // If we can't, then we put the byte in the buffer for error correction
      guard let next = try byteSource() else {
        buffer.append(first)
        yield()
        return
      }
      guard next == 0x80 else {
        buffer.append(first)
        buffer.append(next)
        continue
      }
      guard let fin = try byteSource() else {
        buffer.append(first)
        buffer.append(next)
        yield()
        return
      }
      guard fin == 0xA8 || fin == 0xA9 else {
        buffer.append(first)
        buffer.append(next)
        buffer.append(fin)
        continue
      }
      yield()
    default:
      buffer.append(first)
    }
  }
  // Don't emit an empty newline when there is no more content (e.g. end of file)
  if !buffer.isEmpty {
    yield()
  }
}

// 1-byte sequences
private let ascii = "Swift is a multi-paradigm, compiled programming language created for iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code (\"safer\") than Objective-C and also more concise. It is built with the LLVM compiler framework included in Xcode 6 and later and uses the Objective-C runtime, which allows C, Objective-C, C++ and Swift code to run within a single program."

// 2-byte sequences
private let russian = "Ру́сский язы́к один из восточнославянских языков, национальный язык русского народа."
// 3-byte sequences
private let japanese = "日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。"
// 4-byte sequences
// Most commonly emoji, which are usually mixed with other text.
private let emoji = "Panda 🐼, Dog 🐶, Cat 🐱, Mouse 🐭."

private let longComplexNewlines: String = {
  var longStr = ascii + russian + japanese + emoji + "\n"
  var str = longStr
  str += longStr.split(separator: " ").joined(separator: "\n")
  str += longStr.split(separator: " ").joined(separator: "\r\n")
  str += longStr.split(separator: " ").joined(separator: "\r")
  str += longStr.split(separator: " ").joined(separator: "\u{0B}")
  str += longStr.split(separator: " ").joined(separator: "\u{0C}")
  str += longStr.split(separator: " ").joined(separator: "\u{2028}")
  str += longStr.split(separator: " ").joined(separator: "\u{2029}")
  str += "\n\n"
  return str
}()

public func run_LinkSink_bytes_alpha(_ n: Int) {
  let str = alphaInteriorNewlines
  for _ in 0..<(n*50) {
    lineSink(str, view: .utf8, sink: blackHole)
  }
}
public func run_LinkSink_bytes_complex(_ n: Int) {
  let str = longComplexNewlines
  for _ in 0..<n {
    lineSink(str, view: .utf8, sink: blackHole)
  }
}
public func run_LinkSink_scalars_alpha(_ n: Int) {
  let str = alphaInteriorNewlines
  for _ in 0..<(n*50) {
    lineSink(str, view: .scalar, sink: blackHole)
  }
}
public func run_LinkSink_scalars_complex(_ n: Int) {
  let str = longComplexNewlines
  for _ in 0..<n {
    lineSink(str, view: .utf8, sink: blackHole)
  }
}
public func run_LinkSink_characters_alpha(_ n: Int) {
  let str = alphaInteriorNewlines
  for _ in 0..<(n*50) {
    lineSink(str, view: .character, sink: blackHole)
  }
}
public func run_LinkSink_characters_complex(_ n: Int) {
  let str = longComplexNewlines
  for _ in 0..<n {
    lineSink(str, view: .character, sink: blackHole)
  }
}

fileprivate func setup() {
  var utf8Alpha: Array<String> = []
  lineSink(alphaInteriorNewlines, view: .utf8) { utf8Alpha.append($0) }

  var scalarAlpha: Array<String> = []
  lineSink(alphaInteriorNewlines, view: .scalar) { scalarAlpha.append($0) }

  var characterAlpha: Array<String> = []
  lineSink(alphaInteriorNewlines, view: .character) { characterAlpha.append($0) }

  check(utf8Alpha == scalarAlpha)
  check(utf8Alpha == characterAlpha)

  var utf8Complex: Array<String> = []
  lineSink(longComplexNewlines, view: .utf8) { utf8Complex.append($0) }

  var scalarComplex: Array<String> = []
  lineSink(longComplexNewlines, view: .scalar) { scalarComplex.append($0) }

  var characterComplex: Array<String> = []
  lineSink(longComplexNewlines, view: .character) { characterComplex.append($0) }

  check(utf8Complex == scalarComplex)
  check(utf8Complex == characterComplex)

  print("preconditions checked")
}


private let alphaInteriorNewlines: String =
  """
  abc\(Unicode.Scalar(0x0A)!    // LF
  )def\(Unicode.Scalar(0x0B)!   // VT
  )ghi\(Unicode.Scalar(0x0C)!   // FF
  )jkl\(Unicode.Scalar(0x0D)!   // CR
  )mno\(Unicode.Scalar(0x0D)!)\(Unicode.Scalar(0x0A)!   // CR-LF
  )pqr\(Unicode.Scalar(0x2028)! // LS
  )stu\(Unicode.Scalar(0x2029)! // PS
  )vwx
  yz
  """