From c0c7cfacc227d1412b16d3a17b72d31c56ebf560 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Fri, 30 Aug 2019 13:05:20 +0200 Subject: [PATCH] Fix NSLog() printing of unicode format strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://stackoverflow.com/questions/720052/nslog-incorrect-encoding. The `%s` format placeholder for `NSLog()` expects an encoding other than UTF-8, which leads to garbled Unicode characters when trying to print a UTF-8 encoded string. ```Objective-C NSLog(@"Ä %s %@", "Ä", @("Ä")); ``` prints `Ä √Ñ Ä`. As can be seen in the example above, the workaround is to convert the UTF-8 encoded C-string to an `NSString` object and print that instead. `debug_key()` calls `NSLog()`. --- glfw/cocoa_window.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index e896d3bf5..d576ce222 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -952,13 +952,13 @@ is_ascii_control_char(char x) { &char_count, text ) != noErr) { - debug_key(@"UCKeyTranslate failed for scancode: 0x%x (%s) %s\n", - scancode, safe_name_for_scancode(scancode), format_mods(mods)); + debug_key(@"UCKeyTranslate failed for scancode: 0x%x (%@) %@\n", + scancode, @(safe_name_for_scancode(scancode)), @(format_mods(mods))); window->ns.deadKeyState = 0; return; } - debug_key(@"scancode: 0x%x (%s) %schar_count: %lu deadKeyState: %u repeat: %d", - scancode, safe_name_for_scancode(scancode), format_mods(mods), char_count, window->ns.deadKeyState, event.ARepeat); + debug_key(@"scancode: 0x%x (%@) %@char_count: %lu deadKeyState: %u repeat: %d", + scancode, @(safe_name_for_scancode(scancode)), @(format_mods(mods)), char_count, window->ns.deadKeyState, event.ARepeat); if (process_text) { // this will call insertText which will fill up _glfw.ns.text [self interpretKeyEvents:[NSArray arrayWithObject:event]]; @@ -967,7 +967,7 @@ is_ascii_control_char(char x) { } if (window->ns.deadKeyState && (char_count == 0 || scancode == 0x75)) { // 0x75 is the delete key which needs to be ignored during a compose sequence - debug_key(@"Sending pre-edit text for dead key (text: %s markedText: %@).\n", format_text(_glfw.ns.text), markedText); + debug_key(@"Sending pre-edit text for dead key (text: %@ markedText: %@).\n", @(format_text(_glfw.ns.text)), markedText); _glfwInputKeyboard(window, key, scancode, GLFW_PRESS, mods, [[markedText string] UTF8String], 1); // update pre-edit text return; @@ -979,8 +979,8 @@ is_ascii_control_char(char x) { } } if (is_ascii_control_char(_glfw.ns.text[0])) _glfw.ns.text[0] = 0; // don't send text for ascii control codes - debug_key(@"text: %s glfw_key: %s marked_text: %@\n", - format_text(_glfw.ns.text), _glfwGetKeyName(key), markedText); + debug_key(@"text: %@ glfw_key: %@ marked_text: %@\n", + @(format_text(_glfw.ns.text)), @(_glfwGetKeyName(key)), markedText); if (!window->ns.deadKeyState) { if ([self hasMarkedText]) { _glfwInputKeyboard(window, key, scancode, GLFW_PRESS, mods,