Major string_view refactor. Added namespace sv
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
#include "wrap-string.hpp"
|
||||
#include "wrap-vector.hpp"
|
||||
#include "util.hpp"
|
||||
#include "fast-float.hpp"
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <iomanip>
|
||||
@@ -23,29 +22,113 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace util {
|
||||
namespace sv {
|
||||
|
||||
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;
|
||||
bool valid_int64(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;
|
||||
}
|
||||
|
||||
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 valid_double(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;
|
||||
}
|
||||
|
||||
bool is_identifier(const eng::string &str) {
|
||||
int64_t to_int64(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 to_double(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;
|
||||
}
|
||||
|
||||
string_view ltrim(string_view v) {
|
||||
while ((!v.empty()) && (ascii_isspace(v.front()))) {
|
||||
v.remove_prefix(1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
string_view rtrim(string_view v) {
|
||||
while ((!v.empty()) && (ascii_isspace(v.back()))) {
|
||||
v.remove_suffix(1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
string_view trim(string_view v) {
|
||||
while ((!v.empty()) && (ascii_isspace(v.front()))) {
|
||||
v.remove_prefix(1);
|
||||
}
|
||||
while ((!v.empty()) && (ascii_isspace(v.back()))) {
|
||||
v.remove_suffix(1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
string_view ltrim(string_view v, char c) {
|
||||
while ((!v.empty()) && (v.front() == c)) {
|
||||
v.remove_prefix(1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
string_view rtrim(string_view v, char c) {
|
||||
while ((!v.empty()) && (v.back() == c)) {
|
||||
v.remove_suffix(1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
string_view trim(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;
|
||||
}
|
||||
|
||||
bool has_prefix(string_view s, string_view prefix) {
|
||||
return 0 == s.compare(0, prefix.size(), prefix);
|
||||
}
|
||||
|
||||
bool has_suffix(string_view s, string_view suffix) {
|
||||
if (s.length() >= suffix.length()) {
|
||||
return (0 == s.compare (s.length() - suffix.length(), suffix.length(), suffix));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int common_prefix_length(string_view a, string_view b) {
|
||||
int minlen = std::min(a.size(), b.size());
|
||||
for (int i = 0; i < minlen; i++) {
|
||||
if (a[i] != b[i]) return i;
|
||||
}
|
||||
return minlen;
|
||||
}
|
||||
|
||||
bool is_lua_id(string_view str) {
|
||||
if (str.size() == 0) return false;
|
||||
char c=str[0];
|
||||
if ((!ascii_isalpha(c)) && (c!='_')) return false;
|
||||
@@ -56,6 +139,84 @@ bool is_identifier(const eng::string &str) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_lua_comment(string_view s) {
|
||||
int start = 0;
|
||||
while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
|
||||
return s.substr(start, 2) == "--";
|
||||
}
|
||||
|
||||
string_view read_to_sep(string_view &source, char sep) {
|
||||
size_t pos = source.find(sep);
|
||||
string_view result;
|
||||
if (pos == string_view::npos) {
|
||||
result = source;
|
||||
source = string_view();
|
||||
} else {
|
||||
result = source.substr(0, pos);
|
||||
source = source.substr(pos + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string_view read_to_line(string_view &source) {
|
||||
size_t pos = source.find('\n');
|
||||
string_view result;
|
||||
if (pos == string_view::npos) {
|
||||
result = source;
|
||||
source = string_view();
|
||||
} else {
|
||||
result = source.substr(0, pos);
|
||||
source = source.substr(pos + 1);
|
||||
}
|
||||
if ((!result.empty()) && (result.back() == '\r')) {
|
||||
result.remove_suffix(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string_view read_to_space(string_view &source) {
|
||||
size_t pos1 = 0;
|
||||
while ((pos1 < source.size()) && (!ascii_isspace(source[pos1]))) {
|
||||
pos1 += 1;
|
||||
}
|
||||
string_view result = source.substr(0, pos1);
|
||||
if (pos1 == source.size()) {
|
||||
source = string_view();
|
||||
return result;
|
||||
}
|
||||
size_t pos2 = pos1 + 1;
|
||||
while ((pos2 < source.size()) && (ascii_isspace(source[pos2]))) {
|
||||
pos2 += 1;
|
||||
}
|
||||
source = source.substr(pos2);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace sv
|
||||
|
||||
|
||||
namespace util {
|
||||
|
||||
eng::string ascii_tolower(std::string_view s) {
|
||||
eng::string mod(s);
|
||||
for (int i = 0; i < int(mod.size()); i++) {
|
||||
if (sv::ascii_isupper(mod[i])) {
|
||||
mod[i] += 'a' - 'A';
|
||||
}
|
||||
}
|
||||
return mod;
|
||||
}
|
||||
|
||||
eng::string ascii_toupper(std::string_view s) {
|
||||
eng::string mod(s);
|
||||
for (int i = 0; i < int(mod.size()); i++) {
|
||||
if (sv::ascii_islower(mod[i])) {
|
||||
mod[i] += 'A' - 'a';
|
||||
}
|
||||
}
|
||||
return mod;
|
||||
}
|
||||
|
||||
void quote_string(const eng::string &s, std::ostream *os) {
|
||||
bool anysq = false;
|
||||
bool anydq = false;
|
||||
@@ -251,14 +412,6 @@ eng::string repeat_string(const eng::string &a, int n) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int common_prefix_length(const eng::string &a, const eng::string &b) {
|
||||
int minlen = std::min(a.size(), b.size());
|
||||
for (int i = 0; i < minlen; i++) {
|
||||
if (a[i] != b[i]) return i;
|
||||
}
|
||||
return minlen;
|
||||
}
|
||||
|
||||
eng::string tolower(eng::string input) {
|
||||
for (int i = 0; i < int(input.size()); i++) {
|
||||
@@ -274,148 +427,6 @@ eng::string toupper(eng::string input) {
|
||||
return input;
|
||||
}
|
||||
|
||||
bool has_prefix(const eng::string &s, const eng::string &prefix) {
|
||||
return 0 == s.compare(0, prefix.size(), prefix);
|
||||
}
|
||||
|
||||
bool has_suffix(const eng::string &s, const eng::string &suffix) {
|
||||
if (s.length() >= suffix.length()) {
|
||||
return (0 == s.compare (s.length() - suffix.length(), suffix.length(), suffix));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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) {
|
||||
const char *b = v.data();
|
||||
const char *e = v.data() + v.size();
|
||||
while ((e > b) && (std::isspace(b[0]))) {
|
||||
b++;
|
||||
}
|
||||
return std::string_view(b, e-b);
|
||||
}
|
||||
|
||||
std::string_view sv_rtrim(std::string_view v) {
|
||||
const char *b = v.data();
|
||||
const char *e = v.data() + v.size();
|
||||
while ((e > b) && (std::isspace(e[-1]))) {
|
||||
e--;
|
||||
}
|
||||
return std::string_view(b, e-b);
|
||||
}
|
||||
|
||||
std::string_view sv_trim(std::string_view v) {
|
||||
const char *b = v.data();
|
||||
const char *e = v.data() + v.size();
|
||||
while ((e > b) && (std::isspace(b[0]))) {
|
||||
b++;
|
||||
}
|
||||
while ((e > b) && (std::isspace(e[-1]))) {
|
||||
e--;
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
eng::string rtrim(std::string_view v) {
|
||||
return eng::string(sv_rtrim(v));
|
||||
}
|
||||
|
||||
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 = std::string_view();
|
||||
} else {
|
||||
result = source.substr(0, pos);
|
||||
source = source.substr(pos + 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;
|
||||
@@ -433,12 +444,6 @@ LuaSourcePtr make_lua_source(const eng::string &code) {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_lua_comment(const eng::string &s) {
|
||||
int start = 0;
|
||||
while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
|
||||
return s.substr(start, 2) == "--";
|
||||
}
|
||||
|
||||
eng::string XYZ::debug_string() const {
|
||||
eng::ostringstream oss;
|
||||
oss << "(" << x << "," << y << "," << z << ")";
|
||||
@@ -468,6 +473,57 @@ std::ostream &operator<<(std::ostream &oss, const util::hex8 &v) {
|
||||
}
|
||||
|
||||
LuaDefine(unittests_util, "", "some unit tests") {
|
||||
// test str_to_int64, str_to_double
|
||||
LuaAssert(L, sv::to_int64("123") == 123);
|
||||
LuaAssert(L, sv::to_int64("123.4") == INT64_MIN);
|
||||
LuaAssert(L, sv::to_int64("12ab") == INT64_MIN);
|
||||
LuaAssert(L, sv::to_int64("") == INT64_MIN);
|
||||
LuaAssert(L, sv::to_double("123.5") == 123.5);
|
||||
LuaAssert(L, std::isnan(sv::to_double("12ab")));
|
||||
LuaAssert(L, std::isnan(sv::to_double("")));
|
||||
|
||||
// Test trim, ltrim, rtrim
|
||||
LuaAssert(L, sv::ltrim(" foo ") == "foo ");
|
||||
LuaAssert(L, sv::rtrim(" foo ") == " foo");
|
||||
LuaAssert(L, sv::trim(" foo ") == "foo");
|
||||
LuaAssert(L, sv::trim("foo") == "foo");
|
||||
LuaAssert(L, sv::trim("") == "");
|
||||
LuaAssert(L, sv::ltrim("**foo**", '*') == "foo**");
|
||||
LuaAssert(L, sv::rtrim("**foo**", '*') == "**foo");
|
||||
LuaAssert(L, sv::trim("**foo**", '*') == "foo");
|
||||
LuaAssert(L, sv::trim("foo", '*') == "foo");
|
||||
LuaAssert(L, sv::trim("", '*') == "");
|
||||
|
||||
// Test read_to_line
|
||||
std::string_view v = "foo\nbar\r\nbaz";
|
||||
|
||||
std::string_view v1 = sv::read_to_line(v);
|
||||
LuaAssertStrEq(L, v1, "foo");
|
||||
LuaAssertStrEq(L, v, "bar\r\nbaz");
|
||||
|
||||
std::string_view v2 = sv::read_to_line(v);
|
||||
LuaAssertStrEq(L, v2, "bar");
|
||||
LuaAssertStrEq(L, v, "baz");
|
||||
|
||||
std::string_view v3 = sv::read_to_line(v);
|
||||
LuaAssertStrEq(L, v3, "baz");
|
||||
LuaAssert(L, sv::isnull(v));
|
||||
|
||||
// Test read_to_space
|
||||
v = "foo bar baz";
|
||||
|
||||
std::string_view s1 = sv::read_to_space(v);
|
||||
LuaAssertStrEq(L, s1, "foo");
|
||||
LuaAssertStrEq(L, v, "bar baz");
|
||||
|
||||
std::string_view s2 = sv::read_to_space(v);
|
||||
LuaAssertStrEq(L, s2, "bar");
|
||||
LuaAssertStrEq(L, v, "baz");
|
||||
|
||||
std::string_view s3 = sv::read_to_space(v);
|
||||
LuaAssertStrEq(L, s3, "baz");
|
||||
LuaAssert(L, sv::isnull(v));
|
||||
|
||||
// Test the unioning of ID vectors.
|
||||
util::IdVector idv1,idv2;
|
||||
idv1.push_back(1);
|
||||
@@ -512,31 +568,6 @@ LuaDefine(unittests_util, "", "some unit tests") {
|
||||
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
|
||||
LuaAssert(L, util::tolower("fooBar") == "foobar");
|
||||
|
||||
// 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 ");
|
||||
LuaAssert(L, util::rtrim(" foo ") == " foo");
|
||||
LuaAssert(L, util::trim(" foo ") == "foo");
|
||||
LuaAssert(L, util::trim("foo") == "foo");
|
||||
LuaAssert(L, util::trim("") == "");
|
||||
|
||||
// Test sv_read_line
|
||||
std::string_view v = "foo\nbar\r\n";
|
||||
std::string_view v1 = util::sv_read_line(v);
|
||||
std::string_view v2 = util::sv_read_line(v);
|
||||
std::string_view v3 = util::sv_read_line(v);
|
||||
LuaAssertStrEq(L, v1, "foo");
|
||||
LuaAssertStrEq(L, v2, "bar");
|
||||
LuaAssertStrEq(L, v3, "");
|
||||
|
||||
// Test distance_squared
|
||||
LuaAssert(L, util::distance_squared(1, 1, 5, 4) == 25.0);
|
||||
LuaAssert(L, util::distance_squared(5, 4, 1, 1) == 25.0);
|
||||
|
||||
Reference in New Issue
Block a user