From af08a31efafdb7f518e46e8ece0fcc26bddc9a0a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 2 Jun 2022 16:23:19 +0800 Subject: [PATCH] StringUtils // Advising against attempts of deprecating .utf16 handling. --- Source/Modules/ControllerModules/StringUtils.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/Modules/ControllerModules/StringUtils.swift b/Source/Modules/ControllerModules/StringUtils.swift index dc997a42..b47300ae 100644 --- a/Source/Modules/ControllerModules/StringUtils.swift +++ b/Source/Modules/ControllerModules/StringUtils.swift @@ -26,15 +26,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import Cocoa +/// Shiki's Notes: The cursor index in the IMK inline composition buffer +/// still uses UTF16 index measurements. This means that any attempt of +/// using Swift native UTF8 handlings to replace Zonble's NSString (or +/// .utf16) handlings below will still result in unavoidable necessities +/// of solving the UTF16->UTF8 conversions in another approach. Therefore, +/// I strongly advise against any attempt of such until the day that IMK is +/// capable of handling the cursor index in its inline composition buffer using +/// UTF8 measurements. + 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 + /// 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 /// causes that the NSString and Swift string representation of the same /// 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. + /// in an NSString (or .utf16). public func utf16CharIndex(from utf16Index: Int) -> (Int, String) { let string = self var length = 0