Merge pull request #306 from lkuczborski/variable-crash-fix

Fix for crashing range indexes when variable length is 1
This commit is contained in:
David Jennes
2021-04-10 18:08:27 +02:00
committed by GitHub
3 changed files with 17 additions and 3 deletions

View File

@@ -14,7 +14,9 @@ _None_
### Bug Fixes
_None_
- Fix for crashing range indexes when variable length is 1.
[Łukasz Kuczborski](https://github.com/lkuczborski)
[#306](https://github.com/stencilproject/Stencil/pull/306)
### Internal Changes

View File

@@ -48,7 +48,7 @@ public struct Variable: Equatable, Resolvable {
/// Resolve the variable in the given context
public func resolve(_ context: Context) throws -> Any? {
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
if variable.count > 1 && ((variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\""))) {
// String literal
return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)])
}

View File

@@ -63,13 +63,25 @@ final class VariableTests: XCTestCase {
let result = try variable.resolve(self.context) as? String
try expect(result) == "name"
}
it("can resolve a string literal with one double quote") {
let variable = Variable("\"")
let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil()
}
it("can resolve a string literal with single quotes") {
let variable = Variable("'name'")
let result = try variable.resolve(self.context) as? String
try expect(result) == "name"
}
it("can resolve a string literal with one single quote") {
let variable = Variable("'")
let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil()
}
it("can resolve an integer literal") {
let variable = Variable("5")
let result = try variable.resolve(self.context) as? Int