Global: Move string number parse to text/parsers.

It's not used as much as the other string utils.
This commit is contained in:
Unknown W. Brackets 2020-08-10 01:43:40 -07:00
parent 3d9c4733ab
commit a0f8e788f3
10 changed files with 66 additions and 126 deletions

View file

@ -17,6 +17,7 @@
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include "util/text/parsers.h"
#include "Common/StringUtils.h" #include "Common/StringUtils.h"
#include "Core/Debugger/WebSocket/WebSocketUtils.h" #include "Core/Debugger/WebSocket/WebSocketUtils.h"

View file

@ -26,6 +26,7 @@
#include "i18n/i18n.h" #include "i18n/i18n.h"
#include "ext/xxhash.h" #include "ext/xxhash.h"
#include "file/ini_file.h" #include "file/ini_file.h"
#include "util/text/parsers.h"
#include "Common/ColorConv.h" #include "Common/ColorConv.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Core/Config.h" #include "Core/Config.h"

View file

@ -21,6 +21,7 @@
#include <vector> #include <vector>
#include "base/stringutil.h" #include "base/stringutil.h"
#include "util/text/parsers.h"
#include "Common/ColorConv.h" #include "Common/ColorConv.h"
#include "Core/Config.h" #include "Core/Config.h"
#include "Core/Screenshot.h" #include "Core/Screenshot.h"

View file

@ -22,8 +22,6 @@
#include "base/stringutil.h" #include "base/stringutil.h"
#ifdef _WIN32 #ifdef _WIN32
// Function Cross-Compatibility
#define strcasecmp _stricmp
void OutputDebugStringUTF8(const char *p) { void OutputDebugStringUTF8(const char *p) {
wchar_t temp[4096]; wchar_t temp[4096];
@ -84,46 +82,6 @@ void StringUpper(char *str, int len) {
} }
} }
unsigned int parseHex(const char *_szValue)
{
int Value = 0;
size_t Finish = strlen(_szValue);
if (Finish > 8 ) { Finish = 8; }
for (size_t Count = 0; Count < Finish; Count++) {
Value = (Value << 4);
switch( _szValue[Count] ) {
case '0': break;
case '1': Value += 1; break;
case '2': Value += 2; break;
case '3': Value += 3; break;
case '4': Value += 4; break;
case '5': Value += 5; break;
case '6': Value += 6; break;
case '7': Value += 7; break;
case '8': Value += 8; break;
case '9': Value += 9; break;
case 'A': Value += 10; break;
case 'a': Value += 10; break;
case 'B': Value += 11; break;
case 'b': Value += 11; break;
case 'C': Value += 12; break;
case 'c': Value += 12; break;
case 'D': Value += 13; break;
case 'd': Value += 13; break;
case 'E': Value += 14; break;
case 'e': Value += 14; break;
case 'F': Value += 15; break;
case 'f': Value += 15; break;
default:
Value = (Value >> 4);
Count = Finish;
}
}
return Value;
}
void DataToHexString(const uint8_t *data, size_t size, std::string *output) { void DataToHexString(const uint8_t *data, size_t size, std::string *output) {
Buffer buffer; Buffer buffer;
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
@ -223,49 +181,6 @@ std::string ArrayToString(const uint8_t *data, uint32_t size, int line_len, bool
return oss.str(); return oss.str();
} }
bool TryParse(const std::string &str, uint32_t *const output)
{
char *endptr = NULL;
// Holy crap this is ugly.
// Reset errno to a value other than ERANGE
errno = 0;
unsigned long value = strtoul(str.c_str(), &endptr, 0);
if (!endptr || *endptr)
return false;
if (errno == ERANGE)
return false;
if (ULONG_MAX > UINT_MAX) {
#ifdef _MSC_VER
#pragma warning (disable:4309)
#endif
// Note: The typecasts avoid GCC warnings when long is 32 bits wide.
if (value >= static_cast<unsigned long>(0x100000000ull)
&& value <= static_cast<unsigned long>(0xFFFFFFFF00000000ull))
return false;
}
*output = static_cast<uint32_t>(value);
return true;
}
bool TryParse(const std::string &str, bool *const output)
{
if ("1" == str || !strcasecmp("true", str.c_str()))
*output = true;
else if ("0" == str || !strcasecmp("false", str.c_str()))
*output = false;
else
return false;
return true;
}
void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) void SplitString(const std::string& str, const char delim, std::vector<std::string>& output)
{ {
size_t next = 0; size_t next = 0;

View file

@ -9,6 +9,7 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning (disable:4996) #pragma warning (disable:4996)
#define strncasecmp _strnicmp #define strncasecmp _strnicmp
#define strcasecmp _stricmp
#else #else
#include <strings.h> #include <strings.h>
#endif #endif
@ -59,10 +60,6 @@ inline void StringToHexString(const std::string &data, std::string *output) {
DataToHexString((uint8_t *)(&data[0]), data.size(), output); DataToHexString((uint8_t *)(&data[0]), data.size(), output);
} }
// highly unsafe and not recommended.
unsigned int parseHex(const char* _szValue);
std::string StringFromFormat(const char* format, ...); std::string StringFromFormat(const char* format, ...);
std::string StringFromInt(int value); std::string StringFromInt(int value);
std::string StringFromBool(bool value); std::string StringFromBool(bool value);
@ -72,23 +69,6 @@ std::string ArrayToString(const uint8_t *data, uint32_t size, int line_len = 20,
std::string StripSpaces(const std::string &s); std::string StripSpaces(const std::string &s);
std::string StripQuotes(const std::string &s); std::string StripQuotes(const std::string &s);
bool TryParse(const std::string &str, bool *const output);
bool TryParse(const std::string &str, uint32_t *const output);
template <typename N>
static bool TryParse(const std::string &str, N *const output)
{
std::istringstream iss(str);
N tmp = 0;
if (iss >> tmp)
{
*output = tmp;
return true;
}
else
return false;
}
void SplitString(const std::string& str, const char delim, std::vector<std::string>& output); void SplitString(const std::string& str, const char delim, std::vector<std::string>& output);
void GetQuotedStrings(const std::string& str, std::vector<std::string>& output); void GetQuotedStrings(const std::string& str, std::vector<std::string>& output);
@ -98,14 +78,6 @@ std::string ReplaceAll(std::string input, const std::string& src, const std::str
// Compare two strings, ignore the difference between the ignorestr1 and the ignorestr2 in str1 and str2. // Compare two strings, ignore the difference between the ignorestr1 and the ignorestr2 in str1 and str2.
int strcmpIgnore(std::string str1, std::string str2, std::string ignorestr1, std::string ignorestr2); int strcmpIgnore(std::string str1, std::string str2, std::string ignorestr1, std::string ignorestr2);
template <typename N>
static std::string ValueToString(const N value)
{
std::stringstream string;
string << value;
return string.str();
}
void StringTrimEndNonAlphaNum(char *str); void StringTrimEndNonAlphaNum(char *str);
void SkipSpace(const char **ptr); void SkipSpace(const char **ptr);
void StringUpper(char *str); void StringUpper(char *str);

View file

@ -4,9 +4,6 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#include <direct.h> #include <direct.h>
#ifndef strcasecmp
#define strcasecmp _stricmp
#endif
#else #else
#include <strings.h> #include <strings.h>
#include <dirent.h> #include <dirent.h>
@ -23,6 +20,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/stringutil.h"
#include "file/file_util.h" #include "file/file_util.h"
#include "util/text/utf8.h" #include "util/text/utf8.h"

View file

@ -19,11 +19,10 @@
#include "base/stringutil.h" #include "base/stringutil.h"
#include "file/ini_file.h" #include "file/ini_file.h"
#include "file/vfs.h" #include "file/vfs.h"
#include "util/text/parsers.h"
#ifdef _WIN32 #ifdef _WIN32
#include "../util/text/utf8.h" #include "../util/text/utf8.h"
// Function Cross-Compatibility
#define strcasecmp _stricmp
#endif #endif
static bool ParseLineKey(const std::string &line, size_t &pos, std::string *keyOut) { static bool ParseLineKey(const std::string &line, size_t &pos, std::string *keyOut) {

View file

@ -23,11 +23,6 @@
// TODO: Not a great cross dependency. // TODO: Not a great cross dependency.
#include "Common/Crypto/sha1.h" #include "Common/Crypto/sha1.h"
#ifdef _WIN32
// Function Cross-Compatibility
#define strcasecmp _stricmp
#endif
static const char *const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; static const char *const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
namespace net { namespace net {

View file

@ -1,6 +1,7 @@
#include <cstdio>
#include <string> #include <string>
#include <stdio.h>
#include "base/stringutil.h"
#include "util/text/parsers.h" #include "util/text/parsers.h"
bool Version::ParseVersionString(std::string str) { bool Version::ParseVersionString(std::string str) {
@ -37,3 +38,44 @@ bool ParseMacAddress(std::string str, uint8_t macAddr[6]) {
} }
return true; return true;
} }
bool TryParse(const std::string &str, uint32_t *const output) {
char *endptr = NULL;
// Holy crap this is ugly.
// Reset errno to a value other than ERANGE
errno = 0;
unsigned long value = strtoul(str.c_str(), &endptr, 0);
if (!endptr || *endptr)
return false;
if (errno == ERANGE)
return false;
if (ULONG_MAX > UINT_MAX) {
#ifdef _MSC_VER
#pragma warning (disable:4309)
#endif
// Note: The typecasts avoid GCC warnings when long is 32 bits wide.
if (value >= static_cast<unsigned long>(0x100000000ull)
&& value <= static_cast<unsigned long>(0xFFFFFFFF00000000ull))
return false;
}
*output = static_cast<uint32_t>(value);
return true;
}
bool TryParse(const std::string &str, bool *const output) {
if ("1" == str || !strcasecmp("true", str.c_str()))
*output = true;
else if ("0" == str || !strcasecmp("false", str.c_str()))
*output = false;
else
return false;
return true;
}

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <sstream>
#include "base/basictypes.h" #include "base/basictypes.h"
@ -53,4 +54,19 @@ private:
bool ParseVersionString(std::string str); bool ParseVersionString(std::string str);
}; };
bool ParseMacAddress(std::string str, uint8_t macAddr[6]); bool ParseMacAddress(std::string str, uint8_t macAddr[6]);
bool TryParse(const std::string &str, bool *const output);
bool TryParse(const std::string &str, uint32_t *const output);
template <typename N>
static bool TryParse(const std::string &str, N *const output) {
std::istringstream iss(str);
N tmp = 0;
if (iss >> tmp) {
*output = tmp;
return true;
} else
return false;
}