From 08f6aa209209c56088ba16d1fc8b49cea92574fd Mon Sep 17 00:00:00 2001 From: jyelon Date: Thu, 24 Feb 2022 13:50:43 -0500 Subject: [PATCH] Get rid of wrap-string-view --- luprex/core/cpp/drivenengine.cpp | 4 ++-- luprex/core/cpp/drivenengine.hpp | 4 ++-- luprex/core/cpp/driver-common.cpp | 18 +++++++-------- luprex/core/cpp/driver-linux.cpp | 2 +- luprex/core/cpp/driver-mingw.cpp | 4 ++-- luprex/core/cpp/driver-util.cpp | 14 +++++++----- luprex/core/cpp/driver-util.hpp | 7 +++--- luprex/core/cpp/util.cpp | 32 +++++++++++++-------------- luprex/core/cpp/util.hpp | 17 +++++++------- luprex/core/wrap/wrap-string-view.hpp | 19 ---------------- 10 files changed, 53 insertions(+), 68 deletions(-) delete mode 100644 luprex/core/wrap/wrap-string-view.hpp diff --git a/luprex/core/cpp/drivenengine.cpp b/luprex/core/cpp/drivenengine.cpp index 8439ffa8..7a90add6 100644 --- a/luprex/core/cpp/drivenengine.cpp +++ b/luprex/core/cpp/drivenengine.cpp @@ -241,7 +241,7 @@ void DrivenEngine::drv_recv_incoming(int chid, int nbytes, const char *bytes) { } } -void DrivenEngine::drv_notify_close(int chid, eng::string_view err) { +void DrivenEngine::drv_notify_close(int chid, std::string_view err) { Channel *ch = get_chid(chid); ch->closed_ = true; ch->error_ = err; @@ -260,7 +260,7 @@ void DrivenEngine::drv_clear_lua_source() { rescan_lua_source_ = false; } -void DrivenEngine::drv_add_lua_source(eng::string_view fn, eng::string_view data) { +void DrivenEngine::drv_add_lua_source(std::string_view fn, std::string_view data) { if (lua_source_ == nullptr) { lua_source_.reset(new util::LuaSourceVec); } diff --git a/luprex/core/cpp/drivenengine.hpp b/luprex/core/cpp/drivenengine.hpp index 3b5fd9ad..a0a8ff47 100644 --- a/luprex/core/cpp/drivenengine.hpp +++ b/luprex/core/cpp/drivenengine.hpp @@ -343,7 +343,7 @@ public: // delete it. Closing a channel prevents it from showing up in // 'drv_list_channels'. // - void drv_notify_close(int chid, eng::string_view err); + void drv_notify_close(int chid, std::string_view err); // Notify the DrivenEngine that somebody connected to an incoming port. // This will cause the DrivenEngine to allocate a new channel and put the @@ -361,7 +361,7 @@ public: // Set the lua source code. The driver is expected to read the lua source // code and store it (using this function) once before invoking // - void drv_add_lua_source(eng::string_view fn, eng::string_view data); + void drv_add_lua_source(std::string_view fn, std::string_view data); // Invoke the init or update event. // diff --git a/luprex/core/cpp/driver-common.cpp b/luprex/core/cpp/driver-common.cpp index 19e97c64..fc0fd186 100644 --- a/luprex/core/cpp/driver-common.cpp +++ b/luprex/core/cpp/driver-common.cpp @@ -37,30 +37,30 @@ static void if_error_print_and_exit(const drv::string &str) { } } -static drv::string_view read_file(const char *fn, char *buf, int bufsize, drv::string &err) { +static std::string_view read_file(const char *fn, char *buf, int bufsize, drv::string &err) { FILE *f = fopen(fn, "r"); if (f == 0) { err = drv::string("cannot read file") + fn; buf[0] = 0; - return drv::string_view(buf, 0); + return std::string_view(buf, 0); } int nread = fread(buf, 1, bufsize, f); if (nread < 0) { err = drv::string("cannot read file: ") + fn; buf[0] = 0; - return drv::string_view(buf, 0); + return std::string_view(buf, 0); } if (nread == bufsize) { err = drv::string("file too large: ") + fn; buf[0] = 0; - return drv::string_view(buf, 0); + return std::string_view(buf, 0); } err = ""; - return drv::string_view(buf, nread); + return std::string_view(buf, nread); } -static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, drv::string_view require_cert) { +static SSL_CTX *new_ssl_context(bool server_cert, bool root_certs, std::string_view require_cert) { SSL_CTX *ctx = SSL_CTX_new(TLS_method()); SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); @@ -158,20 +158,20 @@ public: void handle_lua_source() { if (driven_->drv_get_rescan_lua_source()) { drv::string err; - drv::string_view ctrl = read_file("lua/control.lst", chbuf.get(), CHBUF_SIZE, err); + std::string_view ctrl = read_file("lua/control.lst", chbuf.get(), CHBUF_SIZE, err); if_error_print_and_exit(err); drv::vector names = drv::parse_control_lst(ctrl); driven_->drv_clear_lua_source(); for (const drv::string &str : names) { drv::string lfn = drv::string("lua/") + str; - drv::string_view data = read_file(lfn.c_str(), chbuf.get(), CHBUF_SIZE, err); + std::string_view data = read_file(lfn.c_str(), chbuf.get(), CHBUF_SIZE, err); if_error_print_and_exit(err); driven_->drv_add_lua_source(str, data); } } } - void close_channel(ChanInfo &chan, drv::string_view err) { + void close_channel(ChanInfo &chan, std::string_view err) { // std::cerr << "Closing channel " << chan.chid << std::endl; assert(chan.state != CHAN_INACTIVE); // Close and release the SSL channel. diff --git a/luprex/core/cpp/driver-linux.cpp b/luprex/core/cpp/driver-linux.cpp index 4faa5ed6..00be1223 100644 --- a/luprex/core/cpp/driver-linux.cpp +++ b/luprex/core/cpp/driver-linux.cpp @@ -73,7 +73,7 @@ static void enable_tty_raw() { assert(status >= 0); } -static SOCKET open_connection(drv::string_view target, drv::string &err) { +static SOCKET open_connection(std::string_view target, drv::string &err) { struct addrinfo *addrs = nullptr; struct addrinfo *goodaddr = nullptr; struct addrinfo hints; diff --git a/luprex/core/cpp/driver-mingw.cpp b/luprex/core/cpp/driver-mingw.cpp index de15c385..d9e9d3d7 100644 --- a/luprex/core/cpp/driver-mingw.cpp +++ b/luprex/core/cpp/driver-mingw.cpp @@ -59,11 +59,11 @@ static PADDRINFOA find_good_addr(PADDRINFOA addrinfo) { return nullptr; } -static SOCKET open_connection(drv::string_view target, drv::string &err) { +static SOCKET open_connection(std::string_view target, drv::string &err) { PADDRINFOA addrs = nullptr; PADDRINFOA goodaddr = nullptr; SOCKET sock = INVALID_SOCKET; - drv::string_view host, port; + std::string_view host, port; err.clear(); util::split_host_port(target, host, port); diff --git a/luprex/core/cpp/driver-util.cpp b/luprex/core/cpp/driver-util.cpp index 32d5501c..c53a0bb9 100644 --- a/luprex/core/cpp/driver-util.cpp +++ b/luprex/core/cpp/driver-util.cpp @@ -1,17 +1,19 @@ #include "wrap-string.hpp" -#include "wrap-string-view.hpp" #include "wrap-vector.hpp" +#include + #include "driver-util.hpp" #include "luastack.hpp" #include "util.hpp" + namespace drv { -void split_host_port(drv::string_view target, drv::string &host, drv::string &port) { +void split_host_port(std::string_view target, drv::string &host, drv::string &port) { size_t lastcolon = target.rfind(':'); - if (lastcolon == drv::string_view::npos) { + if (lastcolon == std::string_view::npos) { host = ""; port = ""; return; } host = target.substr(0, lastcolon); @@ -21,11 +23,11 @@ void split_host_port(drv::string_view target, drv::string &host, drv::string &po } } -drv::vector parse_control_lst(drv::string_view ctrl) { +drv::vector parse_control_lst(std::string_view ctrl) { drv::vector result; while (!ctrl.empty()) { - drv::string_view line = util::sv_read_line(ctrl); - drv::string_view trimmed = util::sv_trim(line); + std::string_view line = util::sv_read_line(ctrl); + std::string_view trimmed = util::sv_trim(line); if ((trimmed.size() > 0) && (trimmed[0] != '#')) { result.emplace_back(trimmed); } diff --git a/luprex/core/cpp/driver-util.hpp b/luprex/core/cpp/driver-util.hpp index aeef97e7..3e27e204 100644 --- a/luprex/core/cpp/driver-util.hpp +++ b/luprex/core/cpp/driver-util.hpp @@ -3,14 +3,15 @@ #define DRIVER_UTIL_HPP #include "wrap-string.hpp" -#include "wrap-string-view.hpp" #include "wrap-vector.hpp" +#include + namespace drv { -void split_host_port(drv::string_view target, drv::string &host, drv::string &port); +void split_host_port(std::string_view target, drv::string &host, drv::string &port); -drv::vector parse_control_lst(drv::string_view ctrl); +drv::vector parse_control_lst(std::string_view ctrl); } diff --git a/luprex/core/cpp/util.cpp b/luprex/core/cpp/util.cpp index b61d0750..5b4e8930 100644 --- a/luprex/core/cpp/util.cpp +++ b/luprex/core/cpp/util.cpp @@ -271,25 +271,25 @@ double strtodouble(const eng::string &value) { } } -eng::string_view sv_ltrim(eng::string_view v) { +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 eng::string_view(b, e-b); + return std::string_view(b, e-b); } -eng::string_view sv_rtrim(eng::string_view v) { +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 eng::string_view(b, e-b); + return std::string_view(b, e-b); } -eng::string_view sv_trim(eng::string_view v) { +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]))) { @@ -298,25 +298,25 @@ eng::string_view sv_trim(eng::string_view v) { while ((e > b) && (std::isspace(e[-1]))) { e--; } - return eng::string_view(b, e-b); + return std::string_view(b, e-b); } -eng::string ltrim(eng::string_view v) { +eng::string ltrim(std::string_view v) { return eng::string(sv_ltrim(v)); } -eng::string rtrim(eng::string_view v) { +eng::string rtrim(std::string_view v) { return eng::string(sv_rtrim(v)); } -eng::string trim(eng::string_view v) { +eng::string trim(std::string_view v) { return eng::string(sv_trim(v)); } -eng::string_view sv_read_line(eng::string_view &source) { +std::string_view sv_read_line(std::string_view &source) { size_t pos = source.find('\n'); - eng::string_view result; - if (pos == eng::string_view::npos) { + std::string_view result; + if (pos == std::string_view::npos) { result = source; source = ""; } else { @@ -449,10 +449,10 @@ LuaDefine(unittests_util, "", "some unit tests") { LuaAssert(L, util::trim("") == ""); // Test sv_read_line - eng::string_view v = "foo\nbar\r\n"; - eng::string_view v1 = util::sv_read_line(v); - eng::string_view v2 = util::sv_read_line(v); - eng::string_view v3 = util::sv_read_line(v); + 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, ""); diff --git a/luprex/core/cpp/util.hpp b/luprex/core/cpp/util.hpp index b1f00bf0..8d3f6218 100644 --- a/luprex/core/cpp/util.hpp +++ b/luprex/core/cpp/util.hpp @@ -2,7 +2,6 @@ #define UTIL_HPP #include "wrap-string.hpp" -#include "wrap-string-view.hpp" #include "wrap-set.hpp" #include "wrap-map.hpp" #include "wrap-algorithm.hpp" @@ -11,6 +10,8 @@ #include "wrap-memory.hpp" #include "wrap-utility.hpp" +#include + #include "luastack.hpp" #include "spookyv2.hpp" @@ -101,17 +102,17 @@ int64_t strtoint(const eng::string &value, int64_t errval); double strtodouble(const eng::string &value); // Trim a string_view -eng::string_view sv_ltrim(eng::string_view v); -eng::string_view sv_rtrim(eng::string_view v); -eng::string_view sv_trim(eng::string_view v); +std::string_view sv_ltrim(std::string_view v); +std::string_view sv_rtrim(std::string_view v); +std::string_view sv_trim(std::string_view v); // Trim strings: left end, right end, both ends. -eng::string ltrim(eng::string_view s); -eng::string rtrim(eng::string_view s); -eng::string trim(eng::string_view s); +eng::string ltrim(std::string_view s); +eng::string rtrim(std::string_view s); +eng::string trim(std::string_view s); // Read a line from a string_view -eng::string_view sv_read_line(eng::string_view &source); +std::string_view sv_read_line(std::string_view &source); // Calculate distance between two points double distance_squared(double x1, double y1, double x2, double y2); diff --git a/luprex/core/wrap/wrap-string-view.hpp b/luprex/core/wrap/wrap-string-view.hpp deleted file mode 100644 index 98ff780e..00000000 --- a/luprex/core/wrap/wrap-string-view.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef WRAP_STRING_VIEW_HPP -#define WRAP_STRING_VIEW_HPP - -#include "two-mallocs.hpp" -#include - -namespace eng { -template> -using basic_string_view = std::basic_string_view; -using string_view = basic_string_view; -} // namespace eng - -namespace drv { -template> -using basic_string_view = std::basic_string_view; -using string_view = basic_string_view; -} // namespace drv - -#endif // WRAP_STRING_VIEW_HPP