lots of work on determinism in the linux driver.

This commit is contained in:
2022-02-18 03:59:21 -05:00
parent 6a6d2c7f75
commit ba1e923b5a
10 changed files with 175 additions and 110 deletions

View File

@@ -269,22 +269,67 @@ double strtodouble(const std::string &value) {
}
}
std::string ltrim(std::string s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
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 rtrim(std::string s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
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 trim(std::string s) {
return ltrim(rtrim(s));
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 ltrim(std::string_view v) {
return std::string(sv_ltrim(v));
}
std::string rtrim(std::string_view v) {
return std::string(sv_rtrim(v));
}
std::string trim(std::string_view v) {
return std::string(sv_trim(v));
}
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 = "";
} 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);
}
return result;
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
@@ -431,6 +476,15 @@ LuaDefine(unittests_util, "", "some unit tests") {
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);