Rename package to swift-mustache (#27)
* Rename package to swift-mustache * Update CI
This commit is contained in:
38
Sources/Mustache/String.swift
Normal file
38
Sources/Mustache/String.swift
Normal file
@@ -0,0 +1,38 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Hummingbird server framework project
|
||||
//
|
||||
// Copyright (c) 2021-2021 the Hummingbird authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension String {
|
||||
private static let htmlEscapedCharacters: [Character: String] = [
|
||||
"<": "<",
|
||||
">": ">",
|
||||
"&": "&",
|
||||
"\"": """,
|
||||
"'": "'",
|
||||
]
|
||||
/// HTML escape string. Replace '<', '>' and '&' with HTML escaped versions
|
||||
func htmlEscape() -> String {
|
||||
var newString = ""
|
||||
newString.reserveCapacity(count)
|
||||
// currently doing this by going through each character could speed
|
||||
// this us by treating as an array of UInt8's
|
||||
for c in self {
|
||||
if let replacement = Self.htmlEscapedCharacters[c] {
|
||||
newString += replacement
|
||||
} else {
|
||||
newString.append(c)
|
||||
}
|
||||
}
|
||||
return newString
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user