Skip to content

Commit 704959a

Browse files
committed
修复 swift5 中字符串下标的实现
1 parent 04924ab commit 704959a

File tree

2 files changed

+45
-67
lines changed

2 files changed

+45
-67
lines changed

CRBoostSwift/Classes/Foundation+CRBoost.swift

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
405365
extension URL {
406366
@discardableResult
407367
public func trimmingQuery() -> String? {

Example/CRBoostSwift/ViewController.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ class ViewController: UIViewController {
2424
// print(".11".retainDecimal(5))
2525
// print(".".retainDecimal(5))
2626
print("".retainDecimal(5))
27+
28+
29+
let test = "Hello USA 🇺🇸!!! Hello Brazil 🇧🇷!!!"
30+
print(test[safe: 10] as Any) // "🇺🇸"
31+
print(test[11]) // "!"
32+
print(test[10...]) // "🇺🇸!!! Hello Brazil 🇧🇷!!!"
33+
print(test[10..<12]) // "🇺🇸!"
34+
print(test[10...12]) // "🇺🇸!!"
35+
print(test[...10]) // "Hello USA 🇺🇸"
36+
print(test[..<10]) // "Hello USA "
37+
print(test.first as Any) // "H"
38+
print(test.last as Any) // "!"
2739

40+
// Subscripting the Substring
41+
print(test[...][...3]) // "Hell"
42+
43+
// Note that they all return a Substring of the original String.
44+
// To create a new String from a substring
45+
print(test[10...].string) // "🇺🇸!!! Hello Brazil 🇧🇷!!!"
2846
}
2947
}

0 commit comments

Comments
 (0)