Skip to content

Commit eaf8a0d

Browse files
committed
updated comments
1 parent 704f966 commit eaf8a0d

File tree

3 files changed

+40
-75
lines changed

3 files changed

+40
-75
lines changed

src/LanguageManager.cpp

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,35 @@
2121
#include <fstream>
2222
#include <regex>
2323
#include <sstream>
24+
#include <unordered_map>
2425
#include <windows.h>
25-
#include <map>
2626

2727
namespace {
28-
29-
// Build a cache key from id + placeholders.
30-
static std::wstring makeKey(const std::wstring& id,
28+
// --- Local helpers ----------------------------------------------------
29+
std::wstring makeKey(const std::wstring& id,
3130
const std::vector<std::wstring>& repl)
3231
{
33-
// Use a rarely used unit separator as delimiter.
3432
std::wstring key = id;
3533
key.push_back(L'\x1F');
36-
for (const auto& r : repl) {
37-
key += r;
38-
key.push_back(L'\x1F');
39-
}
34+
for (const auto& r : repl) { key += r; key.push_back(L'\x1F'); }
4035
return key;
4136
}
4237

43-
// Global cache holder for getLPCW(); single instance per process.
44-
static std::unordered_map<std::wstring, std::wstring>& lpcwCache()
38+
std::unordered_map<std::wstring, std::wstring>& lpcwCache()
4539
{
4640
static std::unordered_map<std::wstring, std::wstring> cache;
4741
return cache;
4842
}
49-
5043
}
5144

52-
// -----------------------------------------------------------------
53-
// Singleton
54-
// -----------------------------------------------------------------
45+
// --- Singleton ------------------------------------------------------------
5546
LanguageManager& LanguageManager::instance()
5647
{
5748
static LanguageManager mgr;
5849
return mgr;
5950
}
6051

61-
// -----------------------------------------------------------------
62-
// Public loading helpers
63-
// -----------------------------------------------------------------
52+
// --- Loading --------------------------------------------------------------
6453
bool LanguageManager::load(const std::wstring& pluginDir,
6554
const std::wstring& nativeLangXmlPath)
6655
{
@@ -77,8 +66,7 @@ bool LanguageManager::load(const std::wstring& pluginDir,
7766
bool LanguageManager::loadFromIni(const std::wstring& iniFile,
7867
const std::wstring& languageCode)
7968
{
80-
// 1) fallback = English
81-
_table = languageMap;
69+
_table = languageMap; // fallback: English
8270

8371
if (!_cache.load(iniFile))
8472
return false;
@@ -88,7 +76,7 @@ bool LanguageManager::loadFromIni(const std::wstring& iniFile,
8876
for (const auto& kv : it->second)
8977
_table[kv.first] = kv.second;
9078

91-
invalidateCaches(); // Clear derived caches after language change
79+
invalidateCaches();
9280
return true;
9381
}
9482

@@ -97,65 +85,61 @@ void LanguageManager::invalidateCaches()
9785
lpcwCache().clear();
9886
}
9987

100-
// -----------------------------------------------------------------
101-
// String getters
102-
// -----------------------------------------------------------------
103-
// Replace <br/>, then $REPLACE_n (descending), then $REPLACE.
88+
// --- Strings --------------------------------------------------------------
89+
// <br/> -> CRLF, then $REPLACE_STRINGn (high->low), then $REPLACE_STRING.
10490
std::wstring LanguageManager::get(const std::wstring& id,
10591
const std::vector<std::wstring>& repl) const
10692
{
10793
auto it = _table.find(id);
10894
if (it == _table.end())
109-
return id; // developer-friendly fallback: show missing key
95+
return id;
11096

11197
std::wstring result = it->second;
11298
const std::wstring base = L"$REPLACE_STRING";
11399

114-
// 1) Replace <br/> with CRLF (all occurrences)
100+
// <br/> -> CRLF
115101
for (size_t p = result.find(L"<br/>");
116102
p != std::wstring::npos;
117103
p = result.find(L"<br/>", p))
118104
{
119105
result.replace(p, 5, L"\r\n");
120-
p += 2; // advance beyond inserted CRLF to avoid re-scan at same spot
106+
p += 2;
121107
}
122108

123-
// 2) Numbered placeholders: $REPLACE_STRING1, $REPLACE_STRING2, ... (highest index first)
109+
// $REPLACE_STRINGn
124110
for (size_t i = repl.size(); i > 0; --i)
125111
{
126112
const std::wstring ph = base + std::to_wstring(i);
127-
const std::wstring& val = repl[i - 1];
113+
const std::wstring& vv = repl[i - 1];
128114

129115
for (size_t p = result.find(ph);
130116
p != std::wstring::npos;
131117
p = result.find(ph, p))
132118
{
133-
result.replace(p, ph.size(), val);
134-
p += val.size(); // skip over inserted text
119+
result.replace(p, ph.size(), vv);
120+
p += vv.size();
135121
}
136122
}
137123

138-
// 3) Plain $REPLACE_STRING -> repl[0] (empty if not provided)
124+
// $REPLACE_STRING
139125
{
140-
const std::wstring& val = repl.empty() ? std::wstring() : repl[0];
126+
const std::wstring& vv = repl.empty() ? std::wstring() : repl[0];
141127

142128
for (size_t p = result.find(base);
143129
p != std::wstring::npos;
144130
p = result.find(base, p))
145131
{
146-
result.replace(p, base.size(), val);
147-
p += val.size(); // skip over inserted text
132+
result.replace(p, base.size(), vv);
133+
p += vv.size();
148134
}
149135
}
150136

151137
return result;
152138
}
153139

154-
155140
LPCWSTR LanguageManager::getLPCW(const std::wstring& id,
156141
const std::vector<std::wstring>& repl) const
157142
{
158-
// Cache per (id + repl) to avoid wrong reuse for different placeholders.
159143
auto& cache = lpcwCache();
160144
const std::wstring key = makeKey(id, repl);
161145

@@ -166,24 +150,20 @@ LPCWSTR LanguageManager::getLPCW(const std::wstring& id,
166150
return it->second.c_str();
167151
}
168152

169-
170153
LPWSTR LanguageManager::getLPW(const std::wstring& id,
171154
const std::vector<std::wstring>& repl) const
172155
{
173-
// Use a thread-local buffer to avoid data races and overwrite issues.
174156
thread_local std::wstring buf;
175157
buf = get(id, repl);
176-
return buf.empty() ? nullptr : &buf[0];
158+
return buf.empty() ? nullptr : buf.data();
177159
}
178160

179-
180-
// -----------------------------------------------------------------
181-
// Detect active language from Notepad++ nativeLang.xml
182-
// -----------------------------------------------------------------
161+
// --- nativeLang.xml detection --------------------------------------------
183162
std::wstring LanguageManager::detectLanguage(const std::wstring& xmlPath)
184163
{
185164
std::wifstream file(xmlPath);
186-
if (!file.is_open()) return L"english";
165+
if (!file.is_open())
166+
return L"english";
187167

188168
std::wregex rx(L"<Native-Langue .*? filename=\"(.*?)\\.xml\"");
189169
std::wsmatch m;
@@ -196,8 +176,7 @@ std::wstring LanguageManager::detectLanguage(const std::wstring& xmlPath)
196176
break;
197177
}
198178
}
199-
catch (...) {
200-
// keep fallback
201-
}
179+
catch (...) { /* keep fallback */ }
180+
202181
return lang;
203182
}

src/LanguageManager.h

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,29 @@
2323
// --------------------------------------------------------------
2424

2525
#include "IniFileCache.h"
26-
2726
#include <string>
2827
#include <vector>
2928
#include <unordered_map>
30-
#include <map>
31-
#include <windows.h> // LPCWSTR / LPWSTR
29+
#include <windows.h>
3230

3331
class LanguageManager
3432
{
3533
public:
36-
// Singleton accessor
34+
// --- Singleton --------------------------------------------------------
3735
static LanguageManager& instance();
3836

39-
// ---------------------------------------------------------------------
40-
// Loading
41-
// ---------------------------------------------------------------------
42-
// Auto‑detect language code, then call loadFromIni(...)
43-
bool load(const std::wstring& pluginDir,
44-
const std::wstring& nativeLangXml);
45-
46-
// Explicit: give path to languages.ini + language code (en_US, de_DE …)
47-
bool loadFromIni(const std::wstring& iniFile,
48-
const std::wstring& languageCode);
37+
// --- Loading ----------------------------------------------------------
38+
bool load(const std::wstring& pluginDir, const std::wstring& nativeLangXml);
39+
bool loadFromIni(const std::wstring& iniFile, const std::wstring& languageCode);
4940

50-
// ---------------------------------------------------------------------
51-
// String retrieval
52-
// ---------------------------------------------------------------------
41+
// --- Strings ----------------------------------------------------------
5342
std::wstring get(const std::wstring& id,
5443
const std::vector<std::wstring>& repl = {}) const;
55-
56-
LPCWSTR getLPCW(const std::wstring& id,
44+
LPCWSTR getLPCW(const std::wstring& id,
5745
const std::vector<std::wstring>& repl = {}) const;
58-
59-
LPWSTR getLPW(const std::wstring& id,
46+
LPWSTR getLPW(const std::wstring& id,
6047
const std::vector<std::wstring>& repl = {}) const;
6148

62-
// Raw cache access (rarely required)
6349
const IniFileCache& ini() const { return _cache; }
6450

6551
private:
@@ -71,6 +57,6 @@ class LanguageManager
7157
static std::wstring detectLanguage(const std::wstring& nativeLangXmlPath);
7258
void invalidateCaches();
7359

74-
IniFileCache _cache; // languages.ini only
75-
std::unordered_map<std::wstring, std::wstring> _table; // id text
76-
};
60+
IniFileCache _cache; // languages.ini
61+
std::unordered_map<std::wstring, std::wstring> _table; // id -> text
62+
};

src/UndoRedoManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class UndoRedoManager
6767
std::vector<Item> _undo;
6868
std::vector<Item> _redo;
6969

70-
size_t _capacity = 200; // limited stps
70+
size_t _capacity = 200; // limited steps
7171
void trim() {
7272
if (_capacity == 0) return;
7373
while (_undo.size() > _capacity) _undo.erase(_undo.begin());

0 commit comments

Comments
 (0)