Skip to content

Commit

Permalink
revert separation line
Browse files Browse the repository at this point in the history
  • Loading branch information
Artikash committed Dec 15, 2020
1 parent 457aed9 commit 95b145b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion extensions/extrawindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ExtraWindow : public PrettyWindow
void AddSentence(QString sentence)
{
if (sentence.size() > maxSentenceSize) sentence = SENTENCE_TOO_BIG;
if (!showOriginal) sentence = sentence.section("\n----\n", sentence.count("\n----\n") / 2 + 1);
if (!showOriginal && sentence.count(u8"\x200b \n")) sentence = sentence.split(u8"\x200b \n")[1];
sanitize(sentence);
sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end()));
sentenceHistory.push_back(sentence);
Expand Down
61 changes: 31 additions & 30 deletions extensions/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,35 @@ std::string Escape(const std::string& text);

namespace JSON
{
inline std::wstring UTF(int charCode)
template <typename C>
std::basic_string<C> Escape(std::basic_string<C> text)
{
return { (wchar_t)charCode };
int oldSize = text.size();
text.resize(text.size() + std::count_if(text.begin(), text.end(), [](auto ch) { return ch == '\n' || ch == '\r' || ch == '\t' || ch == '\\' || ch == '"'; }));
auto out = text.rbegin();
for (int i = oldSize - 1; i >= 0; --i)
{
if (text[i] == '\n') *out++ = 'n';
else if (text[i] == '\t') *out++ = 't';
else if (text[i] == '\r') *out++ = 'r';
else if (text[i] == '\\' || text[i] == '"') *out++ = text[i];
else
{
*out++ = text[i];
continue;
}
*out++ = '\\';
}
text.erase(std::remove_if(text.begin(), text.end(), [](uint64_t ch) { return ch < 0x20 || ch == 0x7f; }), text.end());
return text;
}

template <typename C> struct UTF {};
template <> struct UTF<wchar_t>
{
inline static std::wstring FromCodepoint(int codepoint) { return { (wchar_t)codepoint }; } // TODO: surrogate pairs
};

template <typename C>
std::pair<std::basic_string<C>, int> Unescape(std::basic_string_view<C> text)
{
Expand All @@ -53,7 +77,7 @@ namespace JSON
if (ch == 'u' && isxdigit(text[i + 2]) && isxdigit(text[i + 3]) && isxdigit(text[i + 4]) && isxdigit(text[i + 5]))
{
char charCode[] = { text[i + 2], text[i + 3], text[i + 4], text[i + 5], 0 };
unescaped += UTF(strtol(charCode, nullptr, 16));
unescaped += UTF<C>::FromCodepoint(strtol(charCode, nullptr, 16));
i += 5;
continue;
}
Expand All @@ -70,29 +94,6 @@ namespace JSON
return { unescaped, i };
}

template <typename C>
std::basic_string<C> Escape(std::basic_string<C> text)
{
int oldSize = text.size();
text.resize(text.size() + std::count_if(text.begin(), text.end(), [](auto ch) { return ch == '\n' || ch == '\r' || ch == '\t' || ch == '\\' || ch == '"'; }));
auto out = text.rbegin();
for (int i = oldSize - 1; i >= 0; --i)
{
if (text[i] == '\n') *out++ = 'n';
else if (text[i] == '\t') *out++ = 't';
else if (text[i] == '\r') *out++ = 'r';
else if (text[i] == '\\' || text[i] == '"') *out++ = text[i];
else
{
*out++ = text[i];
continue;
}
*out++ = '\\';
}
text.erase(std::remove_if(text.begin(), text.end(), [](uint64_t ch) { return ch < 0x20 || ch == 0x7f; }), text.end());
return text;
}

template <typename C>
struct Value : private std::variant<std::monostate, std::nullopt_t, bool, double, std::basic_string<C>, std::vector<Value<C>>, std::unordered_map<std::basic_string<C>, Value<C>>>
{
Expand All @@ -119,10 +120,10 @@ namespace JSON
}
};

template <typename C>
template <typename C, int maxDepth = 25>
Value<C> Parse(std::basic_string_view<C> text, int64_t& i, int depth)
{
if (depth > 25) return {};
if (depth > maxDepth) return {};
C ch;
auto SkipWhitespace = [&]
{
Expand Down Expand Up @@ -169,7 +170,7 @@ namespace JSON
i += 1;
if (SkipWhitespace()) return {};
if (ch == ']') return i += 1, Value<C>(array);
if (!array.emplace_back(Parse(text, i, depth + 1))) return {};
if (!array.emplace_back(Parse<C, maxDepth>(text, i, depth + 1))) return {};
if (SkipWhitespace()) return {};
if (ch == ']') return i += 1, Value<C>(array);
if (ch != ',') return {};
Expand All @@ -188,7 +189,7 @@ namespace JSON
auto key = ExtractString();
if (SkipWhitespace() || ch != ':') return {};
i += 1;
if (!(object[std::move(key)] = Parse(text, i, depth + 1))) return {};
if (!(object[std::move(key)] = Parse<C, maxDepth>(text, i, depth + 1))) return {};
if (SkipWhitespace()) return {};
if (ch == '}') return i += 1, Value<C>(object);
if (ch != ',') return {};
Expand Down
2 changes: 1 addition & 1 deletion extensions/translatewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
if (cache && translationCache->size() > savedSize + 50) SaveCache();

for (int i = 0; i < translation.size(); ++i) if (translation[i] == '\r' && translation[i + 1] == '\n') translation[i] = 0x200b; // for some reason \r appears as newline - no need to double
if (!translation.empty()) (sentence += L"\n----\n") += translation;
if (!translation.empty()) (sentence += L"\x200b \n") += translation;
return true;
}

Expand Down

0 comments on commit 95b145b

Please sign in to comment.