Browse Source

Add string_printf, starting to remove String_Type

master
Jessica James 3 years ago
parent
commit
0dea1de9e0
  1. 4
      src/common/File.cpp
  2. 2
      src/common/INIConfig.cpp
  3. 2
      src/common/IRC_Client.cpp
  4. 27
      src/include/Jupiter/Readable_String.h
  5. 11
      src/include/Jupiter/Shift_String.h
  6. 15
      src/include/Jupiter/Shift_String_Imp.h
  7. 63
      src/include/Jupiter/String_Type.h
  8. 166
      src/include/Jupiter/String_Type_Imp.h
  9. 2
      src/jessilib

4
src/common/File.cpp

@ -163,7 +163,7 @@ bool Jupiter::File::load(FILE *file) {
new_line_r: new_line_r:
m_data->lines.emplace_back(buffer); m_data->lines.emplace_back(buffer);
buffer.erase(); buffer.clear();
// check for optional trailing \n // check for optional trailing \n
@ -190,7 +190,7 @@ bool Jupiter::File::load(FILE *file) {
new_line_n: new_line_n:
m_data->lines.emplace_back(buffer); m_data->lines.emplace_back(buffer);
buffer.erase(); buffer.clear();
// check for optional trailing \r // check for optional trailing \r

2
src/common/INIConfig.cpp

@ -235,7 +235,7 @@ bool Jupiter::INIConfig::read_internal(const char *in_filename) {
if (itr == end) if (itr == end)
{ {
// No data remains to be parsed in buffer; erase buffer and break // No data remains to be parsed in buffer; erase buffer and break
buffer.erase(); buffer.clear();
break; break;
} }

2
src/common/IRC_Client.cpp

@ -188,7 +188,7 @@ void Jupiter::IRC::Client::setPrimaryConfigSection(Jupiter::Config *in_primary_s
if (m_primary_section != nullptr) if (m_primary_section != nullptr)
m_primary_section_name = m_primary_section->getName(); m_primary_section_name = m_primary_section->getName();
else else
m_primary_section_name.erase(); m_primary_section_name.clear();
} }
void Jupiter::IRC::Client::setSecondaryConfigSection(Jupiter::Config *in_secondary_section) { void Jupiter::IRC::Client::setSecondaryConfigSection(Jupiter::Config *in_secondary_section) {

27
src/include/Jupiter/Readable_String.h

@ -29,6 +29,7 @@
#include <cstdio> // FILE #include <cstdio> // FILE
#include <string> // std::basic_string<T> type #include <string> // std::basic_string<T> type
#include <iostream> // std::endl #include <iostream> // std::endl
#include <cstdarg>
#include "InvalidIndex.h" #include "InvalidIndex.h"
#include "DataBuffer.h" #include "DataBuffer.h"
#include "Functions.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 #endif // _READABLE_STRING_H_HEADER

11
src/include/Jupiter/Shift_String.h

@ -80,16 +80,7 @@ namespace Jupiter
/** /**
* @brief Removes all elements from the string. * @brief Removes all elements from the string.
*/ */
virtual void erase(); void clear() override;
/**
* @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);
/** /**
* @brief Sets the internal buffer to be at least large enough to old a specified number of elements. * @brief Sets the internal buffer to be at least large enough to old a specified number of elements.

15
src/include/Jupiter/Shift_String_Imp.h

@ -73,23 +73,12 @@ template<typename T> void Jupiter::Shift_String_Type<T>::remove(size_t index, si
Jupiter::String_Type<T>::remove(index, len); Jupiter::String_Type<T>::remove(index, len);
} }
template<typename T> void Jupiter::Shift_String_Type<T>::erase() template<typename T> void Jupiter::Shift_String_Type<T>::clear()
{ {
Jupiter::String_Type<T>::erase(); Jupiter::String_Type<T>::clear();
Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base; Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base;
} }
// capture
template<typename T> void Jupiter::Shift_String_Type<T>::capture(T *in, size_t in_size)
{
delete[] Jupiter::Shift_String_Type<T>::base;
Jupiter::Shift_String_Type<T>::base = in;
Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base;
Jupiter::String_Type<T>::length = in_size;
}
template<typename T> bool Jupiter::Shift_String_Type<T>::setBufferSize(size_t len) template<typename T> bool Jupiter::Shift_String_Type<T>::setBufferSize(size_t len)
{ {
if (len > Jupiter::String_Type<T>::length) if (len > Jupiter::String_Type<T>::length)

63
src/include/Jupiter/String_Type.h

@ -121,22 +121,7 @@ namespace Jupiter
/** /**
* @brief Removes all elements from the string. * @brief Removes all elements from the string.
*/ */
virtual void erase(); virtual void clear();
/**
* @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;
/** /**
* @brief Sets the value of an element at the specified index. * @brief Sets the value of an element at the specified index.
@ -222,8 +207,8 @@ namespace Jupiter
* @param pos Position to start copying from. * @param pos Position to start copying from.
* @return Partial copy of the input string. * @return Partial copy of the input string.
*/ */
template<template<typename> class R> static R<T> substring(const Jupiter::Readable_String<T> &in, size_t pos); template<template<typename> class R> static R<T> substring(const Jupiter::Readable_String<T> &in, size_t pos); // REPLACE
template<template<typename> class R> static R<T> substring(const T *in, size_t pos); template<template<typename> class R> static R<T> substring(const T *in, size_t pos); // REPLACE
/** /**
* @brief Copies a part of an input string and returns it in an output type. * @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. * @param len Number of elements to copy.
* @return Partial copy of the input string. * @return Partial copy of the input string.
*/ */
template<template<typename> class R> static R<T> substring(const Jupiter::Readable_String<T> &in, size_t pos, size_t len); template<template<typename> class R> static R<T> substring(const Jupiter::Readable_String<T> &in, size_t pos, size_t len); // REPLACE
template<template<typename> class R> static R<T> substring(const T *in, size_t pos, size_t len); template<template<typename> class R> static R<T> substring(const T *in, size_t pos, size_t len); // REPLACE
/**
* @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;
/** Mutative operators */ /** Mutative operators */
inline String_Type<T> &operator+=(const String_Type<T> &right) { this->concat(right); return *this; }; inline String_Type<T> &operator+=(const String_Type<T> &right) { this->concat(right); return *this; };
@ -330,9 +297,27 @@ namespace Jupiter
inline bool operator>=(const std::basic_string<T> &right)const{ return !operator<(right); } inline bool operator>=(const std::basic_string<T> &right)const{ return !operator<(right); }
inline bool operator>=(const T 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 */ T *str{}; /** Pointer for the underlying string of elements */
size_t length{}; /** Number of representable elements in the string */ 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 { namespace literals {

166
src/include/Jupiter/String_Type_Imp.h

@ -155,175 +155,11 @@ template<typename T> void Jupiter::String_Type<T>::remove(size_t index, size_t l
// erase // erase
template<typename T> void Jupiter::String_Type<T>::erase() template<typename T> void Jupiter::String_Type<T>::clear()
{ {
Jupiter::String_Type<T>::length = 0; Jupiter::String_Type<T>::length = 0;
} }
// processEscapeSequences
template<typename T> void Jupiter::String_Type<T>::processEscapeSequences()
{
}
template<> inline void Jupiter::String_Type<char>::processEscapeSequences()
{
if (Jupiter::String_Type<char>::length == 0)
return;
size_t index = 0;
while (index + 1 != Jupiter::String_Type<char>::length)
{
if (operator[](index) == '\\')
{
switch (operator[](++index))
{
case '0':
case '1':
case '2':
case '3':
if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isOctal(operator[](index + 1)))
{
if (index + 2 != Jupiter::String_Type<char>::length && Jupiter_isOctal(operator[](index + 2)))
this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getOctal(operator[](index))) << 6 | static_cast<uint8_t>(Jupiter_getOctal(operator[](index + 1))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(operator[](index + 2))));
else
this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getOctal(operator[](index))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(operator[](index + 1))));
}
else
this->replace(index - 1, 2, static_cast<uint8_t>(Jupiter_getOctal(operator[](index))));
break;
case '4':
case '5':
case '6':
case '7':
if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isOctal(operator[](index + 1)))
this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getOctal(operator[](index))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(operator[](index + 1))));
else
this->replace(index - 1, 2, static_cast<uint8_t>(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<char>::length >= index + 2
&& Jupiter_isHex(operator[](index + 1)) && Jupiter_isHex(operator[](index + 2)))
this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 2))));
break;
case 'U':
if (Jupiter::String_Type<char>::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<uint8_t>(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 2)));
codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getHex(operator[](index + 3))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 4)));
codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getHex(operator[](index + 5))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 6)));
codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getHex(operator[](index + 7))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 8)));
if (codepoint <= 0x007F)
this->replace(index - 1, 10, static_cast<uint8_t>(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<char>::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<uint8_t>(Jupiter_getHex(operator[](index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 2)));
codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getHex(operator[](index + 3))) << 4 | static_cast<uint8_t>(Jupiter_getHex(operator[](index + 4)));
if (codepoint <= 0x007F)
this->replace(index - 1, 6, static_cast<uint8_t>(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<char>::length)
break;
}
else
++index;
}
}
template<> inline void Jupiter::String_Type<wchar_t>::processEscapeSequences()
{
}
// set // set
template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const T &in) template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const T &in)

2
src/jessilib

@ -1 +1 @@
Subproject commit 645d01050203f14908bf6f28f0f6151627d24f93 Subproject commit d7e4f337c786184a68b095449176261ce1700295
Loading…
Cancel
Save