Skip to content

Commit

Permalink
Fixed check for location validity in NSAttributedString extensions (#299
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rajdeep authored Mar 20, 2024
1 parent d2f015c commit 7910aa2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Proton/Sources/Swift/Helpers/NSAttributedString+Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public extension NSAttributedString {
/// specified in range.
func reverseAttributedSubstring(from range: NSRange) -> NSAttributedString? {
guard length > 0,
range.location >= 0,
range.location + range.length < length
else { return nil }

Expand All @@ -70,7 +71,8 @@ public extension NSAttributedString {
/// - location: Starting location
/// - reverseLookup: When true, look up is carried out in reverse direction. Default is false.
func rangeOf(attribute: NSAttributedString.Key, startingLocation location: Int, reverseLookup: Bool = false) -> NSRange? {
guard location < length else { return nil }
guard location >= 0,
location < length else { return nil }

let range = reverseLookup ? NSRange(location: 0, length: location) : NSRange(location: location, length: length - location)
let options = reverseLookup ? EnumerationOptions.reverse : []
Expand All @@ -92,7 +94,8 @@ public extension NSAttributedString {
/// - attribute: Attribute to search
/// - location: Location to inspect
func rangeOf(attribute: NSAttributedString.Key, at location: Int) -> NSRange? {
guard location < length,
guard location >= 0,
location < length,
self.attribute(attribute, at: location, effectiveRange: nil) != nil
else { return nil }

Expand Down Expand Up @@ -127,14 +130,16 @@ public extension NSAttributedString {
/// - attributeKey: Name of the attribute
/// - location: Location to check
func attributeValue<T>(for attributeKey: NSAttributedString.Key, at location: Int) -> T? {
guard location < length else { return nil }
guard location >= 0,
location < length else { return nil }
return attribute(attributeKey, at: location, effectiveRange: nil) as? T
}

/// Alternative to `attributedSubstring(from:_).string`
/// Avoids allocating `NSAttributedString` and all the attributes for that range, only to ignore the range.
func substring(from range: NSRange) -> String {
guard range.upperBound <= length else {
guard range.location >= 0,
range.upperBound <= length else {
assertionFailure("Substring is out of bounds")
return ""
}
Expand All @@ -148,7 +153,8 @@ public extension NSAttributedString {
/// - isCaseInsensitive: Case insensitive search. Defaults to `true`
/// - Returns: Range of search text, if found.
func reverseRange(of searchText: String, startingLocation: Int, isCaseInsensitive: Bool = true) -> NSRange? {
guard startingLocation <= string.utf16.count else {
guard startingLocation >= 0,
startingLocation <= string.utf16.count else {
return nil
}

Expand Down
24 changes: 24 additions & 0 deletions Proton/Tests/ExtensionTests/NSAttributedStringExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ class NSAttributedStringExtensionTests: XCTestCase {
XCTAssertNil(substring)
}

func testReturnsNilForInvalidNegativeReverseRange() {
let text = NSAttributedString(string: "test")
let substring = text.reverseAttributedSubstring(from: NSRange(location: -2, length: 2))
XCTAssertNil(substring)
}

func testReturnsReverseRange() {
let text = NSAttributedString(string: "test")
let range = text.reverseRange(of: "tE", startingLocation: 3)
XCTAssertEqual(range, NSRange(location: 0, length: 2))
}

func testReturnsNilReverseRangeOnOutOfBoundsLocation() {
let text = NSAttributedString(string: "test")
let range = text.reverseRange(of: "tE", startingLocation: 10)
XCTAssertNil(range)
}

func testReturnsNilReverseRangeOnNegativeLocation() {
let text = NSAttributedString(string: "test")
let range = text.reverseRange(of: "tE", startingLocation: -1)
XCTAssertNil(range)
}

func testReturnsAttributedStringForReverseRange() {
let text = NSAttributedString(string: "This is a test string")
let substring = text.reverseAttributedSubstring(from: NSRange(location: 9, length: 4))
Expand Down

0 comments on commit 7910aa2

Please sign in to comment.