-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathTextFormatting.swift
280 lines (238 loc) · 8.12 KB
/
TextFormatting.swift
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
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// Text Formatting Prototype
//
// This file demonstrates the concepts proposed in TextFormatting.rst
//
// We may not want the following protocol exactly as-is; it overlaps somewhat
// with CustomStringConvertible and CustomDebugStringConvertible, and it's not
// clear whether a protocol is even needed.
/// A type that supports format() and debugFormat().
protocol CustomFormatted : CustomStringConvertible, CustomDebugStringConvertible {
associatedtype DebugRepresentation : TextOutputStreamable = String
associatedtype PrintRepresentation : TextOutputStreamable = DebugRepresentation
/// Produce a textual representation for the REPL and
/// Debugger.
///
/// Because String is a TextOutputStreamable, your implementation of
/// debugRepresentation can just return a String. If you want to write
/// directly to the TextOutputStream for efficiency reasons,
/// (e.g. if your representation is huge), you can return a custom
/// DebugRepresentation type.
///
/// NOTE: producing a representation that can be consumed by the
/// REPL to produce an equivalent object is strongly encouraged
/// where possible! For example, String.debugFormat() produces a
/// representation containing quotes, where special characters are
/// escaped, etc. A struct Point { var x, y: Int } might be
/// represented as "Point(x: 3, y: 5)".
func debugFormat() -> DebugRepresentation
/// produce a "pretty" textual representation that can be
/// distinct from the debug format. For example,
/// String.printRepresentation returns the string itself, without quoting.
///
/// In general you can return a String here, but if you need more
/// control, we strongly recommend returning a custom Representation
/// type, e.g. a nested struct of your type. If you're lazy, you
/// can conform to TextOutputStreamable directly and just implement its
/// write() func.
func format() -> PrintRepresentation
}
extension CustomFormatted where PrintRepresentation == DebugRepresentation {
func format() -> DebugRepresentation { return debugFormat() }
}
/// CustomFormatted's conformance to CustomStringConvertible and
/// CustomDebugStringConvertible
extension CustomFormatted {
public var description: String {
return _description
}
public var debugDescription: String {
return _debugDescription
}
//===--- Using [debug]description directly might pick up the stdlib -----===//
//===--- versions and skew test results, so use these for testing -------===//
var _description: String {
var result = ""
x.format().write(to: &result)
return result
}
var _debugDescription: String {
var result = ""
x.debugFormat().write(to: &result)
return result
}
}
extension String : CustomFormatted {
// Streamer for the debug representation of String. When an
// EscapedStringFormat is written to a TextOutputStream, it adds
// surrounding quotes and escapes special characters.
struct EscapedFormat : TextOutputStreamable {
init(_ s: String) {
self._value = s
}
func write<Target: TextOutputStream>(to target: inout Target) {
target.write("\"")
for c in _value.unicodeScalars {
target.write(c.escaped(asASCII: true))
}
target.write("\"")
}
var _value: String
}
func debugFormat() -> EscapedFormat {
return EscapedFormat(self)
}
func format() -> String {
return self
}
}
//===--- Updated Integer APIs ---------------------------------------------===//
// These will be redundant once the new integer protocols are merged to trunk
// but in the meantime they give us a decent protocol and explicit conversions
// among integers. You can skip this section if only interested in formatting
//===----------------------------------------------------------------------===//
public typealias Integer = Swift.FixedWidthInteger
extension Integer {
/// Creates an instance with the same value as `i`
///
/// - Precondition: `i`'s value is representable by `Self`.
init <N: Integer>(_ i: N) {
// Implementation based on Egyptian Multiplication of i by 1 allows us to
// avoid any mixed-type numeric operations, which the current standard
// library doesn't support. Note: we also don't have a protocol that
// provides shift operators, so we use / 2 here.
var n = i
self = 0
if n == 0 { return }
if n < 0 {
self -= 1 as Self
}
else {
self = 1
}
// compute:
// abs(self) = the greatest power of 2 that divides i.
// n = i / self
while (n & 1) == 0 {
self += self
n = n / 2
}
n /= 2 // consume the lowest 1-bit in i
if n == 0 { return }
// Accumulate the rest of the 1 bits in i.
// invariants:
// abs(a) is a power of 2
// n = i / a
var a = self + self
while true {
if n & 1 != 0 {
self += a
if n / 2 == 0 { return }
}
n /= 2
a += a
}
}
}
//===--- End Updated Integer APIs -----------------------------------------===//
/// The way all Integer types are formatted.
extension CustomFormatted where Self : Integer {
func debugFormat() -> IntegerFormat<Self> {
return format(radix: 10)
}
func format(
radix: Int = 10, fill: String = " ", width: Int = 0
) -> IntegerFormat<Self> {
return IntegerFormat(value: self, radix: radix, fill: fill, width: width)
}
}
/// A textual representation for integers
public struct IntegerFormat<T: Integer> : TextOutputStreamable {
public init(value: T, radix: Int, fill: String, width: Int) {
self.value = value
self.radix = T(radix)
self.fill = fill
self.width = width
}
private var value: T
private var radix: T, fill: String, width: Int
private func _writePositive<S: TextOutputStream>(
_ value: T, _ stream: inout S
) -> Int {
if value == 0 {
return 0
}
let rest: T = value / radix
let nDigits = _writePositive(rest, &stream)
let digit = UInt32(value % radix)
let baseCharOrd : UInt32 = digit <= 9
? UnicodeScalar("0").value
: UnicodeScalar("A").value - 10
stream.write(String(UnicodeScalar(baseCharOrd + digit)!))
return nDigits + 1
}
public func write<Target: TextOutputStream>(to target: inout Target) {
var width = 0
var result = ""
if value == 0 {
result = "0"
width += 1
}
else {
let absVal = value < 0 ? 0 - value : value
if (value < 0) {
target.write("-")
width += 1
}
width += _writePositive(absVal, &result)
}
while width < self.width {
width += 1
target.write(self.fill)
}
target.write(result)
}
}
extension Int : CustomFormatted {}
extension UInt : CustomFormatted {}
//===--- Adapter that encloses text in vertical bars, for testing ---------===//
/// A textual representation that encloses some base `TextOutputStreamable` in
/// vertical bars, making padding spaces more apparent.
///
/// - Note: This is just for the benefit of the tests
struct Delimited<T: TextOutputStreamable> : TextOutputStreamable {
var base: T
func write<S: TextOutputStream>(to output: inout S) {
"|".write(to: &output)
base.write(to: &output)
"|".write(to: &output)
}
}
extension TextOutputStreamable {
/// A textual representation that encloses the plain textual representation of
/// `self` in vertical bars, making padding spaces more apparent.
///
/// - Note: This is just for the benefit of the tests
var delimited : Delimited<Self> {
return Delimited(base: self)
}
}
//===--- Tests ------------------------------------------------------------===//
var x = "fubar\n\tbaz"
print(x._description)
// CHECK: fubar
// CHECK-NEXT: baz
print(x._debugDescription)
// CHECK-NEXT: "fubar\n\tbaz"
print(424242.format(radix:16, width:8).delimited)
// CHECK-NEXT: | 67932|
var zero = "0"
print((-434343).format(fill:zero, width:8).delimited)
// CHECK-NEXT: |-0434343|
print((-42).format(radix:13, width:8).delimited)
// CHECK-NEXT: |- 33|
print(0x1EADBEEF.format(radix:16).delimited)
// CHECK-NEXT: 1EADBEEF
print((0xDEADBEEF as UInt).format(radix:16).delimited)
// CHECK-NEXT: DEADBEEF