String & String views: Add bounds checking to range subscripts.

This commit is contained in:
Karoy Lorentey
2018-01-19 03:11:13 +00:00
committed by Michael Ilseman
parent 6d1866f846
commit b8d8949166
4 changed files with 58 additions and 0 deletions

View File

@@ -89,6 +89,34 @@ extension String : StringProtocol, RangeReplaceableCollection {
@_inlineable // FIXME(sil-serialize-all)
public var endIndex: Index { return Index(encodedOffset: _guts.count) }
@_inlineable
@_versioned
@inline(__always)
internal func _boundsCheck(_ index: Index) {
_precondition(index.encodedOffset >= 0 && index.encodedOffset < _guts.count,
"String index is out of bounds")
}
@_inlineable
@_versioned
@inline(__always)
internal func _boundsCheck(_ range: Range<Index>) {
_precondition(
range.lowerBound.encodedOffset >= 0 &&
range.upperBound.encodedOffset <= _guts.count,
"String index range is out of bounds")
}
@_inlineable
@_versioned
@inline(__always)
internal func _boundsCheck(_ range: ClosedRange<Index>) {
_precondition(
range.lowerBound.encodedOffset >= 0 &&
range.upperBound.encodedOffset < _guts.count,
"String index range is out of bounds")
}
@_inlineable // FIXME(sil-serialize-all)
@_versioned // FIXME(sil-serialize-all)
internal func _index(atEncodedOffset offset: Int) -> Index {