@@ -109,18 +109,18 @@ Design Details
109
109
Output Streams
110
110
..............
111
111
112
- The most fundamental part of this design is ``OutputStream ``, a thing
112
+ The most fundamental part of this design is ``TextOutputStream ``, a thing
113
113
into which we can stream text: [#character1 ]_
114
114
115
115
::
116
116
117
- protocol OutputStream {
117
+ protocol TextOutputStream {
118
118
func append(_ text: String)
119
119
}
120
120
121
- Every ``String `` can be used as an ``OutputStream `` directly::
121
+ Every ``String `` can be used as an ``TextOutputStream `` directly::
122
122
123
- extension String : OutputStream {
123
+ extension String : TextOutputStream {
124
124
func append(_ text: String)
125
125
}
126
126
@@ -142,7 +142,7 @@ need to declare conformance: simply give the type a ``debugFormat()``::
142
142
143
143
Because ``String `` is a ``Streamable ``, your implementation of
144
144
``debugFormat `` can just return a ``String ``. If want to write
145
- directly to the ``OutputStream `` for efficiency reasons,
145
+ directly to the ``TextOutputStream `` for efficiency reasons,
146
146
(e.g. if your representation is huge), you can return a custom
147
147
``DebugRepresentation `` type.
148
148
@@ -197,11 +197,11 @@ implement::
197
197
Because it's not always efficient to construct a ``String ``
198
198
representation before writing an object to a stream, we provide a
199
199
``Streamable `` protocol, for types that can write themselves into an
200
- ``OutputStream ``. Every ``Streamable `` is also a ``CustomStringConvertible ``,
200
+ ``TextOutputStream ``. Every ``Streamable `` is also a ``CustomStringConvertible ``,
201
201
naturally::
202
202
203
203
protocol Streamable : CustomStringConvertible {
204
- func writeTo<T: OutputStream >(_ target: [inout] T)
204
+ func writeTo<T: TextOutputStream >(_ target: [inout] T)
205
205
206
206
// You'll never want to reimplement this
207
207
func format() -> PrintRepresentation {
@@ -224,7 +224,7 @@ adds surrounding quotes and escapes special characters::
224
224
struct EscapedStringRepresentation : Streamable {
225
225
var _value: String
226
226
227
- func writeTo<T: OutputStream >(_ target: [inout] T) {
227
+ func writeTo<T: TextOutputStream >(_ target: [inout] T) {
228
228
target.append("\"")
229
229
for c in _value {
230
230
target.append(c.escape())
@@ -233,11 +233,11 @@ adds surrounding quotes and escapes special characters::
233
233
}
234
234
}
235
235
236
- Besides modeling ``OutputStream ``, ``String `` also conforms to
236
+ Besides modeling ``TextOutputStream ``, ``String `` also conforms to
237
237
``Streamable ``::
238
238
239
239
extension String : Streamable {
240
- func writeTo<T: OutputStream >(_ target: [inout] T) {
240
+ func writeTo<T: TextOutputStream >(_ target: [inout] T) {
241
241
target.append(self) // Append yourself to the stream
242
242
}
243
243
@@ -275,12 +275,12 @@ complicated ``format(…)`` might be written::
275
275
struct RadixFormat<T: CustomStringConvertibleInteger> : Streamable {
276
276
var value: T, radix = 10, fill = " ", width = 0
277
277
278
- func writeTo<S: OutputStream >(_ target: [inout] S) {
278
+ func writeTo<S: TextOutputStream >(_ target: [inout] S) {
279
279
_writeSigned(value, &target)
280
280
}
281
281
282
282
// Write the given positive value to stream
283
- func _writePositive<T:CustomStringConvertibleInteger, S: OutputStream>(
283
+ func _writePositive<T:CustomStringConvertibleInteger, S: TextOutputStream>(
284
284
_ value: T, stream: [inout] S
285
285
) -> Int {
286
286
if value == 0 { return 0 }
@@ -293,7 +293,7 @@ complicated ``format(…)`` might be written::
293
293
return nDigits + 1
294
294
}
295
295
296
- func _writeSigned<T:CustomStringConvertibleInteger, S: OutputStream >(
296
+ func _writeSigned<T:CustomStringConvertibleInteger, S: TextOutputStream >(
297
297
_ value: T, target: [inout] S
298
298
) {
299
299
var width = 0
@@ -333,15 +333,15 @@ considerable thought, they are included here for completeness and to
333
333
ensure our proposed design doesn't rule out important directions of
334
334
evolution.
335
335
336
- ``OutputStream `` Adapters
336
+ ``TextOutputStream `` Adapters
337
337
.........................
338
338
339
339
Most text transformations can be expressed as adapters over generic
340
- ``OutputStream ``\ s. For example, it's easy to imagine an upcasing
340
+ ``TextOutputStream ``\ s. For example, it's easy to imagine an upcasing
341
341
adapter that transforms its input to upper case before writing it to
342
342
an underlying stream::
343
343
344
- struct UpperStream<UnderlyingStream:OutputStream > : OutputStream {
344
+ struct UpperStream<UnderlyingStream:TextOutputStream > : TextOutputStream {
345
345
func append(_ x: String) { base.append(x.toUpper()) }
346
346
var base: UnderlyingStream
347
347
}
@@ -353,26 +353,26 @@ processed and written to the underlying stream:
353
353
354
354
.. parsed-literal ::
355
355
356
- struct TrimStream<UnderlyingStream:OutputStream > : OutputStream {
356
+ struct TrimStream<UnderlyingStream:TextOutputStream > : TextOutputStream {
357
357
func append(_ x: String) { ... }
358
358
**func close() { ... } **
359
359
var base: UnderlyingStream
360
360
var bufferedWhitespace: String
361
361
}
362
362
363
- This makes general ``OutputStream `` adapters more complicated to write
364
- and use than ordinary ``OutputStream ``\ s.
363
+ This makes general ``TextOutputStream `` adapters more complicated to write
364
+ and use than ordinary ``TextOutputStream ``\ s.
365
365
366
366
``Streamable `` Adapters
367
367
.......................
368
368
369
- For every conceivable ``OutputStream `` adaptor there's a corresponding
369
+ For every conceivable ``TextOutputStream `` adaptor there's a corresponding
370
370
``Streamable `` adaptor. For example::
371
371
372
372
struct UpperStreamable<UnderlyingStreamable:Streamable> {
373
373
var base: UnderlyingStreamable
374
374
375
- func writeTo<T: OutputStream >(_ target: [inout] T) {
375
+ func writeTo<T: TextOutputStream >(_ target: [inout] T) {
376
376
var adaptedStream = UpperStream(target)
377
377
self.base.writeTo(&adaptedStream)
378
378
target = adaptedStream.base
@@ -418,20 +418,20 @@ least until we do, we opt not to trade away any CPU, memory, and
418
418
power.
419
419
420
420
If we were willing to say that only ``class ``\ es can conform to
421
- ``OutputStream ``, we could eliminate the explicit ``[inout] `` where
422
- ``OutputStream ``\ s are passed around. Then, we'd simply need a
421
+ ``TextOutputStream ``, we could eliminate the explicit ``[inout] `` where
422
+ ``TextOutputStream ``\ s are passed around. Then, we'd simply need a
423
423
``class StringStream `` for creating ``String `` representations. It
424
- would also make ``OutputStream `` adapters a *bit * simpler to use
424
+ would also make ``TextOutputStream `` adapters a *bit * simpler to use
425
425
because you'd never need to "write back" explicitly onto the target
426
- stream. However, stateful ``OutputStream `` adapters would still need a
426
+ stream. However, stateful ``TextOutputStream `` adapters would still need a
427
427
``close() `` method, which makes a perfect place to return a copy of
428
428
the underlying stream, which can then be "written back":
429
429
430
430
.. parsed-literal ::
431
431
432
432
struct AdaptedStreamable<T:Streamable> {
433
433
...
434
- func writeTo<Target: OutputStream >(_ target: [inout] Target) {
434
+ func writeTo<Target: TextOutputStream >(_ target: [inout] Target) {
435
435
// create the stream that transforms the representation
436
436
var adaptedTarget = adapt(target, adapter);
437
437
// write the Base object to the target stream
@@ -444,7 +444,7 @@ the underlying stream, which can then be "written back":
444
444
}
445
445
446
446
We think anyone writing such adapters can handle the need for explicit
447
- write-back, and the ability to use ``String `` as an ``OutputStream ``
447
+ write-back, and the ability to use ``String `` as an ``TextOutputStream ``
448
448
without additionally allocating a ``StringStream `` on the heap seems
449
449
to tip the balance in favor of the current design.
450
450
0 commit comments