4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo
9
#include "simodo/inout/convert/functions.h"
11
namespace simodo::inout
14
void replaceAll(std::u16string &str, const std::u16string &from, const std::u16string &to)
20
while((start_pos = str.find(from, start_pos)) != std::string::npos)
22
str.replace(start_pos, from.length(), to);
23
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
27
std::u16string replace(const std::u16string &str, const std::u16string &from, const std::u16string &to)
33
res.reserve(str.size() * 2);
37
while((pos = str.find(from, start_pos)) != std::string::npos)
39
size_t target_pos = res.size();
40
res.resize(res.size()+pos-start_pos+to.size());
41
str.copy(res.data()+target_pos,pos-start_pos,start_pos);
42
to.copy(res.data()+target_pos+pos-start_pos,to.size());
43
start_pos = pos + from.length();
45
size_t target_pos = res.size();
46
res.resize(res.size()+str.size()-start_pos);
47
str.copy(res.data()+target_pos,str.size()-start_pos,start_pos);
52
std::u16string encodeSpecialChars(const std::u16string & text)
54
return replace(replace(text, u"\n", u"
"), u"\"", u""");
57
std::u16string decodeSpecialChars(const std::u16string & text)
59
return replace(replace(text, u"
", u"\n"), u""", u"\"");
62
void replaceAll(std::string &str, const std::string &from, const std::string &to)
68
while((start_pos = str.find(from, start_pos)) != std::string::npos)
70
str.replace(start_pos, from.length(), to);
71
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
75
std::string encodeSpecialChars(const std::string & text)
77
std::string res = text;
78
replaceAll(res, "\n", "
");
79
replaceAll(res, "\"", """);
83
std::string decodeSpecialChars(const std::string & text)
85
std::string res = text;
86
replaceAll(res, """, "\"");
87
replaceAll(res, "
", "\n");