Skip to content

Truly independent web browser

UI/Qt: Share the conversions between NSString and UTF-16 strings

sideshowbarker005962c57ed9
diff --git a/UI/Qt/Conversions.h b/UI/Qt/Conversions.h
index 1a6c201a2c8..dffde03f1a6 100644
--- a/UI/Qt/Conversions.h
+++ b/UI/Qt/Conversions.h
@@ -25,6 +25,10 @@ namespace Ladybird {
String ns_string_to_string(NSString*);
NSString* string_to_ns_string(StringView);
+// NSString and our UTF-16 strings hold the same code units, lone surrogates included, so offsets carry over unchanged.
+Utf16String ns_string_to_utf16_string(NSString*);
+NSString* utf16_string_to_ns_string(Utf16View const&);
+
NSData* string_to_ns_data(StringView);
Gfx::Color ns_color_to_gfx_color(NSColor*);
diff --git a/UI/Qt/Conversions.mm b/UI/Qt/Conversions.mm
index b289c604596..bb4e4bdc888 100644
--- a/UI/Qt/Conversions.mm
+++ b/UI/Qt/Conversions.mm
@@ -22,6 +22,23 @@ NSString* string_to_ns_string(StringView string)
return [[NSString alloc] initWithData:string_to_ns_data(string) encoding:NSUTF8StringEncoding];
}
+Utf16String ns_string_to_utf16_string(NSString* string)
+{
+ Vector<char16_t> code_units;
+ code_units.resize(string.length);
+ [string getCharacters:reinterpret_cast<unichar*>(code_units.data()) range:NSMakeRange(0, string.length)];
+ return Utf16String::from_utf16(Utf16View { code_units.data(), code_units.size() });
+}
+
+NSString* utf16_string_to_ns_string(Utf16View const& string)
+{
+ Vector<unichar> code_units;
+ code_units.ensure_capacity(string.length_in_code_units());
+ for (size_t index = 0; index < string.length_in_code_units(); ++index)
+ code_units.unchecked_append(string.code_unit_at(index));
+ return [NSString stringWithCharacters:code_units.data() length:code_units.size()];
+}
+
NSData* string_to_ns_data(StringView string)
{
return [NSData dataWithBytes:string.characters_without_null_termination() length:string.length()];
diff --git a/UI/Qt/SpellCheckerMac.mm b/UI/Qt/SpellCheckerMac.mm
index d1fe2c20da0..a8796a5c7c2 100644
--- a/UI/Qt/SpellCheckerMac.mm
+++ b/UI/Qt/SpellCheckerMac.mm
@@ -8,6 +8,7 @@
#include <AK/HashMap.h>
#include <AK/RefPtr.h>
#include <LibCore/EventLoop.h>
+#include <UI/Qt/Conversions.h>
#include <UI/Qt/SpellCheckerMac.h>
#import <AppKit/AppKit.h>
@@ -24,23 +25,6 @@ namespace Ladybird {
// The private text checking type that asks for a single completion of the word at the caret.
static constexpr NSTextCheckingTypes text_checking_type_single_completion = 1ULL << 29;
-// NSString and our strings hold the same UTF-16 code units, lone surrogates included, so offsets carry over unchanged.
-static NSString* ns_string_from_utf16(Utf16String const& string)
-{
- auto view = string.utf16_view();
- Vector<unichar> code_units;
- code_units.ensure_capacity(view.length_in_code_units());
- for (size_t index = 0; index < view.length_in_code_units(); ++index)
- code_units.unchecked_append(view.code_unit_at(index));
- return [[NSString alloc] initWithCharacters:code_units.data() length:code_units.size()];
-}
-
-static Utf16String utf16_from_ns_string(NSString* string)
-{
- char const* utf8 = [string UTF8String];
- return Utf16String::from_utf8(StringView { utf8, strlen(utf8) });
-}
-
// Answers come back on an arbitrary queue. The callbacks that take them stay on the main thread, keyed by request, so
// only this atomically counted holder crosses threads.
struct PendingChecks : public AtomicRefCounted<PendingChecks> {
@@ -71,7 +55,7 @@ public:
m_pending_checks->callbacks.set(id, move(on_complete));
RefPtr<PendingChecks> pending_checks = m_pending_checks;
RefPtr<Core::WeakEventLoopReference> event_loop = Core::EventLoop::current_weak();
- auto* string = ns_string_from_utf16(text);
+ auto* string = utf16_string_to_ns_string(text);
auto handler = ^(NSInteger, NSArray<NSTextCheckingResult*>* results, NSOrthography*, NSInteger) {
Vector<Web::SpellingError> errors;
for (NSTextCheckingResult* result in results) {
@@ -97,26 +81,26 @@ public:
virtual Vector<Utf16String> guesses(DocumentTag tag, Utf16String const& word) override
{
- auto* string = ns_string_from_utf16(word);
+ auto* string = utf16_string_to_ns_string(word);
NSArray<NSString*>* guesses = [[NSSpellChecker sharedSpellChecker] guessesForWordRange:NSMakeRange(0, string.length)
inString:string
language:nil
inSpellDocumentWithTag:tag];
Vector<Utf16String> result;
for (NSString* guess in guesses)
- result.append(utf16_from_ns_string(guess));
+ result.append(ns_string_to_utf16_string(guess));
return result;
}
virtual void ignore(DocumentTag tag, Utf16String const& word) override
{
- [[NSSpellChecker sharedSpellChecker] ignoreWord:ns_string_from_utf16(word) inSpellDocumentWithTag:tag];
+ [[NSSpellChecker sharedSpellChecker] ignoreWord:utf16_string_to_ns_string(word) inSpellDocumentWithTag:tag];
}
// NB: The system dictionary learns the word for every app, so the tab's tag plays no part.
virtual void learn(DocumentTag, Utf16String const& word) override
{
- [[NSSpellChecker sharedSpellChecker] learnWord:ns_string_from_utf16(word)];
+ [[NSSpellChecker sharedSpellChecker] learnWord:utf16_string_to_ns_string(word)];
}
virtual bool can_suggest() const override
@@ -137,7 +121,7 @@ public:
m_pending_suggestions->callbacks.set(id, move(on_complete));
RefPtr<PendingSuggestions> pending_suggestions = m_pending_suggestions;
RefPtr<Core::WeakEventLoopReference> event_loop = Core::EventLoop::current_weak();
- auto* string = ns_string_from_utf16(text);
+ auto* string = utf16_string_to_ns_string(text);
auto handler = ^(NSInteger, NSArray<NSTextCheckingResult*>* candidates) {
auto strong_event_loop = event_loop->take();
if (!strong_event_loop)
@@ -150,7 +134,7 @@ public:
suggestion = Web::WritingSuggestion {
static_cast<u32>(candidate.range.location),
static_cast<u32>(candidate.range.length),
- utf16_from_ns_string(candidate.replacementString),
+ ns_string_to_utf16_string(candidate.replacementString),
};
}
if (auto callback = pending_suggestions->callbacks.take(id); callback.has_value())