SDL: make start&length represent utf32 indices

From 9a607e886e675ae80508116c440407ce724b028f Mon Sep 17 00:00:00 2001
From: williamhCode <[EMAIL REDACTED]>
Date: Fri, 21 Feb 2025 15:35:49 -0500
Subject: [PATCH] make start&length represent utf32 indices

---
 src/video/cocoa/SDL_cocoakeyboard.m | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m
index df614e82d60dd..2443dbaf8a301 100644
--- a/src/video/cocoa/SDL_cocoakeyboard.m
+++ b/src/video/cocoa/SDL_cocoakeyboard.m
@@ -133,8 +133,12 @@ - (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replaceme
     // This key event was consumed by the IME
     [self clearPendingKey];
 
+    NSUInteger utf32SelectedRangeLocation = [[aString substringToIndex:selectedRange.location] lengthOfBytesUsingEncoding:NSUTF32StringEncoding] / 4;
+    NSUInteger utf32SelectionRangeEnd = [[aString substringToIndex:(selectedRange.location + selectedRange.length)] lengthOfBytesUsingEncoding:NSUTF32StringEncoding] / 4;
+    NSUInteger utf32SelectionRangeLength = utf32SelectionRangeEnd - utf32SelectedRangeLocation;
+
     SDL_SendEditingText([aString UTF8String],
-                        (int)selectedRange.location, (int)selectedRange.length);
+                        (int)utf32SelectedRangeLocation, (int)utf32SelectionRangeLength);
 
     DEBUG_IME(@"setMarkedText: %@, (%d, %d) replacement range (%d, %d)", _markedText,
               (int)selectedRange.location, (int)selectedRange.length,