From 26e5c8a84bd8be243f9208b0777f36109f153218 Mon Sep 17 00:00:00 2001 From: Stephan Partzsch Date: Thu, 7 Mar 2024 11:35:56 +0100 Subject: [PATCH] Fix complex pattern hanging forever (https://github.com/SimonFairbairn/SwiftyMarkdown/pull/139). Done by https://github.com/polqf --- Sources/SwiftyMarkdown/SwiftyScanner.swift | 12 +++- .../SwiftyMarkdownAttributedStringTests.swift | 55 ++++++++++--------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Sources/SwiftyMarkdown/SwiftyScanner.swift b/Sources/SwiftyMarkdown/SwiftyScanner.swift index 1834e6a..c2a0086 100644 --- a/Sources/SwiftyMarkdown/SwiftyScanner.swift +++ b/Sources/SwiftyMarkdown/SwiftyScanner.swift @@ -259,7 +259,8 @@ 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)}) { @@ -267,12 +268,19 @@ class SwiftyScanner { } } } - 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) { diff --git a/Tests/SwiftyMarkdownTests/SwiftyMarkdownAttributedStringTests.swift b/Tests/SwiftyMarkdownTests/SwiftyMarkdownAttributedStringTests.swift index 600bf9b..607e471 100644 --- a/Tests/SwiftyMarkdownTests/SwiftyMarkdownAttributedStringTests.swift +++ b/Tests/SwiftyMarkdownTests/SwiftyMarkdownAttributedStringTests.swift @@ -6,32 +6,37 @@ // Copyright © 2019 Voyage Travel Apps. All rights reserved. // -import XCTest @testable import SwiftyMarkdown +import XCTest class SwiftyMarkdownAttributedStringTests: XCTestCase { - - func testThatAttributesAreAppliedCorrectly() { - - let string = """ -# Heading 1 - -A more *complicated* example. This one has **it all**. Here is a [link](http://voyagetravelapps.com/). - -## Heading 2 - -## Heading 3 - -> This one is a blockquote -""" - let md = SwiftyMarkdown(string: string) - let attributedString = md.attributedString() - - 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 testThatAttributesAreAppliedCorrectly() { + let string = """ + # Heading 1 + + A more *complicated* example. This one has **it all**. Here is a [link](http://voyagetravelapps.com/). + + ## Heading 2 + + ## Heading 3 + + > This one is a blockquote + """ + + let md = SwiftyMarkdown(string: string) + let attributedString = md.attributedString() + + 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 ***~~~**~") + } }