Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix complex pattern hanging forever #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Sources/SwiftyMarkdown/SwiftyScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,27 @@ class SwiftyScanner {
} else {
var remainingTags = min(openRange.upperBound - openRange.lowerBound, closeRange.upperBound - closeRange.lowerBound) + 1
while remainingTags > 0 {
if remainingTags >= self.rule.maxTags {
let shouldAppendStyle = remainingTags >= self.rule.maxTags
if shouldAppendStyle {
remainingTags -= self.rule.maxTags
if let style = self.rule.styles[ self.rule.maxTags ] {
if !styles.contains(where: { $0.isEqualTo(style)}) {
styles.append(style)
}
}
}
if let style = self.rule.styles[remainingTags] {

let remainingTagsStyle = self.rule.styles[remainingTags]
if let style = remainingTagsStyle {
remainingTags -= remainingTags
if !styles.contains(where: { $0.isEqualTo(style)}) {
styles.append(style)
}
}

if !shouldAppendStyle && remainingTagsStyle == nil {
break
}
}

for idx in (openRange.upperBound)...(closeRange.lowerBound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ A more *complicated* example. This one has **it all**. Here is a [link](http://v
XCTAssertNotNil(attributedString)

XCTAssertEqual(attributedString.string, "Heading 1\n\nA more complicated example. This one has it all. Here is a link.\n\nHeading 2\n\nHeading 3\n\nThis one is a blockquote")


}


func testComplexPatternDoesNotHangForever() {
let string = "**~*~~~*~*~**~*~* h e a r d ***~*~*~**~*~~~*"

let md = SwiftyMarkdown(string: string)
let attributedString = md.attributedString()

XCTAssertNotNil(attributedString)
XCTAssertEqual(attributedString.string, "**~~*~**~~ h e a r d ***~~~**~")
}
}