Skip to content

Commit 8dee501

Browse files
committed
refactor(tsf): share the profile-GUID match as a header-only helper
`ImeModule::loadImeInfo` compared the flat `guid` and then walked `additionalProfiles` inline, so this one of the four dispatchers had no way to be exercised outside the `build-and-sign` job: the vendored mingw carries no Windows SDK headers. Extracting the match into `PIMETextService/ImeProfileGuids.h`, which includes only `<string>` and the header-only nlohmann json, keeps the behaviour and lets the two pure-C++ helper tests upstream already runs gain a third sibling case. - `imeProfileGuidEqual`: ASCII case-insensitive, the same comparison `stricmp` gave, so a lowercased client id still meets an upper-case manifest. - `imeProfileGuidMatches`: the flat `guid` first, then each `additionalProfiles` entry; a non-object entry, a missing or non-string `guid`, and an empty `guid` are skipped without throwing. - `loadImeInfo` calls it: the 17 inline lines become one call (22 lines changed, 15 fewer), and the first-match `break` of the backend scan is unchanged, so `onConfigure` resolves `ime.json` from either profile GUID. Architecture and design by asdf80730. Implementation generated with AI assistance from omp (tensorfold/Qwen3.8-Flash-Next).
1 parent 564e3e2 commit 8dee501

2 files changed

Lines changed: 84 additions & 17 deletions

File tree

‎PIMETextService/ImeProfileGuids.h‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// Copyright (C) 2026 The PIME Developers
3+
//
4+
// This library is free software; you can redistribute it and/or
5+
// modify it under the terms of the GNU Library General Public
6+
// License as published by the Free Software Foundation; either
7+
// version 2 of the License, or (at your option) any later version.
8+
//
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
// Library General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Library General Public
15+
// License along with this library; if not, write to the
16+
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17+
// Boston, MA 02110-1301, USA.
18+
//
19+
// Generated by omp (Qwen3.8-Flash-Next) without human creative authorship.
20+
// SPDX-License-Identifier: LGPL-2.1-or-later
21+
//
22+
23+
#pragma once
24+
25+
// Header-only profile-GUID matching for the ime.json manifests, kept free of
26+
// Windows SDK includes so tests/ime_profile_guids_test.cpp builds with the
27+
// bare mingw g++ under .tools/mingw64.
28+
29+
#include <string>
30+
#include <nlohmann/json.hpp>
31+
32+
namespace PIME {
33+
34+
// ASCII case-insensitive equality, the same comparison stricmp gives the
35+
// product for the profile GUIDs.
36+
inline bool imeProfileGuidEqual(const std::string& a, const std::string& b) noexcept {
37+
if (a.size() != b.size()) {
38+
return false;
39+
}
40+
for (std::size_t i = 0; i < a.size(); ++i) {
41+
const char ca = (a[i] >= 'A' && a[i] <= 'Z') ? static_cast<char>(a[i] - 'A' + 'a') : a[i];
42+
const char cb = (b[i] >= 'A' && b[i] <= 'Z') ? static_cast<char>(b[i] - 'A' + 'a') : b[i];
43+
if (ca != cb) {
44+
return false;
45+
}
46+
}
47+
return true;
48+
}
49+
50+
// True when `id` names the profile of a parsed ime.json object: the flat
51+
// "guid" key, or one "guid" of the "additionalProfiles" array. A non-object
52+
// entry, a missing or non-string "guid" key, and an empty "guid" are skipped
53+
// without throwing; an empty `id` therefore matches nothing.
54+
inline bool imeProfileGuidMatches(const nlohmann::json& content, const std::string& id) noexcept {
55+
const auto guid = content.find("guid");
56+
if (guid != content.end() && guid->is_string() &&
57+
imeProfileGuidEqual(id, guid->get_ref<const std::string&>())) {
58+
return true;
59+
}
60+
const auto profiles = content.find("additionalProfiles");
61+
if (profiles != content.end() && profiles->is_array()) {
62+
for (const auto& profile : *profiles) {
63+
if (!profile.is_object()) {
64+
continue; // a null or scalar entry carries no guid
65+
}
66+
const auto entry = profile.find("guid");
67+
if (entry == profile.end() || !entry->is_string() ||
68+
entry->get_ref<const std::string&>().empty()) {
69+
continue; // a guid-less entry cannot name the profile
70+
}
71+
if (imeProfileGuidEqual(id, entry->get_ref<const std::string&>())) {
72+
return true;
73+
}
74+
}
75+
}
76+
return false;
77+
}
78+
79+
} // namespace PIME

‎PIMETextService/PIMEImeModule.cpp‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "PIMEImeModule.h"
2121
#include "JsonUtils.h"
22+
#include "ImeProfileGuids.h"
2223
#include "PIMETextService.h"
2324
#include <string>
2425
#include <fstream>
@@ -119,23 +120,10 @@ bool ImeModule::loadImeInfo(const std::string& guid, std::wstring& filePath, jso
119120
if (!content.is_object() || content.is_discarded()) {
120121
continue;
121122
}
122-
std::string imeGuid = jsonValueOr<std::string>(content, "guid", "");
123-
bool matched = (stricmp(guid.c_str(), imeGuid.c_str()) == 0);
124-
// The same service also lives under the GUID of each
125-
// additional language profile (additionalProfiles), so
126-
// onConfigure must find this ime.json from those too.
127-
if (!matched) {
128-
auto profiles = content.find("additionalProfiles");
129-
if (profiles != content.end() && profiles->is_array()) {
130-
for (const auto& profile : *profiles) {
131-
if (stricmp(guid.c_str(),
132-
jsonValueOr<std::string>(profile, "guid", "").c_str()) == 0) {
133-
matched = true;
134-
break;
135-
}
136-
}
137-
}
138-
}
123+
// The flat guid plus every additionalProfiles guid name
124+
// this service; the match is a header-only helper so the
125+
// local gates can run it without the Windows SDK.
126+
bool matched = imeProfileGuidMatches(content, guid);
139127
if (matched) {
140128
// found the language profile
141129
found = true;

0 commit comments

Comments
 (0)