Skip to content

Commit b52f807

Browse files
committed
Give DefaultStringInterpolation its own append paths
This should allow us to separately control how appends are inlined for string interpolation.
1 parent ed44e2f commit b52f807

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ extension _StringGuts {
952952
_invariantCheck()
953953
}
954954

955+
@usableFromInline
955956
internal
956957
mutating func append(_ other: _UnmanagedASCIIString) {
957958
guard other.count > 0 else { return }

stdlib/public/core/StringInterpolation.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
108108
public mutating func appendInterpolation<T>(_ value: T)
109109
where T: TextOutputStreamable, T: CustomStringConvertible
110110
{
111-
value.write(to: &_storage)
111+
value.write(to: &self)
112112
}
113113

114114
/// Interpolates the given value's textual representation into the
@@ -130,7 +130,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
130130
public mutating func appendInterpolation<T>(_ value: T)
131131
where T: TextOutputStreamable
132132
{
133-
value.write(to: &_storage)
133+
value.write(to: &self)
134134
}
135135

136136
/// Interpolates the given value's textual representation into the
@@ -154,7 +154,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
154154
public mutating func appendInterpolation<T>(_ value: T)
155155
where T: CustomStringConvertible
156156
{
157-
_storage += value.description
157+
value.description.write(to: &self)
158158
}
159159

160160
/// Interpolates the given value's textual representation into the
@@ -176,7 +176,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
176176
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
177177
@inlinable
178178
public mutating func appendInterpolation<T>(_ value: T) {
179-
_print_unlocked(value, &_storage)
179+
_print_unlocked(value, &self)
180180
}
181181

182182
/// Creates a string from this instance, consuming the instance in the
@@ -194,6 +194,20 @@ extension DefaultStringInterpolation: CustomStringConvertible {
194194
}
195195
}
196196

197+
extension DefaultStringInterpolation: TextOutputStream {
198+
@inlinable
199+
public mutating func write(_ string: String) {
200+
// Most interpolations will not append to an empty string, so we bypass the
201+
// empty-singleton check.
202+
_storage._guts._appendSlow(string._guts)
203+
}
204+
205+
@inlinable
206+
public mutating func _writeASCII(_ buffer: UnsafeBufferPointer<UInt8>) {
207+
_storage._guts.append(_UnmanagedString(buffer))
208+
}
209+
}
210+
197211
// While not strictly necessary, declaring these is faster than using the
198212
// default implementation.
199213
extension String {

0 commit comments

Comments
 (0)