1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

CharacterMap: Add a find-by-name window

This works the same way as the command-line usage, searching against the
display name as provided by LibUnicode.

I've modified the search loop to cover every possible unicode
code-point, since my previous logic was flawed. Code-points are not
dense, there are gaps, so simply iterating up to the count of them will
skip ones with higher values. Surprisingly, iterating all 1,114,112 of
them still runs in a third of a second. Computers are fast!
This commit is contained in:
Sam Atkins 2022-01-12 16:12:59 +00:00 committed by Andreas Kling
parent 2bf7abcb28
commit 2a7c638cd9
8 changed files with 202 additions and 24 deletions

View file

@ -5,6 +5,7 @@
*/
#include "CharacterMapWidget.h"
#include "SearchCharacters.h"
#include <LibConfig/Client.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
@ -13,34 +14,19 @@
#include <LibGUI/Window.h>
#include <LibGfx/FontDatabase.h>
#include <LibMain/Main.h>
#include <LibUnicode/CharacterTypes.h>
static void search_and_print_results(String const& query)
{
outln("Searching for '{}'", query);
String uppercase_query = query.to_uppercase();
StringView uppercase_query_view = uppercase_query.view();
// FIXME: At time of writing there are 144,697 code points in Unicode. I've added some breathing room,
// but ideally this would be defined in LibUnicode somewhere.
constexpr u32 unicode_character_count = 150000;
// FIXME: There's probably a better way to do this than just looping, but it still only takes ~150ms to run for me!
u32 result_count = 0;
for (u32 i = 1; i < unicode_character_count; ++i) {
if (auto maybe_display_name = Unicode::code_point_display_name(i); maybe_display_name.has_value()) {
auto& display_name = maybe_display_name.value();
// FIXME: This should be a case-sensitive search, since we already converted the query to uppercase
// and the unicode names are all in uppercase. But, that makes it run slower!
// Sensitive: ~175ms, Insensitive: ~140ms
if (display_name.contains(uppercase_query_view, AK::CaseSensitivity::CaseInsensitive)) {
StringBuilder builder;
builder.append_code_point(i);
builder.append(" - ");
builder.append(display_name);
outln(builder.string_view());
result_count++;
}
}
}
for_each_character_containing(query, [&](auto code_point, auto& display_name) {
StringBuilder builder;
builder.append_code_point(code_point);
builder.append(" - ");
builder.append(display_name);
outln(builder.string_view());
result_count++;
});
if (result_count == 0)
outln("No results found.");