Skip to content

Commit

Permalink
[NSString] Fixed out-of-bounds exception (fixes #30).
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Mar 26, 2015
1 parent d5e2993 commit a2e4d4e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## Master

##### Breaking

None.

##### Enhancements

None.

##### Bug Fixes

* Fixed out-of-bounds exception when parsing the declaration in files starting
with a declaration.
[JP Simard](https://github.com/jpsim)
[#30](https://github.com/jpsim/SourceKitten/issues/30)

## 0.3.0

##### Breaking
Expand Down
5 changes: 3 additions & 2 deletions Source/SourceKittenFramework/NSString+SourceKitten.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ extension NSString {
:returns: Line breaks.
*/
public func lineBreaks() -> [Int] {
var lineBreaks = [Int]()
var lineBreaks = [-1] // Count start of file
var searchRange = NSRange(location: 0, length: length)
while searchRange.length > 0 {
while searchRange.length > 0 {
searchRange.length = length - searchRange.location
let foundRange = rangeOfString("\n", options: nil, range: searchRange)
if foundRange.location != NSNotFound {
Expand All @@ -69,6 +69,7 @@ extension NSString {
:returns: Filtered string.
*/
public func filteredSubstring(start: Int, end: Int) -> String {
precondition(end > start, "Range should be positive")
let range = NSRange(location: start, length: end - start - 1)
let unwantedSet = NSCharacterSet.whitespaceAndNewlineCharacterSet().mutableCopy() as NSMutableCharacterSet
unwantedSet.addCharactersInString("{")
Expand Down
2 changes: 1 addition & 1 deletion Source/SourceKittenFrameworkTests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class StringTests: XCTestCase {
}

func testParseLineBreaks() {
XCTAssertEqual("a\nbc\nd\n".lineBreaks(), [1, 4, 6], "should parse line breaks")
XCTAssertEqual("a\nbc\nd\n".lineBreaks(), [-1, 1, 4, 6], "should parse line breaks")
}

func testFilteredSubstring() {
Expand Down

0 comments on commit a2e4d4e

Please sign in to comment.