From a82deb75a14ab37d2daa53bff0e0602c22fd2175 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 5 Mar 2022 11:13:37 +0800 Subject: [PATCH] UPE // Regex Optimization. --- UserPhraseEditor/StringExtension.swift | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/UserPhraseEditor/StringExtension.swift b/UserPhraseEditor/StringExtension.swift index 70faf0f0..8ab7384d 100644 --- a/UserPhraseEditor/StringExtension.swift +++ b/UserPhraseEditor/StringExtension.swift @@ -31,15 +31,13 @@ extension String { var strProcessed = self // 預處理格式 strProcessed = strProcessed.replacingOccurrences(of: " #MACOS", with: "") // 去掉 macOS 標記 - strProcessed = strProcessed.replacingOccurrences(of: " ", with: " ") // CJKWhiteSpace (\x{3000}) to ASCII Space - strProcessed = strProcessed.replacingOccurrences(of: " ", with: " ") // NonBreakWhiteSpace (\x{A0}) to ASCII Space - strProcessed = strProcessed.replacingOccurrences(of: "\t", with: " ") // Tab to ASCII Space - strProcessed.regReplace(pattern: "\\f", replaceWith: "\n") // Form Feed to LF - strProcessed = strProcessed.replacingOccurrences(of: "\r", with: "\n") // CR to LF - strProcessed.regReplace(pattern: " +", replaceWith: " ") // 統整連續空格為一個 ASCII 空格 - strProcessed.regReplace(pattern: "\\n+", replaceWith: "\n") // 統整連續 LF 為一個 LF - strProcessed = strProcessed.replacingOccurrences(of: " \n", with: "\n") // 去除行尾空格 - strProcessed = strProcessed.replacingOccurrences(of: "\n ", with: "\n") // 去除行首空格 + // CJKWhiteSpace (\x{3000}) to ASCII Space + // NonBreakWhiteSpace (\x{A0}) to ASCII Space + // Tab to ASCII Space + // 統整連續空格為一個 ASCII 空格 + strProcessed.regReplace(pattern: #"( +| +| +|\t+)+"#, replaceWith: " ") + strProcessed.regReplace(pattern: #"(\f+|\r+)+"#, replaceWith: "\n") // CR & Form Feed to LF + strProcessed.regReplace(pattern: #"(\n+| \n+|\n+ )"#, replaceWith: "\n") // 去除行尾行首空格與重複行 if strProcessed.prefix(1) == " " { // 去除檔案開頭空格 strProcessed.removeFirst() }