Skip to content

Commit 0ece62d

Browse files
committed
[String] Add Substring.base
Adds Substring.base, analogous to Slice.base, to access the entire String. Tests added.
1 parent aa51936 commit 0ece62d

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

stdlib/public/core/LegacyABI.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ internal func _isContinuation(_ x: UInt8) -> Bool {
3333
return UTF8.isContinuation(x)
3434
}
3535

36+
extension Substring {
37+
@available(*, unavailable, renamed: "Substring.base")
38+
@inlinable
39+
internal var _wholeString: String { return base }
40+
}
41+

stdlib/public/core/StringCreate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ extension String {
193193
internal static func _fromSubstring(
194194
_ substring: __shared Substring
195195
) -> String {
196-
if substring._offsetRange == substring._wholeString._offsetRange {
197-
return substring._wholeString
196+
if substring._offsetRange == substring.base._offsetRange {
197+
return substring.base
198198
}
199199

200200
return String._copying(substring)

stdlib/public/core/Substring.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ public struct Substring {
116116
}
117117

118118
extension Substring {
119-
@inlinable
120-
internal var _wholeGuts: _StringGuts {
121-
@inline(__always) get { return _slice.base._guts }
122-
}
123-
@inlinable
124-
internal var _wholeString: String {
125-
@inline(__always) get { return String(self._wholeGuts) }
126-
}
119+
/// Returns the underlying string from which this Substring was derived.
120+
@_alwaysEmitIntoClient
121+
public var base: String { return _slice.base }
122+
123+
@inlinable @inline(__always)
124+
internal var _wholeGuts: _StringGuts { return base._guts }
127125

128126
@inlinable
129127
internal var _offsetRange: Range<Int> {
@@ -141,7 +139,7 @@ extension Substring {
141139
#else
142140
@usableFromInline @inline(never) @_effects(releasenone)
143141
internal func _invariantCheck() {
144-
self._wholeString._invariantCheck()
142+
self.base._invariantCheck()
145143
}
146144
#endif // INTERNAL_CHECKS_ENABLED
147145
}
@@ -420,7 +418,7 @@ extension Substring {
420418
@inlinable
421419
public var utf8: UTF8View {
422420
get {
423-
return _wholeString.utf8[startIndex..<endIndex]
421+
return base.utf8[startIndex..<endIndex]
424422
}
425423
set {
426424
self = Substring(newValue)
@@ -546,7 +544,7 @@ extension Substring {
546544
@inlinable
547545
public var utf16: UTF16View {
548546
get {
549-
return _wholeString.utf16[startIndex..<endIndex]
547+
return base.utf16[startIndex..<endIndex]
550548
}
551549
set {
552550
self = Substring(newValue)
@@ -672,7 +670,7 @@ extension Substring {
672670
@inlinable
673671
public var unicodeScalars: UnicodeScalarView {
674672
get {
675-
return _wholeString.unicodeScalars[startIndex..<endIndex]
673+
return base.unicodeScalars[startIndex..<endIndex]
676674
}
677675
set {
678676
self = Substring(newValue)

test/stdlib/subString.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,13 @@ SubstringTests.test("Persistent Content") {
238238
expectEqual("bcdefg", (str.dropFirst(1) + "g") as String)
239239
}
240240

241+
SubstringTests.test("Substring.base") {
242+
let str = "abéÏ01😓🎃👨‍👨‍👧‍👦"
243+
expectEqual(str, str.dropLast().base)
244+
for idx in str.indices {
245+
expectEqual(str, str[idx...].base)
246+
expectEqual(str, str[...idx].base)
247+
}
248+
}
249+
241250
runAllTests()

validation-test/stdlib/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension String {
5959

6060
extension Substring {
6161
var bufferID: ObjectIdentifier? {
62-
return _wholeString.bufferID
62+
return base.bufferID
6363
}
6464
}
6565

0 commit comments

Comments
 (0)