@@ -323,85 +323,45 @@ extension StringProtocol {
323323 }
324324}
325325
326- // https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
327- extension String {
328- public subscript( i : Int ) -> Character {
329- return self [ index ( startIndex, offsetBy: i ) ]
326+ // https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language/38215613#38215613
327+ extension StringProtocol {
328+ public subscript( _ offset : Int ) -> Element {
329+ self [ index ( startIndex, offsetBy: offset ) ]
330330 }
331-
332- public subscript( i : Int ) -> String {
333- return String ( self [ i ] as Character )
331+
332+ public subscript( _ range : Range < Int > ) -> SubSequence {
333+ prefix ( range . lowerBound + range . count ) . suffix ( range . count )
334334 }
335-
336- public subscript( r: Range < Int > ) -> String {
337- let start = index ( startIndex, offsetBy: r. lowerBound)
338- let end = index ( startIndex, offsetBy: r. upperBound)
339- return String ( self [ start ..< end] )
335+
336+ public subscript( _ range: ClosedRange < Int > ) -> SubSequence {
337+ prefix ( range. lowerBound+ range. count) . suffix ( range. count)
340338 }
341-
342- public subscript( bounds: CountableRange < Int > ) -> Substring {
343- let start = index ( startIndex, offsetBy: bounds. lowerBound)
344- let end = index ( startIndex, offsetBy: bounds. upperBound)
345- return self [ start ..< end]
339+
340+ public subscript( _ range: PartialRangeThrough < Int > ) -> SubSequence {
341+ prefix ( range. upperBound. advanced ( by: 1 ) )
346342 }
347-
348- public subscript( bounds: CountableClosedRange < Int > ) -> Substring {
349- let start = index ( startIndex, offsetBy: bounds. lowerBound)
350- let end = index ( startIndex, offsetBy: bounds. upperBound)
351- return self [ start ... end]
343+
344+ public subscript( _ range: PartialRangeUpTo < Int > ) -> SubSequence {
345+ prefix ( range. upperBound)
352346 }
353-
354- public subscript( bounds: CountablePartialRangeFrom < Int > ) -> Substring {
355- let start = index ( startIndex, offsetBy: bounds. lowerBound)
356- let end = index ( endIndex, offsetBy: - 1 )
357- return self [ start ... end]
358- }
359-
360- public subscript( bounds: PartialRangeThrough < Int > ) -> Substring {
361- let end = index ( startIndex, offsetBy: bounds. upperBound)
362- return self [ startIndex ... end]
363- }
364-
365- public subscript( bounds: PartialRangeUpTo < Int > ) -> Substring {
366- let end = index ( startIndex, offsetBy: bounds. upperBound)
367- return self [ startIndex ..< end]
347+
348+ public subscript( _ range: PartialRangeFrom < Int > ) -> SubSequence {
349+ suffix ( Swift . max ( 0 , count- range. lowerBound) )
368350 }
369351}
370352
371- extension Substring {
372- public subscript( i: Int ) -> Character {
373- return self [ index ( startIndex, offsetBy: i) ]
374- }
375-
376- public subscript( bounds: CountableRange < Int > ) -> Substring {
377- let start = index ( startIndex, offsetBy: bounds. lowerBound)
378- let end = index ( startIndex, offsetBy: bounds. upperBound)
379- return self [ start ..< end]
380- }
381-
382- public subscript( bounds: CountableClosedRange < Int > ) -> Substring {
383- let start = index ( startIndex, offsetBy: bounds. lowerBound)
384- let end = index ( startIndex, offsetBy: bounds. upperBound)
385- return self [ start ... end]
386- }
387-
388- public subscript( bounds: CountablePartialRangeFrom < Int > ) -> Substring {
389- let start = index ( startIndex, offsetBy: bounds. lowerBound)
390- let end = index ( endIndex, offsetBy: - 1 )
391- return self [ start ... end]
392- }
393-
394- public subscript( bounds: PartialRangeThrough < Int > ) -> Substring {
395- let end = index ( startIndex, offsetBy: bounds. upperBound)
396- return self [ startIndex ... end]
397- }
353+ extension LosslessStringConvertible {
354+ public var string : String { . init( self ) }
355+ }
398356
399- public subscript( bounds: PartialRangeUpTo < Int > ) -> Substring {
400- let end = index ( startIndex, offsetBy: bounds. upperBound)
401- return self [ startIndex ..< end]
357+ extension BidirectionalCollection {
358+ public subscript( safe offset: Int ) -> Element ? {
359+ guard !isEmpty, let i = index ( startIndex, offsetBy: offset, limitedBy: index ( before: endIndex) ) else { return nil }
360+ return self [ i]
402361 }
403362}
404363
364+
405365extension URL {
406366 @discardableResult
407367 public func trimmingQuery( ) -> String ? {
0 commit comments