Code to implement HTTP requests done. Also, rewrote str_to_int64, str_to_double

This commit is contained in:
2022-04-15 17:24:07 -04:00
parent 453809b65c
commit b6d603034e
17 changed files with 3705 additions and 99 deletions

View File

@@ -1,5 +1,6 @@
#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "fast-float.hpp"
#include <algorithm>
@@ -11,6 +12,8 @@
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <charconv>
#ifdef WIN32
#endif
@@ -22,12 +25,24 @@
namespace util {
static bool ascii_isalpha(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
eng::string ascii_tolower(const eng::string &s) {
eng::string mod = s;
for (int i = 0; i < int(mod.size()); i++) {
if (ascii_isupper(mod[i])) {
mod[i] += 'a' - 'A';
}
}
return mod;
}
static bool ascii_isdigit(char c) {
return ((c >= '0') && (c <= '9'));
eng::string ascii_toupper(const eng::string &s) {
eng::string mod = s;
for (int i = 0; i < int(mod.size()); i++) {
if (ascii_islower(mod[i])) {
mod[i] += 'A' - 'a';
}
}
return mod;
}
bool is_identifier(const eng::string &str) {
@@ -271,33 +286,40 @@ bool has_suffix(const eng::string &s, const eng::string &suffix) {
}
}
bool validinteger(const eng::string &value) {
char *endptr;
if (value.size() == 0) return false;
strtoll(value.c_str(), &endptr, 10);
return (endptr == value.c_str() + value.size());
bool valid_int64(std::string_view value) {
int64_t result;
const char *last = value.data() + value.size();
auto r = std::from_chars(value.data(), last, result, 10);
if (r.ec != std::errc()) return false;
if (r.ptr != last) return false;
return true;
}
int64_t strtoint(const eng::string &value, int64_t errval) {
char *endptr;
if (value.size() == 0) return errval;
int64_t result = strtoll(value.c_str(), &endptr, 10);
if (endptr == value.c_str() + value.size()) {
return result;
} else {
return errval;
}
int64_t str_to_int64(std::string_view value, int64_t errval) {
int64_t result;
const char *last = value.data() + value.size();
auto r = std::from_chars(value.data(), last, result, 10);
if (r.ec != std::errc()) return errval;
if (r.ptr != last) return errval;
return result;
}
double strtodouble(const eng::string &value) {
char *endptr;
if (value.size() == 0) return std::nan("");
double result = strtod(value.c_str(), &endptr);
if (endptr == value.c_str() + value.size()) {
return result;
} else {
return std::nan("");
}
bool valid_double(std::string_view value) {
double result;
const char *last = value.data() + value.size();
auto r = fast_float::from_chars(value.data(), last, result);
if (r.ec != std::errc()) return false;
if (r.ptr != last) return false;
return true;
}
double str_to_double(std::string_view value, double errval) {
double result;
const char *last = value.data() + value.size();
auto r = fast_float::from_chars(value.data(), last, result);
if (r.ec != std::errc()) return errval;
if (r.ptr != last) return errval;
return result;
}
std::string_view sv_ltrim(std::string_view v) {
@@ -330,6 +352,30 @@ std::string_view sv_trim(std::string_view v) {
return std::string_view(b, e-b);
}
std::string_view sv_ltrim(std::string_view v, char c) {
while ((!v.empty()) && (v.front() == c)) {
v.remove_prefix(1);
}
return v;
}
std::string_view sv_rtrim(std::string_view v, char c) {
while ((!v.empty()) && (v.back() == c)) {
v.remove_suffix(1);
}
return v;
}
std::string_view sv_trim(std::string_view v, char c) {
while ((!v.empty()) && (v.front() == c)) {
v.remove_prefix(1);
}
while ((!v.empty()) && (v.back() == c)) {
v.remove_suffix(1);
}
return v;
}
eng::string ltrim(std::string_view v) {
return eng::string(sv_ltrim(v));
}
@@ -342,24 +388,34 @@ eng::string trim(std::string_view v) {
return eng::string(sv_trim(v));
}
std::string_view sv_split_one(std::string_view &v, char sep) {
size_t pos = v.find(sep);
// If there's no separator in the buffer, return a null view.
if (pos == std::string_view::npos) {
return std::string_view();
}
// Split into stuff before the separator, and stuff after.
std::string_view result = v.substr(0, pos);
v = v.substr(pos + 1);
return result;
}
std::string_view sv_read_line(std::string_view &source) {
size_t pos = source.find('\n');
std::string_view result;
if (pos == std::string_view::npos) {
result = source;
source = "";
source = std::string_view();
} else {
result = source.substr(0, pos);
source = source.substr(pos + 1);
}
int fsize = result.size();
if ((fsize >= 1) && (result[fsize - 1] == '\r')) {
result.remove_suffix(1);
}
result = sv_rtrim(result, '\r');
return result;
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
@@ -456,18 +512,14 @@ LuaDefine(unittests_util, "", "some unit tests") {
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
LuaAssert(L, util::tolower("fooBar") == "foobar");
// test validinteger, strtoint, strtodouble
LuaAssert(L, util::validinteger("123") == true);
LuaAssert(L, util::validinteger("123.4") == false);
LuaAssert(L, util::validinteger("12ab") == false);
LuaAssert(L, util::validinteger("") == false);
LuaAssert(L, util::strtoint("123", -5) == 123);
LuaAssert(L, util::strtoint("123.4", -5) == -5);
LuaAssert(L, util::strtoint("12ab", -5) == -5);
LuaAssert(L, util::strtoint("", -5) == -5);
LuaAssert(L, util::strtodouble("123.5") == 123.5);
LuaAssert(L, std::isnan(util::strtodouble("12ab")));
LuaAssert(L, std::isnan(util::strtodouble("")));
// test str_to_int64, str_to_double
LuaAssert(L, util::str_to_int64("123") == 123);
LuaAssert(L, util::str_to_int64("123.4") == INT64_MIN);
LuaAssert(L, util::str_to_int64("12ab") == INT64_MIN);
LuaAssert(L, util::str_to_int64("") == INT64_MIN);
LuaAssert(L, util::str_to_double("123.5") == 123.5);
LuaAssert(L, std::isnan(util::str_to_double("12ab")));
LuaAssert(L, std::isnan(util::str_to_double("")));
// Test trim, ltrim, rtrim
LuaAssert(L, util::ltrim(" foo ") == "foo ");