Browse Source

Remove Jupiter string formatting functions

master
Jessica James 3 years ago
parent
commit
eaff7f0548
  1. 2
      src/common/HTTP_Server.cpp
  2. 34
      src/common/IRC_Client.cpp
  3. 15
      src/include/Jupiter/Readable_String.h
  4. 30
      src/include/Jupiter/String.hpp
  5. 109
      src/include/Jupiter/String_Imp.h
  6. 24
      src/include/Jupiter/String_Type.h
  7. 46
      src/include/Jupiter/String_Type_Imp.h

2
src/common/HTTP_Server.cpp

@ -473,7 +473,7 @@ int Jupiter::HTTP::Server::Data::process_request(HTTPSession &session) {
result += "Server: "_jrs JUPITER_VERSION ENDL; result += "Server: "_jrs JUPITER_VERSION ENDL;
result += Jupiter::StringS::Format("Content-Length: %u" ENDL, content_result->size()); result += string_printf("Content-Length: %u" ENDL, content_result->size());
if (session.keep_alive) if (session.keep_alive)
result += "Connection: keep-alive"_jrs ENDL; result += "Connection: keep-alive"_jrs ENDL;

34
src/common/IRC_Client.cpp

@ -329,33 +329,33 @@ void Jupiter::IRC::Client::setAutoReconnect(int val) {
} }
void Jupiter::IRC::Client::joinChannel(std::string_view in_channel) { void Jupiter::IRC::Client::joinChannel(std::string_view in_channel) {
m_socket->send(Jupiter::StringS::Format("JOIN %.*s" ENDL, in_channel.size(), in_channel.data())); m_socket->send(string_printf("JOIN %.*s" ENDL, in_channel.size(), in_channel.data()));
} }
void Jupiter::IRC::Client::joinChannel(std::string_view in_channel, std::string_view in_password) { void Jupiter::IRC::Client::joinChannel(std::string_view in_channel, std::string_view in_password) {
m_socket->send(Jupiter::StringS::Format("JOIN %.*s %.*s" ENDL, in_channel.size(), in_channel.data(), in_password.size(), in_password.data())); m_socket->send(string_printf("JOIN %.*s %.*s" ENDL, in_channel.size(), in_channel.data(), in_password.size(), in_password.data()));
} }
void Jupiter::IRC::Client::partChannel(std::string_view in_channel) { void Jupiter::IRC::Client::partChannel(std::string_view in_channel) {
m_socket->send(Jupiter::StringS::Format("PART %.*s" ENDL, in_channel.size(), in_channel.data())); m_socket->send(string_printf("PART %.*s" ENDL, in_channel.size(), in_channel.data()));
Jupiter::ReferenceString channel_name = in_channel; Jupiter::ReferenceString channel_name = in_channel;
m_channels[channel_name].setType(-2); m_channels[channel_name].setType(-2);
} }
void Jupiter::IRC::Client::partChannel(std::string_view in_channel, std::string_view in_message) { void Jupiter::IRC::Client::partChannel(std::string_view in_channel, std::string_view in_message) {
m_socket->send(Jupiter::StringS::Format("PART %.*s :%.*s" ENDL, in_channel.size(), in_channel.data(), in_message.size(), in_message.data())); m_socket->send(string_printf("PART %.*s :%.*s" ENDL, in_channel.size(), in_channel.data(), in_message.size(), in_message.data()));
Jupiter::ReferenceString channel_name = in_channel; Jupiter::ReferenceString channel_name = in_channel;
m_channels[channel_name].setType(-2); m_channels[channel_name].setType(-2);
} }
void Jupiter::IRC::Client::sendMessage(std::string_view dest, std::string_view message) { void Jupiter::IRC::Client::sendMessage(std::string_view dest, std::string_view message) {
m_socket->send(Jupiter::StringS::Format("PRIVMSG %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data())); m_socket->send(string_printf("PRIVMSG %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data()));
} }
void Jupiter::IRC::Client::sendNotice(std::string_view dest, std::string_view message) { void Jupiter::IRC::Client::sendNotice(std::string_view dest, std::string_view message) {
m_socket->send(Jupiter::StringS::Format("NOTICE %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data())); m_socket->send(string_printf("NOTICE %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data()));
} }
size_t Jupiter::IRC::Client::messageChannels(int type, std::string_view message) { size_t Jupiter::IRC::Client::messageChannels(int type, std::string_view message) {
@ -670,20 +670,20 @@ int Jupiter::IRC::Client::process_line(std::string_view line) {
break; break;
case Reply::LUSERCLIENT: // 251 case Reply::LUSERCLIENT: // 251
{ {
Jupiter::StringL key = "RawData."; std::string key = "RawData.";
size_t offset; size_t offset = key.size();
unsigned int i = 1; unsigned int i = 1;
Jupiter::ReferenceString value; Jupiter::ReferenceString value;
auto config_loop_condition = [&] auto config_loop_condition = [&]
{ {
offset = key.aformat("%u", i); key += std::to_string(i);
value = Jupiter::IRC::Client::readConfigValue(key); value = Jupiter::IRC::Client::readConfigValue(key);
return !value.empty(); return !value.empty();
}; };
while (config_loop_condition()) while (config_loop_condition())
{ {
key.truncate(offset); key.erase(offset);
Jupiter::IRC::Client::send(value); Jupiter::IRC::Client::send(value);
i++; i++;
} }
@ -1066,7 +1066,7 @@ int Jupiter::IRC::Client::process_line(std::string_view line) {
{ {
if (w1 == "PING"sv) if (w1 == "PING"sv)
{ {
m_socket->send(Jupiter::StringS::Format("PONG %.*s" ENDL, first_split.second.size(), first_split.second.data())); m_socket->send(string_printf("PONG %.*s" ENDL, first_split.second.size(), first_split.second.data()));
} }
else if (w1 == "NICK"sv) else if (w1 == "NICK"sv)
{ {
@ -1163,7 +1163,7 @@ void Jupiter::IRC::Client::disconnect(bool stayDead)
void Jupiter::IRC::Client::disconnect(std::string_view message, bool stayDead) void Jupiter::IRC::Client::disconnect(std::string_view message, bool stayDead)
{ {
m_socket->send(Jupiter::StringS::Format("QUIT :%.*s" ENDL, message.size(), message.data())); m_socket->send(string_printf("QUIT :%.*s" ENDL, message.size(), message.data()));
Jupiter::IRC::Client::disconnect(stayDead); Jupiter::IRC::Client::disconnect(stayDead);
} }
@ -1405,16 +1405,14 @@ bool Jupiter::IRC::Client::startCAP() {
bool Jupiter::IRC::Client::registerClient() { bool Jupiter::IRC::Client::registerClient() {
bool result = true; bool result = true;
const char *localHostname = Jupiter::Socket::getLocalHostname(); const char *localHostname = Jupiter::Socket::getLocalHostname();
Jupiter::StringS messageToSend; std::string message = string_printf("USER %.*s %s %.*s :%.*s" ENDL, m_nickname.size(), m_nickname.data(), localHostname, m_server_hostname.size(), m_server_hostname.c_str(), m_realname.size(), m_realname.data());
messageToSend.format("USER %.*s %s %.*s :%.*s" ENDL, m_nickname.size(), m_nickname.data(), localHostname, m_server_hostname.size(), m_server_hostname.c_str(), m_realname.size(), m_realname.data());
if (m_socket->send(messageToSend) <= 0) if (m_socket->send(message) <= 0)
result = false; result = false;
messageToSend.format("NICK %.*s" ENDL, m_nickname.size(), m_nickname.data()); message = string_printf("NICK %.*s" ENDL, m_nickname.size(), m_nickname.data());
if (m_socket->send(messageToSend) <= 0) if (m_socket->send(message) <= 0)
result = false; result = false;
m_connection_status = 3; m_connection_status = 3;

15
src/include/Jupiter/Readable_String.h

@ -160,15 +160,13 @@ namespace Jupiter {
} }
} }
inline std::string string_printf(const char* format, ...) { inline std::string vstring_printf(const char* format, va_list args) {
std::string result; std::string result;
va_list args;
va_start(args, format);
va_list args_copy; va_list args_copy;
va_copy(args_copy, args); va_copy(args_copy, args);
int min_length = std::vsnprintf(nullptr, 0, format, args_copy); int min_length = std::vsnprintf(nullptr, 0, format, args_copy);
va_end(args_copy); va_end(args_copy);
if (min_length > 0) { if (min_length > 0) {
result.resize(min_length); result.resize(min_length);
@ -182,6 +180,15 @@ inline std::string string_printf(const char* format, ...) {
} }
} }
return result;
}
inline std::string string_printf(const char* format, ...) {
va_list args;
va_start(args, format);
std::string result = vstring_printf(format, args);
va_end(args); va_end(args);
return result; return result;
} }

30
src/include/Jupiter/String.hpp

@ -47,36 +47,6 @@ namespace Jupiter
{ {
public: public:
/**
* @brief Sets the String's contents based on the format string and input variables.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t.
*
* @param format Format that the string is compared against.
* @param args Inputs to match the format specifiers.
* @return Number of characters written.
*/
size_t vformat(const T *format, va_list args);
/**
* @brief Appends to a String's contents based on the format string and input variables.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t.
*
* @param format Format that the string is compared against.
* @param args Inputs to match the format specifiers.
* @return Number of characters written.
*/
size_t avformat(const T *format, va_list args);
/**
* @brief Sets the String's contents based on the format string and input variables.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t.
*
* @param format Format that the string is compared against.
* @param ... Inputs to match the format specifiers.
* @return String containing the new format.
*/
static String_Strict Format(const T *format, ...);
/** /**
* @brief Creates a partial copy of the string. * @brief Creates a partial copy of the string.
* *

109
src/include/Jupiter/String_Imp.h

@ -26,29 +26,6 @@
#include "String.hpp" #include "String.hpp"
#if !defined va_copy
#if defined __INTEL_COMPILER
#pragma message("Warning: va_copy not properly defined. Assuming common implementation.")
#define va_copy(dst, src) ((void)((dst) = (src)))
#else
#error "va_copy not defined."
#endif // __INTEL_COMPILER
#endif // va_copy
#if !defined JUPITER_VSCPRINTF
#if defined _WIN32
#define JUPITER_VSCPRINTF(format, format_args) _vscprintf(format, format_args)
#define JUPITER_VSCWPRINTF(format, format_args) _vscwprintf(format, format_args)
#else // _WIN32
#define JUPITER_VSCPRINTF(format, format_args) vsnprintf(nullptr, 0, format, format_args)
#define JUPITER_VSCWPRINTF(format, format_args) vswprintf(nullptr, 0, format, format_args)
#endif // _WIN32
#endif // JUPITER_VSCPRINTF
/** /**
* IMPLEMENTATION: * IMPLEMENTATION:
* String_Strict * String_Strict
@ -170,92 +147,6 @@ template<typename T> Jupiter::String_Strict<T>::String_Strict(const Jupiter::Rea
Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base; Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base;
} }
// vformat()
template<> size_t inline Jupiter::String_Strict<char>::vformat(const char *format, va_list args)
{
int minLen;
va_list sargs;
va_copy(sargs, args);
minLen = JUPITER_VSCPRINTF(format, sargs);
va_end(sargs);
if (minLen < 0) return 0; // We simply can not work with this.
this->setBufferSizeNoCopy(minLen + 1); // vsnprintf REQUIRES space for an additional null character.
minLen = vsnprintf(Jupiter::String_Type<char>::str, minLen + 1, format, args);
if (minLen < 0) return 0;
return Jupiter::String_Type<char>::length = minLen;
}
template<> size_t inline Jupiter::String_Strict<wchar_t>::vformat(const wchar_t *format, va_list args)
{
int minLen;
va_list sargs;
va_copy(sargs, args);
minLen = JUPITER_VSCWPRINTF(format, sargs);
va_end(sargs);
if (minLen < 0) return 0; // We simply can not work with this.
this->setBufferSizeNoCopy(minLen + 1); // vsnprintf REQUIRES space for an additional null character.
minLen = vswprintf(Jupiter::String_Type<wchar_t>::str, minLen + 1, format, args);
if (minLen < 0) return 0;
return Jupiter::String_Type<wchar_t>::length = minLen;
}
template<typename T> size_t Jupiter::String_Strict<T>::vformat(const T *format, va_list args)
{
return 0;
}
// avformat()
template<> size_t inline Jupiter::String_Strict<char>::avformat(const char *format, va_list args)
{
int minLen;
va_list sargs;
va_copy(sargs, args);
minLen = JUPITER_VSCPRINTF(format, sargs);
va_end(sargs);
if (minLen < 0) return 0; // We simply can not work with this.
this->setBufferSize(Jupiter::String_Type<char>::length + minLen + 1); // vsnprintf REQUIRES space for an additional null character.
minLen = vsnprintf(Jupiter::String_Type<char>::str + Jupiter::String_Type<char>::length, minLen + 1, format, args);
if (minLen <= 0) return 0;
Jupiter::String_Type<char>::length += minLen;
return minLen;
}
template<> size_t inline Jupiter::String_Strict<wchar_t>::avformat(const wchar_t *format, va_list args)
{
int minLen;
va_list sargs;
va_copy(sargs, args);
minLen = JUPITER_VSCWPRINTF(format, sargs);
va_end(sargs);
if (minLen < 0) return 0; // We simply can not work with this.
this->setBufferSize(minLen + Jupiter::String_Type<wchar_t>::length + 1); // vsnprintf REQUIRES space for an additional null character.
minLen = vswprintf(Jupiter::String_Type<wchar_t>::str + Jupiter::String_Type<wchar_t>::length, minLen + 1, format, args);
if (minLen <= 0) return 0;
Jupiter::String_Type<wchar_t>::length += minLen;
return minLen;
}
template<typename T> size_t Jupiter::String_Strict<T>::avformat(const T *format, va_list args)
{
return 0;
}
template<typename T> Jupiter::String_Strict<T> Jupiter::String_Strict<T>::Format(const T *format, ...)
{
String_Strict<T> r;
va_list args;
va_start(args, format);
r.vformat(format, args);
va_end(args);
return r;
}
template<typename T> typename Jupiter::String_Strict<T> Jupiter::String_Strict<T>::substring(size_t pos) const template<typename T> typename Jupiter::String_Strict<T> Jupiter::String_Strict<T>::substring(size_t pos) const
{ {
return Jupiter::String_Strict<T>::substring(*this, pos); return Jupiter::String_Strict<T>::substring(*this, pos);

24
src/include/Jupiter/String_Type.h

@ -70,30 +70,6 @@ namespace Jupiter
*/ */
const T *data() const; const T *data() const;
/**
* @brief Sets the String's contents based on the format string and input variables.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t.
*
* @param format Format that the string is compared against.
* @param ... Inputs to match the format specifiers.
* @return Number of characters written.
*/
size_t format(const std::basic_string<T> &format, ...);
size_t format(const T *format, ...);
virtual size_t vformat(const T *format, va_list args) = 0;
/**
* @brief Appends to a String's contents based on the format string and input variables.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t.
*
* @param format Format that the string is compared against.
* @param ... Inputs to match the format specifiers.
* @return Number of characters written.
*/
size_t aformat(const std::basic_string<T> &format, ...);
size_t aformat(const T *format, ...);
virtual size_t avformat(const T *format, va_list args) = 0;
/** /**
* @brief Truncates the string by a specified number of elements. * @brief Truncates the string by a specified number of elements.
* *

46
src/include/Jupiter/String_Type_Imp.h

@ -56,52 +56,6 @@ template<typename T> const T *Jupiter::String_Type<T>::data() const
return Jupiter::String_Type<T>::str; return Jupiter::String_Type<T>::str;
} }
// format forwards
template<typename T> size_t Jupiter::String_Type<T>::format(const std::basic_string<T> &format, ...)
{
const char *ptr = format.c_str();
size_t r;
va_list args;
va_start(args, ptr);
r = this->vformat(ptr, args);
va_end(args);
return r;
}
template<typename T> size_t Jupiter::String_Type<T>::format(const T *format, ...)
{
size_t r;
va_list args;
va_start(args, format);
r = this->vformat(format, args);
va_end(args);
return r;
}
// aformat forwards
template<typename T> size_t Jupiter::String_Type<T>::aformat(const std::basic_string<T> &format, ...)
{
const char *ptr = format.c_str();
size_t r;
va_list args;
va_start(args, ptr);
r = this->avformat(ptr, args);
va_end(args);
return r;
}
template<typename T> size_t Jupiter::String_Type<T>::aformat(const T *format, ...)
{
size_t r;
va_list args;
va_start(args, format);
r = this->avformat(format, args);
va_end(args);
return r;
}
// truncate base // truncate base
template<typename T> size_t Jupiter::String_Type<T>::truncate(size_t n) template<typename T> size_t Jupiter::String_Type<T>::truncate(size_t n)

Loading…
Cancel
Save