Skip to content

Commit

Permalink
Refactor straighteningQuotes
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jan 27, 2025
1 parent 35c5db6 commit 0523e53
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2014-2024 1024jp
// © 2014-2025 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -183,9 +183,6 @@ extension EditorTextView {
/// Straightens all curly quotes.
@IBAction func straightenQuotesInSelection(_ sender: Any?) {

self.transformSelection {
$0.replacing(/[“”‟„]/, with: "\"")
.replacing(/[‘’‛‚]/, with: "'")
}
self.transformSelection(to: \.straighteningQuotes)
}
}
23 changes: 16 additions & 7 deletions Packages/EditorCore/Sources/StringUtils/String+Case.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2018-2024 1024jp
// © 2018-2025 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -28,7 +28,15 @@ import Foundation.NSRegularExpression

public extension String {

/// Transform all camel and pascal case words to snake case.
/// Straightens all curly quotes.
var straighteningQuotes: String {

self.replacing(/[‘’‚‛]/, with: "'") // U+2018..201B
.replacing(/[“”„‟]/, with: "\"") // U+201C..201F
}


/// The string transformed all camel- and pascal-case words to snake-case.
var snakecased: String {

self.ranges(pattern: "(?<=\\w)(?=\\p{uppercase})")
Expand All @@ -39,7 +47,7 @@ public extension String {
}


/// Transform all snake and pascal case words to camel case.
/// The string transformed all snake- and pascal-case words to camel-case.
var camelcased: String {

self.ranges(pattern: "(?<=\\w)(?:\\p{uppercase}|_\\w)")
Expand All @@ -50,7 +58,7 @@ public extension String {
}


/// Transform all snake and camel case words to pascal case.
/// The string transformed all snake- and camel-case words to pascal-case.
var pascalcased: String {

self.ranges(pattern: "(?:\\b|(?<=\\w)_)\\w")
Expand All @@ -64,9 +72,10 @@ public extension String {

// MARK: Private Methods

/// <#Description#>
/// - Parameter pattern: <#pattern description#>
/// - Returns: <#description#>
/// Returns the ranges matching the given regular expression pattern.
///
/// - Parameter pattern: The regular expression pattern.
/// - Returns: The ranges of matched.
private func ranges(pattern: String) -> [Range<Index>] {

(try! NSRegularExpression(pattern: pattern))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2015-2024 1024jp
// © 2015-2025 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,6 +68,14 @@ struct StringExtensionsTests {
}


@Test func straightenQuotes() {

#expect("I am a “dog.”".straighteningQuotes == "I am a \"dog.\"")
#expect("I am a ‘dog.’".straighteningQuotes == "I am a 'dog.'")
#expect("type `echo`".straighteningQuotes == "type `echo`")
}


@Test func codingCases() {

#expect("AbcDefg Hij".snakecased == "abc_defg hij")
Expand Down

0 comments on commit 0523e53

Please sign in to comment.