Repo // NSStringUtils -> StringUtils.

This commit is contained in:
ShikiSuen 2022-06-02 00:42:23 +08:00
parent 97c7918940
commit c30cc6697f
5 changed files with 37 additions and 47 deletions

View File

@ -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..<exactEnd]
let joined = selectedReadings.joined(separator: "-")
@ -254,7 +253,7 @@ class InputState {
],
range: NSRange(
location: end,
length: (composingBuffer as NSString).length - end
length: composingBuffer.utf16.count - end
)
)
return attributedString
@ -292,10 +291,9 @@ class InputState {
}
var chkIfUserPhraseExists: Bool {
let text = (composingBuffer as NSString).substring(with: markedRange)
let (exactBegin, _) = (composingBuffer as NSString).characterIndex(
from: markedRange.location)
let (exactEnd, _) = (composingBuffer as NSString).characterIndex(
let text = composingBuffer.substring(with: markedRange)
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length)
let selectedReadings = readings[exactBegin..<exactEnd]
let joined = selectedReadings.joined(separator: "-")
@ -306,10 +304,9 @@ class InputState {
}
var userPhrase: String {
let text = (composingBuffer as NSString).substring(with: markedRange)
let (exactBegin, _) = (composingBuffer as NSString).characterIndex(
from: markedRange.location)
let (exactEnd, _) = (composingBuffer as NSString).characterIndex(
let text = composingBuffer.substring(with: markedRange)
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length)
let selectedReadings = readings[exactBegin..<exactEnd]
let joined = selectedReadings.joined(separator: "-")
@ -318,11 +315,9 @@ class InputState {
var userPhraseConverted: String {
let text =
OpenCCBridge.crossConvert(
(composingBuffer as NSString).substring(with: markedRange)) ?? ""
let (exactBegin, _) = (composingBuffer as NSString).characterIndex(
from: markedRange.location)
let (exactEnd, _) = (composingBuffer as NSString).characterIndex(
OpenCCBridge.crossConvert(composingBuffer.substring(with: markedRange)) ?? ""
let (exactBegin, _) = composingBuffer.utf16CharIndex(from: markedRange.location)
let (exactEnd, _) = composingBuffer.utf16CharIndex(
from: markedRange.location + markedRange.length)
let selectedReadings = readings[exactBegin..<exactEnd]
let joined = selectedReadings.joined(separator: "-")

View File

@ -206,7 +206,7 @@ extension KeyHandler {
if input.isCursorBackward || input.emacsKey == vChewingEmacsKey.backward, input.isShiftHold {
var index = state.markerIndex
if index > 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,

View File

@ -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[..<string.index(string.startIndex, offsetBy: fixedIndex)].utf16.count
}
public func previousUtf16Position(for index: Int) -> 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[..<string.index(string.startIndex, offsetBy: fixedIndex)].utf16.count
}
public var count: Int {
(self as String).count
public var expandingTildeInPath: String {
(self as NSString).expandingTildeInPath
}
public func split() -> [NSString] {
Array(self as String).map {
NSString(string: String($0))
}
public func substring(with nsRange: NSRange) -> String {
(self as NSString).substring(with: nsRange)
}
}

View File

@ -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()

View File

@ -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 = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33127AE792F00A19448 /* InputSourceHelper.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = InputSourceHelper.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33527AE795800A19448 /* mgrPrefs.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = mgrPrefs.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33727AE79CD00A19448 /* NSStringUtils.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = NSStringUtils.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33727AE79CD00A19448 /* StringUtils.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = StringUtils.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33C27AE7CC100A19448 /* ctlAboutWindow.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlAboutWindow.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A33F27AE7CD900A19448 /* ctlCandidateHorizontal.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlCandidateHorizontal.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
5B62A34027AE7CD900A19448 /* ctlCandidate.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlCandidate.swift; sourceTree = "<group>"; 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 */,