diff --git a/libs/crypt/piauth.cpp b/libs/crypt/piauth.cpp index f5643f24..8ce5f329 100644 --- a/libs/crypt/piauth.cpp +++ b/libs/crypt/piauth.cpp @@ -238,7 +238,7 @@ PIAuth::State PIAuth::receive(PIByteArray & ba) { rinfo = crypt.decrypt(rinfo, secret_key, &ok); if (!ok) return disconnect(ba, "Error while exchange keys"); state = Connected; - connected(rinfo); + connected(PIString::fromUTF8(rinfo)); ba << (int)state << crypt.generateRandomBuff(randomi() % PIAUTH_NOISE_MAX_SIZE); return state; } diff --git a/libs/main/core/picli.h b/libs/main/core/picli.h index 5a71db20..19325ab1 100644 --- a/libs/main/core/picli.h +++ b/libs/main/core/picli.h @@ -57,7 +57,7 @@ public: //! \~english Add argument with name "name", short key = "shortKey" and full key = name. //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени. void addArgument(const PIString & name, const char * shortKey, bool value = false) { - _args << Argument(name, PIChar(shortKey), name, value); + _args << Argument(name, PIChar::fromUTF8(shortKey), name, value); needParse = true; } @@ -71,7 +71,7 @@ public: //! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey". //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey". void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false) { - _args << Argument(name, PIChar(shortKey), fullKey, value); + _args << Argument(name, PIChar::fromUTF8(shortKey), fullKey, value); needParse = true; } diff --git a/libs/main/text/pichar.cpp b/libs/main/text/pichar.cpp index 64eef9de..ac75b930 100644 --- a/libs/main/text/pichar.cpp +++ b/libs/main/text/pichar.cpp @@ -90,7 +90,7 @@ int charCompare(const PIChar & f, const PIChar & s) { if (f.isAscii() && s.isAscii()) return strncmp(f.toCharPtr(), s.toCharPtr(), 1); return #ifdef PIP_ICU - u_strCompare((const UChar *)f.toWCharPtr(), 1, (const UChar *)s.toWCharPtr(), 1, 0); + u_strCompare((const UChar *)f.toWCharPtr(), 1, (const UChar *)s.toWCharPtr(), 1, 0); #else # ifdef WINDOWS CompareStringW(LOCALE_USER_DEFAULT, 0, (PCNZWCH)f.toWCharPtr(), 1, (PCNZWCH)s.toWCharPtr(), 1) - 2; @@ -111,11 +111,6 @@ bool winIsCharType(const ushort * ch, int type) { } -PIChar::PIChar(const char * c, int * bytes) { - ch = charFromCodepage(c, 4, __syslocname__, bytes); -} - - PIChar PIChar::fromConsole(char c) { PIChar ret; ret.ch = charFromCodepage(&c, 1, __sysoemname__); @@ -130,10 +125,20 @@ PIChar PIChar::fromSystem(char c) { } +PIChar PIChar::fromSystem(const char * c) { + PIChar ret; + int l = 0; + while (c[l] != '\0' && l < 4) + ++l; + ret.ch = charFromCodepage(c, l, __syslocname__); + return ret; +} + + PIChar PIChar::fromUTF8(const char * c) { PIChar ret; int l = 0; - while (c[l] != '\0') + while (c[l] != '\0' && l < 4) ++l; ret.ch = charFromCodepage(c, l, __utf8name__); return ret; diff --git a/libs/main/text/pichar.h b/libs/main/text/pichar.h index 2d39742a..0fe96237 100644 --- a/libs/main/text/pichar.h +++ b/libs/main/text/pichar.h @@ -57,9 +57,9 @@ public: //! \~russian Создает 2-байтный символ из `wchar_t` PIChar(wchar_t c) { ch = c; } - //! \~english Contructs symbol from system locale and no more than 4 bytes of string - //! \~russian Создает символ из системной локали не более 4 байт длины - explicit PIChar(const char * c, int * bytes = 0); + //! \~english Contructs 2-bytes symbol from `char16_t` + //! \~russian Создает 2-байтный символ из `char16_t` + PIChar(char16_t c) { ch = c; } //! \~english Copy operator //! \~russian Оператор присваивания @@ -177,6 +177,10 @@ public: //! \~russian Возвращает символ из системной кодировки static PIChar fromSystem(char c); + //! \~english Returns symbol from system codepage + //! \~russian Возвращает символ из системной кодировки + static PIChar fromSystem(const char * c); + //! \~english Returns symbol from UTF8 codepage //! \~russian Возвращает символ из UTF8 кодировки static PIChar fromUTF8(const char * c); @@ -225,43 +229,6 @@ inline bool operator<=(const char v, const PIChar & c) { return (PIChar(v) <= c); } - -//! \relatesalso PIChar -//! \~english Compare operator -//! \~russian Оператор сравнения -inline bool operator==(const char * v, const PIChar & c) { - return (PIChar(v) == c); -} - -//! \relatesalso PIChar -//! \~english Compare operator -//! \~russian Оператор сравнения -inline bool operator>(const char * v, const PIChar & c) { - return (PIChar(v) > c); -} - -//! \relatesalso PIChar -//! \~english Compare operator -//! \~russian Оператор сравнения -inline bool operator<(const char * v, const PIChar & c) { - return (PIChar(v) < c); -} - -//! \relatesalso PIChar -//! \~english Compare operator -//! \~russian Оператор сравнения -inline bool operator>=(const char * v, const PIChar & c) { - return (PIChar(v) >= c); -} - -//! \relatesalso PIChar -//! \~english Compare operator -//! \~russian Оператор сравнения -inline bool operator<=(const char * v, const PIChar & c) { - return (PIChar(v) <= c); -} - - //! \relatesalso PIChar //! \~english Compare operator //! \~russian Оператор сравнения diff --git a/libs/main/text/pistring.cpp b/libs/main/text/pistring.cpp index 715c50df..09fc76eb 100644 --- a/libs/main/text/pistring.cpp +++ b/libs/main/text/pistring.cpp @@ -404,6 +404,16 @@ PIString PIString::fromAscii(const char * s, int len) { } +PIString PIString::fromAscii(const PIByteArray & ascii) { + PIString ret; + ret.resize(ascii.size()); + for (int l = 0; l < ret.size_s(); ++l) { + ret[l] = PIChar(ascii[l]); + } + return ret; +} + + PIString PIString::fromCodepage(const char * s, const char * c) { PIString ret; if (s[0] > '\0') @@ -572,12 +582,6 @@ PIString & PIString::operator+=(const char * str) { } -PIString & PIString::operator+=(const PIByteArray & ba) { - appendFromChars((const char *)ba.data(), ba.size_s(), __utf8name__); - return *this; -} - - PIString::~PIString() { deleteData(); } @@ -593,6 +597,16 @@ PIString & PIString::operator+=(const wchar_t * str) { } +PIString & PIString::operator+=(const char16_t * str) { + if (!str) return *this; + int i = -1; + while (str[++i]) { + d.push_back(PIChar(str[i])); + } + return *this; +} + + PIString & PIString::operator+=(const PIString & str) { d.append(str.d); return *this; @@ -1161,12 +1175,24 @@ int PIString::entries(const PIChar c) const { } +bool PIString::startsWith(const PIChar c) const { + if (isEmpty()) return false; + return front() == c; +} + + bool PIString::startsWith(const PIString & str) const { if (size() < str.size()) return false; return str == left(str.size()); } +bool PIString::endsWith(const PIChar c) const { + if (isEmpty()) return false; + return back() == c; +} + + bool PIString::endsWith(const PIString & str) const { if (size() < str.size()) return false; return str == right(str.size()); diff --git a/libs/main/text/pistring.h b/libs/main/text/pistring.h index e81fa4d1..c9494b1a 100644 --- a/libs/main/text/pistring.h +++ b/libs/main/text/pistring.h @@ -79,7 +79,7 @@ public: } PIString & operator+=(const char * str); PIString & operator+=(const wchar_t * str); - PIString & operator+=(const PIByteArray & ba); + PIString & operator+=(const char16_t * str); PIString & operator+=(const PIString & str); PIString & operator+=(const PIConstChars & str); @@ -113,11 +113,11 @@ public: //! \~english Contructs string with single character "c". //! \~russian Создает строку из одного символа "c". - explicit PIString(const PIChar c) { d.push_back(c); } + PIString(const PIChar c) { d.push_back(c); } //! \~english Contructs string with single character "c". //! \~russian Создает строку из одного символа "c". - explicit PIString(const char c) { d.push_back(PIChar(c)); } + PIString(const char c) { d.push_back(PIChar(c)); } //! \~english Contructs string from C-string "str" (system codepage). //! \~russian Создает строку из C-строки "str" (кодировка системы). @@ -143,9 +143,17 @@ public: //! \endcode PIString(const wchar_t * str) { *this += str; } - //! \~english Contructs string from byte array "ba" (as UTF-8). - //! \~russian Создает строку из байтового массива "ba" (как UTF-8). - explicit PIString(const PIByteArray & ba) { *this += ba; } + //! \~english Contructs string from \c char16_t C-string "str". + //! \~russian Создает строку из \c char16_t C-строки "str". + //! \~\details + //! \~english + //! "str" should be null-terminated + //! \~russian + //! "str" должна заканчиваться нулевым \c char16_t + //! \~\code + //! PIString s(u"string"); + //! \endcode + PIString(const char16_t * str) { *this += str; } //! \~english Contructs string from "len" characters of buffer "str". //! \~russian Создает строку из "len" символов массива "str". @@ -181,7 +189,7 @@ public: d.push_back(c); } - explicit PIString(const PIConstChars & c) { *this += c; } + PIString(const PIConstChars & c) { *this += c; } ~PIString(); @@ -217,6 +225,14 @@ public: return *this; } + //! \~english Assign operator. + //! \~russian Оператор присваивания. + PIString & operator=(const char16_t * o) { + d.clear(); + *this += o; + return *this; + } + //! \~english Compare operator. //! \~russian Оператор сравнения. bool operator==(const PIString & str) const; @@ -452,6 +468,13 @@ public: return *this; } + //! \~english Insert string "str" at the begin of string. + //! \~russian Вставляет "str" в начало строки. + PIString & push_front(const char16_t * str) { + insert(0, str); + return *this; + } + //! \~english Insert string "str" at the begin of string. //! \~russian Вставляет "str" в начало строки. PIString & push_front(const PIString & str) { @@ -480,6 +503,13 @@ public: return *this; } + //! \~english Insert string "str" at the end of string. + //! \~russian Вставляет "str" в конец строки. + PIString & append(const char16_t * str) { + *this += str; + return *this; + } + //! \~english Insert string "str" at the end of string. //! \~russian Вставляет "str" в конец строки. PIString & append(const PIString & str) { @@ -787,6 +817,16 @@ public: //! \endcode PIString & insert(const int index, const char * c) { return insert(index, PIString(c)); } + //! \~english Insert string "str" after index "index" and return this string. + //! \~russian Вставляет строку "str" после позиции "index" и возвращает эту строку. + //! \~\details + //! \~\code + //! PIString s("stg"); + //! s.insert(2, u"rin"); + //! piCout << s; // s = "string" + //! \endcode + PIString & insert(const int index, const char16_t * c) { return insert(index, PIString(c)); } + //! \~english Enlarge string to length "len" by addition characters "c" at the end, and return this string. //! \~russian Увеличивает длину строки до "len" добавлением символов "c" в конец и возвращает эту строку. //! \~\details @@ -1184,10 +1224,26 @@ public: //! \endcode int entries(const PIChar c) const; + //! \~english Returns if string starts with "c". + //! \~russian Возвращает начинается ли строка с "c". + bool startsWith(const char c) const { return startsWith(PIChar(c)); } + + //! \~english Returns if string starts with "c". + //! \~russian Возвращает начинается ли строка с "c". + bool startsWith(const PIChar c) const; + //! \~english Returns if string starts with "str". //! \~russian Возвращает начинается ли строка со "str". bool startsWith(const PIString & str) const; + //! \~english Returns if string ends with "c". + //! \~russian Возвращает оканчивается ли строка на "c". + bool endsWith(const char c) const { return endsWith(PIChar(c)); } + + //! \~english Returns if string ends with "c". + //! \~russian Возвращает оканчивается ли строка на "c". + bool endsWith(const PIChar c) const; + //! \~english Returns if string ends with "str". //! \~russian Возвращает оканчивается ли строка на "str". bool endsWith(const PIString & str) const; @@ -1705,6 +1761,10 @@ public: //! \~russian Возвращает строку созданную из "len" символов ASCII. static PIString fromAscii(const char * s, int len); + //! \~english Returns string constructed from ASCII. + //! \~russian Возвращает строку созданную из ASCII. + static PIString fromAscii(const PIByteArray & ascii); + //! \~english Returns string constructed from "cp" codepage. //! \~russian Возвращает строку созданную из кодировки "cp". static PIString fromCodepage(const char * s, const char * cp); diff --git a/libs/main/text/pistring_std.h b/libs/main/text/pistring_std.h index 4b760eba..873fd227 100644 --- a/libs/main/text/pistring_std.h +++ b/libs/main/text/pistring_std.h @@ -30,6 +30,7 @@ #ifdef QNX typedef std::basic_string wstring; #endif +#include "piliterals.h" #include "pistringlist.h" @@ -117,12 +118,12 @@ inline std::istream & operator>>(std::istream & s, PIString & v) { //! \relatesalso PIStringList \brief Output operator to std::ostream (cout) inline std::ostream & operator<<(std::ostream & s, const PIStringList & v) { - s << PIChar("{"); + s << PIChar('{'); for (uint i = 0; i < v.size(); ++i) { - s << PIChar("\"") << v[i] << PIChar("\""); - if (i < v.size() - 1) s << PIStringAscii(", "); + s << PIChar('"') << v[i] << PIChar('"'); + if (i < v.size() - 1) s << ", "_a; } - s << PIChar("}"); + s << PIChar('}'); return s; } diff --git a/libs/main/types/pibytearray.cpp b/libs/main/types/pibytearray.cpp index d88d1459..6207c9ea 100644 --- a/libs/main/types/pibytearray.cpp +++ b/libs/main/types/pibytearray.cpp @@ -348,7 +348,7 @@ PIString PIByteArray::toHex() const { else hexData[i * 2 + 1] = (j + 'a' - 10); } - return PIString(hex); + return PIString::fromAscii(hex); } diff --git a/libs/opencl/piopencl.cpp b/libs/opencl/piopencl.cpp index 2879bfd4..8b368d87 100644 --- a/libs/opencl/piopencl.cpp +++ b/libs/opencl/piopencl.cpp @@ -139,7 +139,7 @@ void PIOpenCL::Initializer::init() { PIOpenCL::Context::Context() { - PRIVATE->complex_src = PIResources::get("3rd/clcomplex.h") + "\n"; + PRIVATE->complex_src = PIString::fromUTF8(PIResources::get("3rd/clcomplex.h")) + "\n"; zero(); } diff --git a/main_picloud_test.cpp b/main_picloud_test.cpp index b1d3108c..35dedfe0 100644 --- a/main_picloud_test.cpp +++ b/main_picloud_test.cpp @@ -31,7 +31,7 @@ int main(int argc, char * argv[]) { CONNECTL(&c, threadedReadEvent, ([&](const uchar * readed, ssize_t size) { PIByteArray ba(readed, size); if (size < 1024) { - PIString str = PIString(ba); + PIString str = PIString::fromUTF8(ba); piCout << "[Client] data:" << str; if (str == "ping_S") c.write(PIString("pong_S").toByteArray()); } else @@ -44,7 +44,7 @@ int main(int argc, char * argv[]) { clients->append(cl); CONNECTL(cl, threadedReadEvent, ([cl, &rnd](const uchar * readed, ssize_t size) { PIByteArray ba(readed, size); - PIString str = PIString(ba); + PIString str = PIString::fromUTF8(ba); piCout << "[Server] data from" << cl << ":" << str; if (str == "ping") { cl->write(PIString("pong").toByteArray()); diff --git a/utils/crypt_tool/main.cpp b/utils/crypt_tool/main.cpp index dd9fcebb..8d4ec3b7 100644 --- a/utils/crypt_tool/main.cpp +++ b/utils/crypt_tool/main.cpp @@ -30,7 +30,7 @@ void usage() { piCout << Cyan << "Version" << Bold << PIPVersion() << NewLine; piCout << Green << Bold << "Usage:" << Default << "\"picrypt [-thr] [-g ] [-o ] [{[-c ] [-d ]} {[-p ] [-s ] [-k ] [-x " - "]}]\"" + "]}]\"" << NewLine; piCout << Green << Bold << "Details:"; piCout << "-h --help " << Green << "- display this message and exit"; @@ -96,7 +96,7 @@ int main(int argc, char * argv[]) { } if (cli.hasArgument("pass")) secret = crypt.hash(cli.argumentValue("pass")); if (cli.hasArgument("secret")) secret = PIByteArray::fromBase64(cli.argumentValue("secret").toByteArray()); - if (cli.hasArgument("hex")) secret = PIByteArray::fromHex(cli.argumentValue("hex").toByteArray()); + if (cli.hasArgument("hex")) secret = PIByteArray::fromHex(PIString::fromAscii(cli.argumentValue("hex").toByteArray())); if (secret.size() == crypt.sizeKey()) { PIFile inf; PIString in_file;