diff --git a/src/common/File.cpp b/src/common/File.cpp index 2f1dd84..9c2bb23 100644 --- a/src/common/File.cpp +++ b/src/common/File.cpp @@ -163,7 +163,7 @@ bool Jupiter::File::load(FILE *file) { new_line_r: m_data->lines.emplace_back(buffer); - buffer.erase(); + buffer.clear(); // check for optional trailing \n @@ -190,7 +190,7 @@ bool Jupiter::File::load(FILE *file) { new_line_n: m_data->lines.emplace_back(buffer); - buffer.erase(); + buffer.clear(); // check for optional trailing \r diff --git a/src/common/INIConfig.cpp b/src/common/INIConfig.cpp index 161242d..f0532f3 100644 --- a/src/common/INIConfig.cpp +++ b/src/common/INIConfig.cpp @@ -235,7 +235,7 @@ bool Jupiter::INIConfig::read_internal(const char *in_filename) { if (itr == end) { // No data remains to be parsed in buffer; erase buffer and break - buffer.erase(); + buffer.clear(); break; } diff --git a/src/common/IRC_Client.cpp b/src/common/IRC_Client.cpp index 225a2c8..79eec7c 100644 --- a/src/common/IRC_Client.cpp +++ b/src/common/IRC_Client.cpp @@ -188,7 +188,7 @@ void Jupiter::IRC::Client::setPrimaryConfigSection(Jupiter::Config *in_primary_s if (m_primary_section != nullptr) m_primary_section_name = m_primary_section->getName(); else - m_primary_section_name.erase(); + m_primary_section_name.clear(); } void Jupiter::IRC::Client::setSecondaryConfigSection(Jupiter::Config *in_secondary_section) { diff --git a/src/include/Jupiter/Readable_String.h b/src/include/Jupiter/Readable_String.h index 4f9e959..297276a 100644 --- a/src/include/Jupiter/Readable_String.h +++ b/src/include/Jupiter/Readable_String.h @@ -29,6 +29,7 @@ #include // FILE #include // std::basic_string type #include // std::endl +#include #include "InvalidIndex.h" #include "DataBuffer.h" #include "Functions.h" @@ -159,4 +160,30 @@ namespace Jupiter { } } +inline std::string string_printf(const char* format, ...) { + std::string result; + va_list args; + va_start(args, format); + + va_list args_copy; + va_copy(args_copy, args); + int min_length = std::vsnprintf(nullptr, 0, format, args_copy); + va_end(args_copy); + + if (min_length > 0) { + result.resize(min_length); + min_length = vsnprintf(result.data(), result.size() + 1, format, args); + if (min_length <= 0) { + result.clear(); + } + + if (result.size() != min_length) { + throw std::runtime_error("result.size() != min_length"); + } + } + + va_end(args); + return result; +} + #endif // _READABLE_STRING_H_HEADER \ No newline at end of file diff --git a/src/include/Jupiter/Shift_String.h b/src/include/Jupiter/Shift_String.h index 8e118ee..43e032b 100644 --- a/src/include/Jupiter/Shift_String.h +++ b/src/include/Jupiter/Shift_String.h @@ -80,16 +80,7 @@ namespace Jupiter /** * @brief Removes all elements from the string. */ - virtual void erase(); - - /** - * @brief Captures an input string of elements and uses it as the internal string. - * Note: Do NOT externally delete[] or free() 'in'. - * - * @param in Pointer to the start of the string to capture - * @param in_length Number of elements in the string - */ - virtual void capture(T *in, size_t in_length); + void clear() override; /** * @brief Sets the internal buffer to be at least large enough to old a specified number of elements. diff --git a/src/include/Jupiter/Shift_String_Imp.h b/src/include/Jupiter/Shift_String_Imp.h index 0f89f71..2d4dc49 100644 --- a/src/include/Jupiter/Shift_String_Imp.h +++ b/src/include/Jupiter/Shift_String_Imp.h @@ -73,23 +73,12 @@ template void Jupiter::Shift_String_Type::remove(size_t index, si Jupiter::String_Type::remove(index, len); } -template void Jupiter::Shift_String_Type::erase() +template void Jupiter::Shift_String_Type::clear() { - Jupiter::String_Type::erase(); + Jupiter::String_Type::clear(); Jupiter::String_Type::str = Jupiter::Shift_String_Type::base; } -// capture - -template void Jupiter::Shift_String_Type::capture(T *in, size_t in_size) -{ - delete[] Jupiter::Shift_String_Type::base; - - Jupiter::Shift_String_Type::base = in; - Jupiter::String_Type::str = Jupiter::Shift_String_Type::base; - Jupiter::String_Type::length = in_size; -} - template bool Jupiter::Shift_String_Type::setBufferSize(size_t len) { if (len > Jupiter::String_Type::length) diff --git a/src/include/Jupiter/String_Type.h b/src/include/Jupiter/String_Type.h index f3de50f..e7339a7 100644 --- a/src/include/Jupiter/String_Type.h +++ b/src/include/Jupiter/String_Type.h @@ -121,22 +121,7 @@ namespace Jupiter /** * @brief Removes all elements from the string. */ - virtual void erase(); - - /** - * @brief Processes escape sequences in a string. - * Source reference: http://en.cppreference.com/w/cpp/language/escape - */ - virtual void processEscapeSequences(); - - /** - * @brief Captures an input string of elements and uses it as the internal string. - * Note: Do NOT externally delete[] or free() 'in'. - * - * @param in Pointer to the start of the string to capture - * @param in_length Number of elements in the string - */ - virtual void capture(T *in, size_t in_length) = 0; + virtual void clear(); /** * @brief Sets the value of an element at the specified index. @@ -222,8 +207,8 @@ namespace Jupiter * @param pos Position to start copying from. * @return Partial copy of the input string. */ - template class R> static R substring(const Jupiter::Readable_String &in, size_t pos); - template class R> static R substring(const T *in, size_t pos); + template class R> static R substring(const Jupiter::Readable_String &in, size_t pos); // REPLACE + template class R> static R substring(const T *in, size_t pos); // REPLACE /** * @brief Copies a part of an input string and returns it in an output type. @@ -235,26 +220,8 @@ namespace Jupiter * @param len Number of elements to copy. * @return Partial copy of the input string. */ - template class R> static R substring(const Jupiter::Readable_String &in, size_t pos, size_t len); - template class R> static R substring(const T *in, size_t pos, size_t len); - - /** - * @brief Sets the internal buffer to be at least large enough to old a specified number of elements. - * Note: This does nothing if len is less than the string's current length. - * - * @param len Minimum number of elements the string buffer must be able to hold. - * @return True if a new buffer was allocated, false otherwise. - */ - virtual bool setBufferSize(size_t len) = 0; - - /** - * @brief Empties the string, and sets the internal buffer to be at least large enough to old a specified number of elements. - * Note: This does nothing if len is less than the string's current length. - * - * @param len Minimum number of elements the string buffer must be able to hold. - * @return True if a new buffer was allocated, false otherwise. - */ - virtual bool setBufferSizeNoCopy(size_t len) = 0; + template class R> static R substring(const Jupiter::Readable_String &in, size_t pos, size_t len); // REPLACE + template class R> static R substring(const T *in, size_t pos, size_t len); // REPLACE /** Mutative operators */ inline String_Type &operator+=(const String_Type &right) { this->concat(right); return *this; }; @@ -330,9 +297,27 @@ namespace Jupiter inline bool operator>=(const std::basic_string &right)const{ return !operator<(right); } inline bool operator>=(const T right)const{ return !operator<(right); } - protected: + protected: // Things which cannot be removed right now T *str{}; /** Pointer for the underlying string of elements */ size_t length{}; /** Number of representable elements in the string */ + + /** + * @brief Sets the internal buffer to be at least large enough to old a specified number of elements. + * Note: This does nothing if len is less than the string's current length. + * + * @param len Minimum number of elements the string buffer must be able to hold. + * @return True if a new buffer was allocated, false otherwise. + */ + virtual bool setBufferSize(size_t len) = 0; + + /** + * @brief Empties the string, and sets the internal buffer to be at least large enough to old a specified number of elements. + * Note: This does nothing if len is less than the string's current length. + * + * @param len Minimum number of elements the string buffer must be able to hold. + * @return True if a new buffer was allocated, false otherwise. + */ + virtual bool setBufferSizeNoCopy(size_t len) = 0; }; namespace literals { diff --git a/src/include/Jupiter/String_Type_Imp.h b/src/include/Jupiter/String_Type_Imp.h index d7de48a..16447a5 100644 --- a/src/include/Jupiter/String_Type_Imp.h +++ b/src/include/Jupiter/String_Type_Imp.h @@ -155,175 +155,11 @@ template void Jupiter::String_Type::remove(size_t index, size_t l // erase -template void Jupiter::String_Type::erase() +template void Jupiter::String_Type::clear() { Jupiter::String_Type::length = 0; } -// processEscapeSequences - -template void Jupiter::String_Type::processEscapeSequences() -{ -} - -template<> inline void Jupiter::String_Type::processEscapeSequences() -{ - if (Jupiter::String_Type::length == 0) - return; - - size_t index = 0; - while (index + 1 != Jupiter::String_Type::length) - { - if (operator[](index) == '\\') - { - switch (operator[](++index)) - { - case '0': - case '1': - case '2': - case '3': - if (index + 1 != Jupiter::String_Type::length && Jupiter_isOctal(operator[](index + 1))) - { - if (index + 2 != Jupiter::String_Type::length && Jupiter_isOctal(operator[](index + 2))) - this->replace(index - 1, 4, static_cast(Jupiter_getOctal(operator[](index))) << 6 | static_cast(Jupiter_getOctal(operator[](index + 1))) << 3 | static_cast(Jupiter_getOctal(operator[](index + 2)))); - else - this->replace(index - 1, 3, static_cast(Jupiter_getOctal(operator[](index))) << 3 | static_cast(Jupiter_getOctal(operator[](index + 1)))); - } - else - this->replace(index - 1, 2, static_cast(Jupiter_getOctal(operator[](index)))); - break; - case '4': - case '5': - case '6': - case '7': - if (index + 1 != Jupiter::String_Type::length && Jupiter_isOctal(operator[](index + 1))) - this->replace(index - 1, 3, static_cast(Jupiter_getOctal(operator[](index))) << 3 | static_cast(Jupiter_getOctal(operator[](index + 1)))); - else - this->replace(index - 1, 2, static_cast(Jupiter_getOctal(operator[](index)))); - break; - case 'a': - this->replace(index - 1, 2, '\a'); - break; - case 'b': - this->replace(index - 1, 2, '\b'); - break; - case 'f': - this->replace(index - 1, 2, '\f'); - break; - case 'n': - this->replace(index - 1, 2, '\n'); - break; - case 'r': - this->replace(index - 1, 2, '\r'); - break; - case 't': - this->replace(index - 1, 2, '\t'); - break; - case 'v': - this->replace(index - 1, 2, '\v'); - break; - case '?': - this->replace(index - 1, 2, '\?'); - break; - case '\'': - this->replace(index - 1, 2, '\''); - break; - case '\"': - this->replace(index - 1, 2, '\"'); - break; - case '\\': - this->replace(index - 1, 2, '\\'); - break; - case 'x': - if (Jupiter::String_Type::length >= index + 2 - && Jupiter_isHex(operator[](index + 1)) && Jupiter_isHex(operator[](index + 2))) - this->replace(index - 1, 4, static_cast(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast(Jupiter_getHex(operator[](index + 2)))); - break; - case 'U': - if (Jupiter::String_Type::length >= index + 8 - && Jupiter_isHex(operator[](index + 1)) && Jupiter_isHex(operator[](index + 2)) && Jupiter_isHex(operator[](index + 3)) && Jupiter_isHex(operator[](index + 4)) && Jupiter_isHex(operator[](index + 5)) && Jupiter_isHex(operator[](index + 6)) && Jupiter_isHex(operator[](index + 7)) && Jupiter_isHex(operator[](index + 8))) - { - uint32_t codepoint = static_cast(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast(Jupiter_getHex(operator[](index + 2))); - codepoint <<= 8; - codepoint |= static_cast(Jupiter_getHex(operator[](index + 3))) << 4 | static_cast(Jupiter_getHex(operator[](index + 4))); - codepoint <<= 8; - codepoint |= static_cast(Jupiter_getHex(operator[](index + 5))) << 4 | static_cast(Jupiter_getHex(operator[](index + 6))); - codepoint <<= 8; - codepoint |= static_cast(Jupiter_getHex(operator[](index + 7))) << 4 | static_cast(Jupiter_getHex(operator[](index + 8))); - if (codepoint <= 0x007F) - this->replace(index - 1, 10, static_cast(codepoint)); - else if (codepoint <= 0x07FF) - { - char bytes[2]; - bytes[0] = 0xC0 | ((codepoint >> 6) & 0x1F); - bytes[1] = 0x80 | (codepoint & 0x3F); - this->replace(index - 1, 10, bytes, sizeof(bytes)); - ++index; - } - else if (codepoint <= 0xFFFF) - { - char bytes[3]; - bytes[0] = 0xE0 | ((codepoint >> 12) & 0x0F); - bytes[1] = 0x80 | ((codepoint >> 6) & 0x3F); - bytes[2] = 0x80 | (codepoint & 0x3F); - this->replace(index - 1, 10, bytes, sizeof(bytes)); - index += 2; - } - else if (codepoint <= 0x10FFFF) - { - char bytes[4]; - bytes[0] = 0xE0 | ((codepoint >> 18) & 0x0F); - bytes[1] = 0x80 | ((codepoint >> 12) & 0x3F); - bytes[2] = 0x80 | ((codepoint >> 6) & 0x3F); - bytes[3] = 0x80 | (codepoint & 0x3F); - this->replace(index - 1, 10, bytes, sizeof(bytes)); - index += 3; - } - } - break; - case 'u': - if (Jupiter::String_Type::length >= index + 4 - && Jupiter_isHex(operator[](index + 1)) && Jupiter_isHex(operator[](index + 2)) && Jupiter_isHex(operator[](index + 3)) && Jupiter_isHex(operator[](index + 4))) - { - uint16_t codepoint = static_cast(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast(Jupiter_getHex(operator[](index + 2))); - codepoint <<= 8; - codepoint |= static_cast(Jupiter_getHex(operator[](index + 3))) << 4 | static_cast(Jupiter_getHex(operator[](index + 4))); - if (codepoint <= 0x007F) - this->replace(index - 1, 6, static_cast(codepoint)); - else if (codepoint <= 0x07FF) - { - char bytes[2]; - bytes[0] = 0xC0 | ((codepoint >> 6) & 0x1F); - bytes[1] = 0x80 | (codepoint & 0x3F); - this->replace(index - 1, 6, bytes, sizeof(bytes)); - ++index; - } - else if (codepoint <= 0xFFFF) - { - char bytes[3]; - bytes[0] = 0xE0 | ((codepoint >> 12) & 0x0F); - bytes[1] = 0x80 | ((codepoint >> 6) & 0x3F); - bytes[2] = 0x80 | (codepoint & 0x3F); - this->replace(index - 1, 6, bytes, sizeof(bytes)); - index += 2; - } - } - break; - default: - break; - } - if (index == Jupiter::String_Type::length) - break; - } - else - ++index; - } -} - -template<> inline void Jupiter::String_Type::processEscapeSequences() -{ -} - // set template size_t Jupiter::String_Type::set(size_t index, const T &in) diff --git a/src/jessilib b/src/jessilib index 645d010..d7e4f33 160000 --- a/src/jessilib +++ b/src/jessilib @@ -1 +1 @@ -Subproject commit 645d01050203f14908bf6f28f0f6151627d24f93 +Subproject commit d7e4f337c786184a68b095449176261ce1700295