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

Proposal: Support replacing regular expressions #123

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Sources/StencilSwiftKit/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Stencil

public extension Extension {
public func registerStencilSwiftExtensions() {
func registerStencilSwiftExtensions() {
registerTags()
registerStringsFilters()
registerNumbersFilters()
Expand Down
15 changes: 14 additions & 1 deletion Sources/StencilSwiftKit/Filters+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extension Filters.Strings {
while idx < scalars.endIndex, let scalar = UnicodeScalar(scalars[idx].value), characterSet.contains(scalar) {
idx = scalars.index(after: idx)
}
if idx > scalars.index(after: start) && idx < scalars.endIndex,
if !string.isEmpty && idx > scalars.index(after: start) && idx < scalars.endIndex,
let scalar = UnicodeScalar(scalars[idx].value),
CharacterSet.lowercaseLetters.contains(scalar) {
idx = scalars.index(before: idx)
Expand Down Expand Up @@ -264,9 +264,22 @@ extension Filters.Strings {
let source = try Filters.parseString(from: value)
let substring = try Filters.parseStringArgument(from: arguments, at: 0)
let replacement = try Filters.parseStringArgument(from: arguments, at: 1)
if substring.first == "/" && substring.last == "/" {
let regex = String(substring.dropFirst().dropLast())
return replaceRegex(source: source, regex: regex, replacement: replacement)
}
return source.replacingOccurrences(of: substring, with: replacement)
}

/// Replaces in the given string the given regular expression with the replacement
/// - Parameters:
/// - source: The source string
/// - regex: The regular expression
/// - replacement: The replacement string
private static func replaceRegex(source: String, regex: String, replacement: String) -> String {
return source.replacingOccurrences(of: regex, with: replacement, options: .regularExpression)
}

/// Converts an arbitrary string to a valid swift identifier. Takes an optional Mode argument:
/// - normal (default): uppercase the first character, prefix with an underscore if starting
/// with a number, replace invalid characters by underscores
Expand Down
5 changes: 3 additions & 2 deletions Tests/StencilSwiftKitTests/StringFiltersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,12 @@ extension StringFiltersTests {

extension StringFiltersTests {
func testReplace() throws {
let expectations = [
let expectations: [(Input, String, String, String)] = [
(Input(string: "string"), "ing", "oke", "stroke"),
(Input(string: "string"), "folks", "mates", "string"),
(Input(string: "hi mates!"), "hi", "Yo", "Yo mates!"),
(Input(string: "string with spaces"), " ", "_", "string_with_spaces")
(Input(string: "string with spaces"), " ", "_", "string_with_spaces"),
(Input(string: "string with numbers 42"), "/\\s\\d+$/", "", "string with numbers")
]

for (input, substring, replacement, expected) in expectations {
Expand Down