Skip to content

Commit b33fefb

Browse files
committed
[stdlib] String: be more consistent about when markEncoding is called
1 parent ed7d60c commit b33fefb

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ extension _StringGuts {
316316
internal var isUTF16: Bool { _object.isUTF16 }
317317

318318
@_alwaysEmitIntoClient // Swift 5.7
319+
@inline(__always)
319320
internal func markEncoding(_ i: String.Index) -> String.Index {
320321
isUTF8 ? i._knownUTF8 : i._knownUTF16
321322
}

stdlib/public/core/StringUnicodeScalarView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension String.UnicodeScalarView: BidirectionalCollection {
207207
i = _uncheckedIndex(before: i)
208208
}
209209
}
210-
return _guts.markEncoding(i)
210+
return i
211211
}
212212

213213
@_alwaysEmitIntoClient
@@ -238,7 +238,7 @@ extension String.UnicodeScalarView: BidirectionalCollection {
238238
}
239239
guard limit > start || i >= limit else { return nil }
240240
}
241-
return _guts.markEncoding(i)
241+
return i
242242
}
243243
}
244244

stdlib/public/core/Substring.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,14 @@ extension Substring: StringProtocol {
208208
// not fall on grapheme boundaries in `base`.
209209

210210
let i = _wholeGuts.validateCharacterIndex(i, in: _bounds)
211-
let r = _uncheckedIndex(after: i)
212-
return _wholeGuts.markEncoding(r)
211+
return _uncheckedIndex(after: i)
213212
}
214213

215214
/// A version of `index(after:)` that assumes that the given index:
216215
///
217216
/// - has the right encoding,
218217
/// - is within bounds, and
219218
/// - is character aligned within this substring.
220-
///
221-
/// It does not mark the encoding of the returned index.
222219
internal func _uncheckedIndex(after i: Index) -> Index {
223220
_internalInvariant(_wholeGuts.hasMatchingEncoding(i))
224221
_internalInvariant(i._isScalarAligned)
@@ -252,7 +249,7 @@ extension Substring: StringProtocol {
252249
r = r._characterAligned
253250
}
254251

255-
return r
252+
return _wholeGuts.markEncoding(r)
256253
}
257254

258255
public func index(before i: Index) -> Index {
@@ -269,17 +266,14 @@ extension Substring: StringProtocol {
269266
// alignment/validation work.
270267
_precondition(i > startIndex, "Substring index is out of bounds")
271268

272-
let r = _uncheckedIndex(before: i)
273-
return _wholeGuts.markEncoding(r)
269+
return _uncheckedIndex(before: i)
274270
}
275271

276272
/// A version of `index(before:)` that assumes that the given index:
277273
///
278274
/// - has the right encoding,
279275
/// - is within bounds, and
280276
/// - is character aligned within this substring.
281-
///
282-
/// It does not mark the encoding of the returned index.
283277
internal func _uncheckedIndex(before i: Index) -> Index {
284278
_internalInvariant(_wholeGuts.hasMatchingEncoding(i))
285279
_internalInvariant(i._isScalarAligned)
@@ -304,7 +298,7 @@ extension Substring: StringProtocol {
304298
r = r._characterAligned
305299
}
306300

307-
return r
301+
return _wholeGuts.markEncoding(r)
308302
}
309303

310304
public func index(_ i: Index, offsetBy distance: Int) -> Index {
@@ -328,7 +322,7 @@ extension Substring: StringProtocol {
328322
i = _uncheckedIndex(before: i)
329323
}
330324
}
331-
return _wholeGuts.markEncoding(i)
325+
return i
332326
}
333327

334328
public func index(
@@ -368,7 +362,7 @@ extension Substring: StringProtocol {
368362
}
369363
guard limit > start || i >= limit else { return nil }
370364
}
371-
return _wholeGuts.markEncoding(i)
365+
return i
372366
}
373367

374368
public func distance(from start: Index, to end: Index) -> Int {

0 commit comments

Comments
 (0)