Skip to content

Commit

Permalink
add ability to ignore certain tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed May 6, 2024
1 parent 3a16acd commit 9e89260
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Sources/String+Typographizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import Foundation

extension String {
public func typographized(language: String, isHTML: Bool = false, debug: Bool = false, measurePerformance: Bool = false) -> String {
public func typographized(language: String, isHTML: Bool = false, ignore: [UnicodeScalar] = [], debug: Bool = false, measurePerformance: Bool = false) -> String {
var t = Typographizer(language: language, text: self)
t.isHTML = isHTML
t.ignored = ignore
t.isDebugModeEnabled = debug
t.isMeasurePerformanceEnabled = measurePerformance

Expand Down
5 changes: 5 additions & 0 deletions Sources/Typographizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct Typographizer {
var isDebugModeEnabled = false
var isMeasurePerformanceEnabled = false
var isHTML = false
var ignored: [UnicodeScalar] = []

private var openingDoubleQuote: String = "·"
private var closingDoubleQuote: String = "·"
Expand Down Expand Up @@ -165,6 +166,10 @@ struct Typographizer {

private mutating func nextToken() throws -> Token? {
while let ch = self.nextScalar() {
if ignored.contains(ch) {
return try self.unchangedToken(ch)
}

switch ch {
case "´",
"`":
Expand Down
4 changes: 4 additions & 0 deletions TypographizerTests/TypographizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class TypographizerTests: XCTestCase {
XCTAssertEqual("John's.".typographized(language: "en"), "John’s.")
}

func testIgnore() {
XCTAssertEqual("`Hi´".typographized(language: "en", ignore: ["`", "´"]), "`Hi´")
}

func testPerformanceExample() {
let string = Array(repeating: self.string, count: 1000).joined(separator: " ")
self.measure {
Expand Down

0 comments on commit 9e89260

Please sign in to comment.