From c30cc6697fb3d445034b249a8247eb6e236af651 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 2 Jun 2022 00:42:23 +0800 Subject: [PATCH] Repo // NSStringUtils -> StringUtils. --- .../ControllerModules/InputState.swift | 31 ++++++++----------- .../ControllerModules/KeyHandler_States.swift | 14 ++++----- ...{NSStringUtils.swift => StringUtils.swift} | 26 +++++++--------- .../LangModelRelated/mgrLangModel.swift | 5 ++- vChewing.xcodeproj/project.pbxproj | 8 ++--- 5 files changed, 37 insertions(+), 47 deletions(-) rename Source/Modules/ControllerModules/{NSStringUtils.swift => StringUtils.swift} (79%) diff --git a/Source/Modules/ControllerModules/InputState.swift b/Source/Modules/ControllerModules/InputState.swift index 83b5e684..c322ab20 100644 --- a/Source/Modules/ControllerModules/InputState.swift +++ b/Source/Modules/ControllerModules/InputState.swift @@ -176,7 +176,7 @@ class InputState { return "" } - let text = (composingBuffer as NSString).substring(with: markedRange) + let text = composingBuffer.substring(with: markedRange) if markedRange.length < kMinMarkRangeLength { ctlInputMethod.tooltipController.setColor(state: .denialInsufficiency) return String( @@ -194,9 +194,8 @@ class InputState { ) } - let (exactBegin, _) = (composingBuffer as NSString).characterIndex( - from: markedRange.location) - let (exactEnd, _) = (composingBuffer as NSString).characterIndex( + let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location) + let (exactEnd, _) = composingBuffer.utf16CharIndex( from: markedRange.location + markedRange.length) let selectedReadings = readings[exactBegin.. 0 { - index = UInt((state.composingBuffer as NSString).previousUtf16Position(for: Int(index))) + index = UInt(state.composingBuffer.utf16PreviousPosition(for: Int(index))) let marking = InputState.Marking( composingBuffer: state.composingBuffer, cursorIndex: state.cursorIndex, @@ -226,10 +226,8 @@ extension KeyHandler { // Shift + Right if input.isCursorForward || input.emacsKey == vChewingEmacsKey.forward, input.isShiftHold { var index = state.markerIndex - // 這裡繼續用 NSString 是為了與 Zonble 之前引入的 NSStringUtils 相容。 - // 不然的話,這行判斷會失敗、引發「9B51408D」錯誤。 - if index < ((state.composingBuffer as NSString).length) { - index = UInt((state.composingBuffer as NSString).nextUtf16Position(for: Int(index))) + if index < (state.composingBuffer.utf16.count) { + index = UInt(state.composingBuffer.utf16NextPosition(for: Int(index))) let marking = InputState.Marking( composingBuffer: state.composingBuffer, cursorIndex: state.cursorIndex, @@ -565,8 +563,8 @@ extension KeyHandler { if input.isShiftHold { // Shift + Right - if currentState.cursorIndex < (currentState.composingBuffer as NSString).length { - let nextPosition = (currentState.composingBuffer as NSString).nextUtf16Position( + if currentState.cursorIndex < currentState.composingBuffer.utf16.count { + let nextPosition = currentState.composingBuffer.utf16NextPosition( for: Int(currentState.cursorIndex)) let marking: InputState.Marking! = InputState.Marking( composingBuffer: currentState.composingBuffer, @@ -615,7 +613,7 @@ extension KeyHandler { if input.isShiftHold { // Shift + left if currentState.cursorIndex > 0 { - let previousPosition = (currentState.composingBuffer as NSString).previousUtf16Position( + let previousPosition = currentState.composingBuffer.utf16PreviousPosition( for: Int(currentState.cursorIndex)) let marking: InputState.Marking! = InputState.Marking( composingBuffer: currentState.composingBuffer, diff --git a/Source/Modules/ControllerModules/NSStringUtils.swift b/Source/Modules/ControllerModules/StringUtils.swift similarity index 79% rename from Source/Modules/ControllerModules/NSStringUtils.swift rename to Source/Modules/ControllerModules/StringUtils.swift index f90b0710..4beae553 100644 --- a/Source/Modules/ControllerModules/NSStringUtils.swift +++ b/Source/Modules/ControllerModules/StringUtils.swift @@ -26,8 +26,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import Cocoa -extension NSString { - /// Converts the index in an NSString to the index in a Swift string. +extension String { + /// Converts the index in an NSString or .utf16 to the index in a Swift string. /// /// An Emoji might be compose by more than one UTF-16 code points, however /// the length of an NSString is only the sum of the UTF-16 code points. It @@ -35,8 +35,8 @@ extension NSString { /// string have different lengths once the string contains such Emoji. The /// method helps to find the index in a Swift string by passing the index /// in an NSString. - public func characterIndex(from utf16Index: Int) -> (Int, String) { - let string = (self as String) + public func utf16CharIndex(from utf16Index: Int) -> (Int, String) { + let string = self var length = 0 for (i, character) in string.enumerated() { length += character.utf16.count @@ -47,29 +47,27 @@ extension NSString { return (string.count, string) } - public func nextUtf16Position(for index: Int) -> Int { - var (fixedIndex, string) = characterIndex(from: index) + public func utf16NextPosition(for index: Int) -> Int { + var (fixedIndex, string) = utf16CharIndex(from: index) if fixedIndex < string.count { fixedIndex += 1 } return string[.. Int { - var (fixedIndex, string) = characterIndex(from: index) + public func utf16PreviousPosition(for index: Int) -> Int { + var (fixedIndex, string) = utf16CharIndex(from: index) if fixedIndex > 0 { fixedIndex -= 1 } return string[.. [NSString] { - Array(self as String).map { - NSString(string: String($0)) - } + public func substring(with nsRange: NSRange) -> String { + (self as NSString).substring(with: nsRange) } } diff --git a/Source/Modules/LangModelRelated/mgrLangModel.swift b/Source/Modules/LangModelRelated/mgrLangModel.swift index 2360ff47..770b4182 100644 --- a/Source/Modules/LangModelRelated/mgrLangModel.swift +++ b/Source/Modules/LangModelRelated/mgrLangModel.swift @@ -357,10 +357,9 @@ enum mgrLangModel { static func dataFolderPath(isDefaultFolder: Bool) -> String { let appSupportPath = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].path - var userDictPathSpecified = (mgrPrefs.userDataFolderSpecified as NSString).expandingTildeInPath + var userDictPathSpecified = mgrPrefs.userDataFolderSpecified.expandingTildeInPath var userDictPathDefault = - (URL(fileURLWithPath: appSupportPath).appendingPathComponent("vChewing").path as NSString) - .expandingTildeInPath + URL(fileURLWithPath: appSupportPath).appendingPathComponent("vChewing").path.expandingTildeInPath userDictPathDefault.ensureTrailingSlash() userDictPathSpecified.ensureTrailingSlash() diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index b5d162ad..92f54571 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -28,7 +28,7 @@ 5B62A32927AE77D100A19448 /* FSEventStreamHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A32827AE77D100A19448 /* FSEventStreamHelper.swift */; }; 5B62A33227AE792F00A19448 /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33127AE792F00A19448 /* InputSourceHelper.swift */; }; 5B62A33627AE795800A19448 /* mgrPrefs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33527AE795800A19448 /* mgrPrefs.swift */; }; - 5B62A33827AE79CD00A19448 /* NSStringUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33727AE79CD00A19448 /* NSStringUtils.swift */; }; + 5B62A33827AE79CD00A19448 /* StringUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33727AE79CD00A19448 /* StringUtils.swift */; }; 5B62A33D27AE7CC100A19448 /* ctlAboutWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33C27AE7CC100A19448 /* ctlAboutWindow.swift */; }; 5B62A34627AE7CD900A19448 /* ctlCandidateHorizontal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A33F27AE7CD900A19448 /* ctlCandidateHorizontal.swift */; }; 5B62A34727AE7CD900A19448 /* ctlCandidate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A34027AE7CD900A19448 /* ctlCandidate.swift */; }; @@ -202,7 +202,7 @@ 5B62A32827AE77D100A19448 /* FSEventStreamHelper.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = FSEventStreamHelper.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5B62A33127AE792F00A19448 /* InputSourceHelper.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = InputSourceHelper.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5B62A33527AE795800A19448 /* mgrPrefs.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = mgrPrefs.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; - 5B62A33727AE79CD00A19448 /* NSStringUtils.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = NSStringUtils.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; + 5B62A33727AE79CD00A19448 /* StringUtils.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = StringUtils.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5B62A33C27AE7CC100A19448 /* ctlAboutWindow.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlAboutWindow.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5B62A33F27AE7CD900A19448 /* ctlCandidateHorizontal.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlCandidateHorizontal.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5B62A34027AE7CD900A19448 /* ctlCandidate.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlCandidate.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; @@ -421,7 +421,7 @@ 5B7F225C2808501000DDD3CB /* KeyHandler_HandleInput.swift */, 5B61B0C9280BEFD4002E3CFA /* KeyHandler_Misc.swift */, 5B3133BE280B229700A4A505 /* KeyHandler_States.swift */, - 5B62A33727AE79CD00A19448 /* NSStringUtils.swift */, + 5B62A33727AE79CD00A19448 /* StringUtils.swift */, 5BAA8FBD282CAF380066C406 /* SyllableComposer.swift */, 5BF8423027BAA942008E7E4C /* vChewingKanjiConverter.swift */, ); @@ -1074,7 +1074,7 @@ 5B38F5A4281E2E49007D5F5D /* 5_LanguageModel.swift in Sources */, 5BAEFAD028012565001F42C9 /* mgrLangModel.swift in Sources */, 5B782EC4280C243C007276DE /* KeyHandler_HandleCandidate.swift in Sources */, - 5B62A33827AE79CD00A19448 /* NSStringUtils.swift in Sources */, + 5B62A33827AE79CD00A19448 /* StringUtils.swift in Sources */, 5BA9FD0F27FEDB6B002DE248 /* suiPrefPaneGeneral.swift in Sources */, 5BA9FD4927FEF3C9002DE248 /* Section.swift in Sources */, 5BA9FD3E27FEF3C8002DE248 /* Utilities.swift in Sources */,