diff --git a/libs/main/core/piconstchars.cpp b/libs/main/core/piconstchars.cpp index cab4905b..0e253350 100644 --- a/libs/main/core/piconstchars.cpp +++ b/libs/main/core/piconstchars.cpp @@ -17,6 +17,7 @@ along with this program. If not, see . */ #include "piconstchars.h" +#include "pistring.h" //! \addtogroup Core @@ -38,10 +39,108 @@ //! //! \~russian //! Это обертка вокруг \c const char * строки. %PIConstChars не скопирует -//! строку, а только хранит указатель и размер. +//! строку, а хранит только указатель и размер. //! //! Предоставляет API схожий с обычной строкой, с методами сравнения и информационными. //! //! Используется для более удобной работы с обычными C-строками. //! //! \} + + +PIConstChars PIConstChars::mid(const int start, const int len) const { + int s = start, l = len; + if (l == 0 || s >= (int)size() || isEmpty()) return PIConstChars(""); + if (s < 0) { + l += s; + s = 0; + } + if (l < 0) { + return PIConstChars(str + s, (int)size() - s); + } else { + if (l > (int)size() - s) + l = (int)size() - s; + return PIConstChars(str + s, l); + } + return PIConstChars(""); +} + + +PIConstChars PIConstChars::left(const int l) const { + if (l <= 0) return PIConstChars(""); + return mid(0, l); +} + + +PIConstChars PIConstChars::right(const int l) const { + if (l <= 0) return PIConstChars(""); + return mid((int)size() - l, l); +} + + +PIConstChars & PIConstChars::cutLeft(const int l) { + if (l <= 0) return *this; + if (l >= (int)size()) + *this = PIConstChars(""); + else { + str += l; + len -= l; + } + return *this; +} + + +PIConstChars & PIConstChars::cutRight(const int l) { + if (l <= 0) return *this; + if (l >= (int)size()) + *this = PIConstChars(""); + else { + len -= l; + } + return *this; +} + + +PIConstChars PIConstChars::takeLeft(const int len) { + PIConstChars ret(left(len)); + cutLeft(len); + return ret; +} + + +PIConstChars PIConstChars::takeRight(const int len) { + PIConstChars ret(right(len)); + cutRight(len); + return ret; +} + + +PIConstChars & PIConstChars::trim() { + if (isEmpty()) return *this; + int st = -1, fn = 0; + for (int i = 0; i < (int)len; ++i) { + if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) { + st = i; + break; + } + } + if (st < 0) { + *this = PIConstChars(""); + return *this; + } + for (int i = (int)len - 1; i >= 0; --i) { + if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) { + fn = i; + break; + } + } + if (fn < (int)len - 1) cutRight((int)len - fn - 1); + if (st > 0) cutLeft(st); + return *this; +} + + +PIString PIConstChars::toString() const { + if (isEmpty()) return PIString(); + return PIString::fromAscii(str, len); +} diff --git a/libs/main/core/piconstchars.h b/libs/main/core/piconstchars.h index af99701a..f2805c35 100644 --- a/libs/main/core/piconstchars.h +++ b/libs/main/core/piconstchars.h @@ -62,6 +62,10 @@ public: //! \~russian Доступ на чтение к символу по индексу `index`. inline const char & operator [](size_t index) const {return str[index];} + //! \~english Read-only access to character by `index`. + //! \~russian Доступ на чтение к символу по индексу `index`. + inline char at(size_t index) const {return str[index];} + //! \~english Returns \c char * string pointer. //! \~russian Возвращает \c char * указатель строки. inline const char * data() const {return str;} @@ -90,6 +94,55 @@ public: //! \~russian Возвращает длину строки в символах. inline ssize_t size_s() const {return len;} + //! \~english Returns part of string from character at index "start" and maximum length "len". + //! \~russian Возвращает подстроку от символа "start" и максимальной длиной "len". + //! \~\sa \a left(), \a right() + PIConstChars mid(const int start, const int len = -1) const; + + //! \~english Returns part of string from start and maximum length "len". + //! \~russian Возвращает подстроку от начала и максимальной длиной "len". + //! \~\sa \a mid(), \a right() + PIConstChars left(const int len) const; + + //! \~english Returns part of string at end and maximum length "len". + //! \~russian Возвращает подстроку максимальной длиной "len" и до конца. + //! \~\sa \a mid(), \a left() + PIConstChars right(const int len) const; + + //! \~english Remove part of string from start and maximum length "len" and return this string. + //! \~russian Удаляет часть строки от начала и максимальной длины "len", возвращает эту строку. + //! \~\sa \a cutRight() + PIConstChars & cutLeft(const int len); + + //! \~english Remove part of string at end and maximum length "len" and return this string. + //! \~russian Удаляет часть строки максимальной длины "len" от конца, возвращает эту строку. + //! \~\sa \a cutLeft() + PIConstChars & cutRight(const int len); + + //! \~english Take a part from the begin of string with maximum length "len" and return it. + //! \~russian Извлекает часть строки от начала максимальной длины "len" и возвращает её. + //! \~\sa \a takeRight() + PIConstChars takeLeft(const int len); + + //! \~english Take a part from the end of string with maximum length "len" and return it. + //! \~russian Извлекает часть строки с конца максимальной длины "len" и возвращает её. + //! \~\sa \a takeLeft() + PIConstChars takeRight(const int len); + + //! \~english Remove spaces at the start and at the end of string and return this string. + //! \~russian Удаляет пробельные символы с начала и конца строки и возвращает эту строку. + //! \~\sa \a trimmed() + PIConstChars & trim(); + + //! \~english Returns copy of this string without spaces at the start and at the end. + //! \~russian Возвращает копию этой строки без пробельных символов с начала и конца. + //! \~\sa \a trim() + PIConstChars trimmed() const {return PIConstChars(*this).trim();} + + //! \~english Returns as PIString. + //! \~russian Возвращает как PIString. + PIString toString() const; + //! \~english Assign operator. //! \~russian Оператор присваивания. inline PIConstChars & operator =(const PIConstChars & s) {