Skip to content

Commit

Permalink
Enabled escaping special characters in S-expression strings when regi…
Browse files Browse the repository at this point in the history
…stering in the user dictionary.
  • Loading branch information
nathancorvussolis committed Feb 20, 2021
1 parent 45fb790 commit 51f7385
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
67 changes: 63 additions & 4 deletions common/parseskkdic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ std::wstring ParseConcat(const std::wstring &s)
std::wregex re;
std::wsmatch res;
wchar_t u;
LPCWSTR bsrep = L"\uf05c";
LPCWSTR bsrep = L"\uF05C";

ret = s;

Expand Down Expand Up @@ -271,20 +271,22 @@ std::wstring MakeConcat(const std::wstring &s)

ret = s;

// "/" -> \057, ";" -> \073
// '/' or ';'
static const std::wregex respcch(L"[/;]");

if (std::regex_search(ret, respcch))
{
// "\"" -> "\\\"", "\\" -> "\\\\"
re.assign(L"([\\\"\\\\])");
// '"' -> "\"", '\' -> "\\"
re.assign(L"([\"\\\\])");
fmt.assign(L"\\$1");
ret = std::regex_replace(ret, re, fmt);

// '/' -> "\057"
re.assign(L"/");
fmt.assign(L"\\057");
ret = std::regex_replace(ret, re, fmt);

// ';' -> "\073"
re.assign(L";");
fmt.assign(L"\\073");
ret = std::regex_replace(ret, re, fmt);
Expand All @@ -294,3 +296,60 @@ std::wstring MakeConcat(const std::wstring &s)

return ret;
}

std::wstring EscapeGadgetString(const std::wstring &s)
{
std::wstring ret, fmt;
std::wregex re;
LPCWSTR bsrep = L"\uF05C";

ret = s;

//実行変換もどきの文字列パラメータをエスケープ
static const std::wregex regadget(L"^\\(.+\\)$");

if (std::regex_match(s, regadget))
{
std::wstring esc, str;
std::wstring tmp = s;
std::wsmatch m;

// "\\" -> '\uF05C'
re.assign(L"\\\\\\\\");
fmt.assign(bsrep);
tmp = std::regex_replace(tmp, re, fmt);

// <SPC>"..." ignoring escaped quotation
static const std::wregex restring(L" (\"\"|\".*?[^\\\\]\")");

while (std::regex_search(tmp, m, restring))
{
esc += m.prefix().str();
str = m.str();
tmp = m.suffix().str();

// '/' -> "\057"
re.assign(L"/");
fmt.assign(L"\\057");
str = std::regex_replace(str, re, fmt);

// ';' -> "\073"
re.assign(L";");
fmt.assign(L"\\073");
str = std::regex_replace(str, re, fmt);

esc += str;
}

esc += tmp;

// '\uF05C' -> "\\"
re.assign(bsrep);
fmt.assign(L"\\\\");
esc = std::regex_replace(esc, re, fmt);

ret = esc;
}

return ret;
}
1 change: 1 addition & 0 deletions common/parseskkdic.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ void ParseSKKDicCandiate(const std::wstring &s, SKKDICCANDIDATES &c);
void ParseSKKDicOkuriBlock(const std::wstring &s, SKKDICOKURIBLOCKS &o);
std::wstring ParseConcat(const std::wstring &s);
std::wstring MakeConcat(const std::wstring &s);
std::wstring EscapeGadgetString(const std::wstring &s);
4 changes: 3 additions & 1 deletion imcrvmgr/SearchUserDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ void AddUserDic(WCHAR command, const std::wstring &searchkey, const std::wstring
return;
}

candidate_esc = MakeConcat(candidate);
candidate_esc = EscapeGadgetString(candidate);
candidate_esc = MakeConcat(candidate_esc);

annotation_esc = MakeConcat(annotation);

EnterCriticalSection(&csUserData); // !
Expand Down

0 comments on commit 51f7385

Please sign in to comment.