From b637c4c4a4af5e9ddc2e81e3b31f3953a42c817d Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 18 Mar 2021 12:58:48 +0000 Subject: [PATCH] Tidy standalone processing --- Sources/HummingbirdMustache/Parser.swift | 4 ++ .../HummingbirdMustache/Template+Parser.swift | 39 ++++++------------- .../HummingbirdMustache/Template+Render.swift | 2 +- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Sources/HummingbirdMustache/Parser.swift b/Sources/HummingbirdMustache/Parser.swift index 33e2509..c6cb23b 100644 --- a/Sources/HummingbirdMustache/Parser.swift +++ b/Sources/HummingbirdMustache/Parser.swift @@ -406,6 +406,10 @@ extension HBParser { } mutating func setPosition(_ index: Int) throws { + if index == self.range.endIndex { + _setPosition(index) + return + } guard range.contains(index) else { throw Error.invalidPosition } guard validateUTF8Character(at: index).0 != nil else { throw Error.invalidPosition } _setPosition(index) diff --git a/Sources/HummingbirdMustache/Template+Parser.swift b/Sources/HummingbirdMustache/Template+Parser.swift index 210f025..e7373ed 100644 --- a/Sources/HummingbirdMustache/Template+Parser.swift +++ b/Sources/HummingbirdMustache/Template+Parser.swift @@ -82,11 +82,8 @@ extension HBMustacheTemplate { // section parser.unsafeAdvance() let (name, method) = try parseName(&parser, state: state) - if state.newLine, hasLineFinished(&parser) { + if isStandalone(&parser, state: state) { setNewLine = true - if parser.current() == "\n" { - parser.unsafeAdvance() - } } else if whiteSpaceBefore.count > 0 { tokens.append(.text(whiteSpaceBefore)) whiteSpaceBefore = "" @@ -98,11 +95,8 @@ extension HBMustacheTemplate { // inverted section parser.unsafeAdvance() let (name, method) = try parseName(&parser, state: state) - if state.newLine, hasLineFinished(&parser) { + if isStandalone(&parser, state: state) { setNewLine = true - if parser.current() == "\n" { - parser.unsafeAdvance() - } } else if whiteSpaceBefore.count > 0 { tokens.append(.text(whiteSpaceBefore)) whiteSpaceBefore = "" @@ -117,11 +111,8 @@ extension HBMustacheTemplate { guard name == state.sectionName else { throw Error.sectionCloseNameIncorrect } - if state.newLine, hasLineFinished(&parser) { + if isStandalone(&parser, state: state) { setNewLine = true - if parser.current() == "\n" { - parser.unsafeAdvance() - } } else if whiteSpaceBefore.count > 0 { tokens.append(.text(whiteSpaceBefore)) whiteSpaceBefore = "" @@ -132,12 +123,7 @@ extension HBMustacheTemplate { // comment parser.unsafeAdvance() _ = try parseComment(&parser, state: state) - if state.newLine, hasLineFinished(&parser) { - setNewLine = true - if !parser.reachedEnd() { - parser.unsafeAdvance() - } - } + setNewLine = isStandalone(&parser, state: state) case "{": // unescaped variable @@ -167,11 +153,8 @@ extension HBMustacheTemplate { if whiteSpaceBefore.count > 0 { tokens.append(.text(whiteSpaceBefore)) } - if state.newLine, hasLineFinished(&parser) { + if isStandalone(&parser, state: state) { setNewLine = true - if parser.current() == "\n" { - parser.unsafeAdvance() - } tokens.append(.partial(name, indentation: whiteSpaceBefore)) } else { tokens.append(.partial(name, indentation: nil)) @@ -182,12 +165,7 @@ extension HBMustacheTemplate { // set delimiter parser.unsafeAdvance() state = try parserSetDelimiter(&parser, state: state) - if state.newLine, hasLineFinished(&parser) { - setNewLine = true - if !parser.reachedEnd() { - parser.unsafeAdvance() - } - } + setNewLine = isStandalone(&parser, state: state) default: // variable @@ -278,12 +256,17 @@ extension HBMustacheTemplate { if parser.reachedEnd() { return true } parser2.read(while: Set(" \t\r")) if parser2.current() == "\n" { + parser2.unsafeAdvance() try! parser.setPosition(parser2.getPosition()) return true } return false } + static func isStandalone(_ parser: inout HBParser, state: ParserState) -> Bool { + return state.newLine && hasLineFinished(&parser) + } + private static let sectionNameCharsWithoutBrackets = Set("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?") private static let sectionNameChars = Set("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()") } diff --git a/Sources/HummingbirdMustache/Template+Render.swift b/Sources/HummingbirdMustache/Template+Render.swift index 6f5b006..778b3a9 100644 --- a/Sources/HummingbirdMustache/Template+Render.swift +++ b/Sources/HummingbirdMustache/Template+Render.swift @@ -8,7 +8,7 @@ extension HBMustacheTemplate { /// - Returns: Rendered text func render(_ stack: [Any], context: HBMustacheContext? = nil, indentation: String? = nil) -> String { var string = "" - if let indentation = indentation { + if let indentation = indentation, indentation != "" { for token in tokens { if string.last == "\n" { string += indentation