Skip to content

Commit

Permalink
Rework string functions into functions and rework ShvPath to inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
syyyr committed Oct 21, 2024
1 parent 8ea5be7 commit ea7ed6d
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 225 deletions.
2 changes: 1 addition & 1 deletion libshvbroker/src/brokeraclnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ unsigned AccessAclNode::keyToRuleIndex(const std::string &key)
if(std::regex_search(key, color_match, color_regex)) {
if (color_match.size() > 1) {
bool ok;
unsigned ix = static_cast<unsigned>(shv::core::String::toInt(color_match[1], &ok));
unsigned ix = static_cast<unsigned>(shv::core::string::toInt(color_match[1], &ok));
if(ok)
return ix;
}
Expand Down
4 changes: 2 additions & 2 deletions libshvbroker/src/brokerapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ void BrokerApp::onClientLogin(int connection_id)

conn->setParent(client_app_node);
{
std::string mount_point = client_id_node->shvPath() + "/subscriptions";
std::string mount_point = client_id_node->shvPath().asString() + "/subscriptions";
auto *nd = new SubscriptionsNode(conn);
if(!m_nodesTree->mount(mount_point, nd))
SHV_EXCEPTION("Cannot mount connection subscription list to device tree, connection id: " + std::to_string(connection_id)
Expand All @@ -1010,7 +1010,7 @@ void BrokerApp::onClientLogin(int connection_id)
SHV_EXCEPTION("Cannot mount connection to device tree, connection id: " + std::to_string(connection_id));
connect(cli_nd, &ClientShvNode::destroyed, cli_nd->parentNode(), &shv::iotqt::node::ShvNode::deleteIfEmptyWithParents, Qt::QueuedConnection);
}
mount_point = cli_nd->shvPath();
mount_point = cli_nd->shvPath().asString();
QTimer::singleShot(0, this, [this, mount_point]() {
sendNotifyToSubscribers(mount_point, cp::Rpc::SIG_MOUNTED_CHANGED, "", true);
});
Expand Down
2 changes: 1 addition & 1 deletion libshvbroker/src/rpc/masterbrokerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ std::string MasterBrokerConnection::masterExportedToLocalPath(const std::string
static const std::string DIR_BROKER = cp::Rpc::DIR_BROKER;
if(shv::core::utils::ShvPath::startsWithPath(master_path, DIR_BROKER))
return master_path;
return shv::core::utils::ShvPath(m_exportedShvPath).appendPath(master_path);
return shv::core::utils::ShvPath(m_exportedShvPath).appendPath(master_path).asString();
}

std::string MasterBrokerConnection::localPathToMasterExported(const std::string &local_path) const
Expand Down
61 changes: 23 additions & 38 deletions libshvcore/include/shv/core/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,36 @@
#include <string>
#include <vector>

namespace shv::core {

class SHVCORE_DECL_EXPORT String : public std::string
{
using Super = std::string;
public:
namespace shv::core::string {
enum CaseSensitivity {CaseSensitive, CaseInsensitive};
enum SplitBehavior {KeepEmptyParts, SkipEmptyParts};

using Super::Super;
String();
String(const std::string &o);
String(std::string &&o);

static bool equal(const std::string &a, const std::string &b, String::CaseSensitivity case_sensitivity);
static std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, String::CaseSensitivity case_sensitivity = CaseSensitive);
static std::string::size_type indexOf(const std::string &haystack, char needle);
std::string::size_type indexOf(const std::string &needle, String::CaseSensitivity case_sensitivity = CaseSensitive) const;
std::string::size_type indexOf(char needle) const;
size_t lastIndexOf(char c) const;
SHVCORE_DECL_EXPORT bool equal(const std::string &a, const std::string &b, CaseSensitivity case_sensitivity);
SHVCORE_DECL_EXPORT std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, CaseSensitivity case_sensitivity = CaseSensitive);
SHVCORE_DECL_EXPORT std::string::size_type indexOf(const std::string &haystack, char needle);

static constexpr auto WhiteSpaceChars = " \t\n\r\f\v";
static std::string& rtrim(std::string& s, const char* t = WhiteSpaceChars);
static std::string& ltrim(std::string& s, const char* t = WhiteSpaceChars);
static std::string& trim(std::string& s, const char* t = WhiteSpaceChars);
constexpr auto WhiteSpaceChars = " \t\n\r\f\v";
SHVCORE_DECL_EXPORT std::string& rtrim(std::string& s, const char* t = WhiteSpaceChars);
SHVCORE_DECL_EXPORT std::string& ltrim(std::string& s, const char* t = WhiteSpaceChars);
SHVCORE_DECL_EXPORT std::string& trim(std::string& s, const char* t = WhiteSpaceChars);

std::string mid(size_t pos, size_t cnt = std::string::npos) const;
SHVCORE_DECL_EXPORT std::string mid(const std::string& str, size_t pos, size_t cnt = std::string::npos);

static std::pair<size_t, size_t> indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket);
static std::vector<std::string> split(const std::string &str, char delim, SplitBehavior split_behavior = SkipEmptyParts);
static std::string join(const std::vector<std::string> &lst, const std::string &delim);
static std::string join(const std::vector<std::string> &lst, char delim);
static std::string join(const std::vector<shv::core::StringView> &lst, char delim);
static int replace(std::string &str, const std::string &from, const std::string &to);
static int replace(std::string &str, const char from, const char to);
SHVCORE_DECL_EXPORT std::pair<size_t, size_t> indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket);
SHVCORE_DECL_EXPORT std::vector<std::string> split(const std::string &str, char delim, SplitBehavior split_behavior = SkipEmptyParts);
SHVCORE_DECL_EXPORT std::string join(const std::vector<std::string> &lst, const std::string &delim);
SHVCORE_DECL_EXPORT std::string join(const std::vector<std::string> &lst, char delim);
SHVCORE_DECL_EXPORT std::string join(const std::vector<shv::core::StringView> &lst, char delim);
SHVCORE_DECL_EXPORT int replace(std::string &str, const std::string &from, const std::string &to);
SHVCORE_DECL_EXPORT int replace(std::string &str, const char from, const char to);

static std::string& upper(std::string& s);
static std::string toUpper(const std::string& s);
static std::string& lower(std::string& s);
static std::string toLower(const std::string& s);
SHVCORE_DECL_EXPORT std::string& upper(std::string& s);
SHVCORE_DECL_EXPORT std::string toUpper(const std::string& s);
SHVCORE_DECL_EXPORT std::string& lower(std::string& s);
SHVCORE_DECL_EXPORT std::string toLower(const std::string& s);

template<typename T>
static std::string toString(T i, size_t width = 0, char fill_char = ' ')
std::string toString(T i, size_t width = 0, char fill_char = ' ')
{
std::ostringstream ss;
ss << i;
Expand All @@ -58,9 +45,7 @@ class SHVCORE_DECL_EXPORT String : public std::string
ret = fill_char + ret;
return ret;
}
static int toInt(const std::string &str, bool *ok = nullptr);
static double toDouble(const std::string &str, bool *ok);

};
SHVCORE_DECL_EXPORT int toInt(const std::string &str, bool *ok = nullptr);
SHVCORE_DECL_EXPORT double toDouble(const std::string &str, bool *ok);

}
11 changes: 6 additions & 5 deletions libshvcore/include/shv/core/utils/shvpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ SHVCORE_DECL_EXPORT bool startsWithPath(const std::string_view &str, const std::

}

class SHVCORE_DECL_EXPORT ShvPath : public shv::core::String
class SHVCORE_DECL_EXPORT ShvPath
{
using Super = shv::core::String;
public:
static constexpr auto SHV_PATH_QUOTE = '\'';
static constexpr auto SHV_PATH_DELIM = '/';
static constexpr auto SHV_PATH_METHOD_DELIM = ':';
public:
ShvPath();
ShvPath(shv::core::String &&o);
ShvPath(const shv::core::String &o);
using Super::Super;
ShvPath(std::string &&o);
ShvPath(const std::string &o);

const std::string &asString() const;

Expand Down Expand Up @@ -92,5 +90,8 @@ class SHVCORE_DECL_EXPORT ShvPath : public shv::core::String
}
}
}

private:
std::string m_str;
};
}
99 changes: 32 additions & 67 deletions libshvcore/src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@

#include <algorithm>

namespace shv::core {
namespace shv::core::string {

String::String() = default;

String::String(const std::string &o)
: Super(o)
{
}

String::String(std::string &&o)
: Super(o)
{
}

std::string::size_type String::indexOf(const std::string & str_haystack, const std::string &str_needle, String::CaseSensitivity case_sensitivity)
std::string::size_type indexOf(const std::string & str_haystack, const std::string &str_needle, CaseSensitivity case_sensitivity)
{
auto it = std::search(
str_haystack.begin(), str_haystack.end(),
Expand All @@ -29,62 +17,39 @@ std::string::size_type String::indexOf(const std::string & str_haystack, const s
return (it == str_haystack.end())? std::string::npos: static_cast<std::string::size_type>(it - str_haystack.begin());
}

std::string::size_type String::indexOf(const std::string &haystack, char needle)
std::string::size_type indexOf(const std::string &haystack, char needle)
{
for (std::string::size_type i = 0; i < haystack.length(); i++)
if(haystack[i] == needle)
return i;
return std::string::npos;
}

std::string::size_type String::indexOf(const std::string &needle, String::CaseSensitivity case_sensitivity) const
std::string mid(const std::string& str, size_t pos, size_t cnt)
{
return indexOf(*this, needle, case_sensitivity);
}

std::string::size_type String::indexOf(char needle) const
{
return indexOf(*this, needle);
}

size_t String::lastIndexOf(char c) const
{
if(empty())
return std::string::npos;
for (size_t i = size() - 1; ; i--) {
if(at(i) == c)
return i;
if(i == 0)
break;
}
return std::string::npos;
}

std::string String::mid(size_t pos, size_t cnt) const
{
if(pos < size())
return substr(pos, cnt);
if(pos < str.size())
return str.substr(pos, cnt);
return {};
}

std::string& String::rtrim(std::string& s, const char* t)
std::string& rtrim(std::string& s, const char* t)
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}

std::string& String::ltrim(std::string& s, const char* t)
std::string& ltrim(std::string& s, const char* t)
{
s.erase(0, s.find_first_not_of(t));
return s;
}

std::string& String::trim(std::string& s, const char* t)
std::string& trim(std::string& s, const char* t)
{
return ltrim(rtrim(s, t), t);
}

bool String::equal(std::string const& a, std::string const& b, String::CaseSensitivity case_sensitivity)
bool equal(std::string const& a, std::string const& b, CaseSensitivity case_sensitivity)
{
if (a.length() == b.length()) {
return std::equal(
Expand All @@ -99,24 +64,24 @@ bool String::equal(std::string const& a, std::string const& b, String::CaseSensi
return false;
}

std::vector<std::string> String::split(const std::string &str, char delim, SplitBehavior split_behavior)
std::vector<std::string> split(const std::string &str, char delim, SplitBehavior split_behavior)
{
using namespace std;
vector<string> ret;
vector<std::string> ret;
size_t pos = 0;
while(true) {
size_t pos2 = str.find_first_of(delim, pos);
string s = (pos2 == string::npos)? str.substr(pos): str.substr(pos, pos2 - pos);
std::string s = (pos2 == std::string::npos)? str.substr(pos): str.substr(pos, pos2 - pos);
if(split_behavior == KeepEmptyParts || !s.empty())
ret.push_back(s);
if(pos2 == string::npos)
if(pos2 == std::string::npos)
break;
pos = pos2 + 1;
}
return ret;
}

std::string String::join(const std::vector<std::string> &lst, const std::string &delim)
std::string join(const std::vector<std::string> &lst, const std::string &delim)
{
std::string ret;
for(const auto &s : lst) {
Expand All @@ -127,7 +92,7 @@ std::string String::join(const std::vector<std::string> &lst, const std::string
return ret;
}

std::string String::join(const std::vector<std::string> &lst, char delim)
std::string join(const std::vector<std::string> &lst, char delim)
{
std::string ret;
for(const auto &s : lst) {
Expand All @@ -138,7 +103,7 @@ std::string String::join(const std::vector<std::string> &lst, char delim)
return ret;
}

std::string String::join(const std::vector<StringView> &lst, char delim)
std::string join(const std::vector<StringView> &lst, char delim)
{
std::string ret;
for(const auto &s : lst) {
Expand All @@ -149,7 +114,7 @@ std::string String::join(const std::vector<StringView> &lst, char delim)
return ret;
}

int String::replace(std::string &str, const std::string &from, const std::string &to)
int replace(std::string &str, const std::string &from, const std::string &to)
{
int i = 0;
size_t pos = 0;
Expand All @@ -163,7 +128,7 @@ int String::replace(std::string &str, const std::string &from, const std::string
return i;
}

int String::replace(std::string& str, const char from, const char to)
int replace(std::string& str, const char from, const char to)
{
int n = 0;
for (char& i : str) {
Expand All @@ -175,33 +140,33 @@ int String::replace(std::string& str, const char from, const char to)
return n;
}

std::string &String::upper(std::string &s)
std::string &upper(std::string &s)
{
for (char& i : s)
i = static_cast<char>(std::toupper(i));
return s;
}

std::string String::toUpper(const std::string& s)
std::string toUpper(const std::string& s)
{
std::string ret(s);
return upper(ret);
}

std::string &String::lower(std::string &s)
std::string &lower(std::string &s)
{
for (char& i : s)
i = static_cast<char>(std::tolower(i));
return s;
}

std::string String::toLower(const std::string& s)
std::string toLower(const std::string& s)
{
std::string ret(s);
return lower(ret);
}

int String::toInt(const std::string &str, bool *ok)
int toInt(const std::string &str, bool *ok)
{
int ret = 0;
bool is_ok = false;
Expand All @@ -221,7 +186,7 @@ int String::toInt(const std::string &str, bool *ok)
return ret;
}

double String::toDouble(const std::string &str, bool *ok)
double toDouble(const std::string &str, bool *ok)
{
double ret = 0;
bool is_ok = false;
Expand All @@ -246,15 +211,15 @@ size_t find_str(const std::string &haystack, size_t begin_pos, size_t end_pos, c
{
using namespace std;
if(needle.empty())
return string::npos;
return std::string::npos;
if(begin_pos > haystack.size())
return string::npos;
return std::string::npos;
if(end_pos > haystack.size())
return string::npos;
return std::string::npos;
if(end_pos < begin_pos)
return string::npos;
return std::string::npos;
if(needle.size() > (end_pos - begin_pos))
return string::npos;
return std::string::npos;
auto pos1 = begin_pos;
auto pos2 = end_pos - needle.size() + 1;
while(pos1 < pos2) {
Expand All @@ -267,11 +232,11 @@ size_t find_str(const std::string &haystack, size_t begin_pos, size_t end_pos, c
return pos1;
++pos1;
}
return string::npos;
return std::string::npos;
}
}

std::pair<size_t, size_t> String::indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket)
std::pair<size_t, size_t> indexOfBrackets(const std::string &haystack, size_t begin_pos, size_t end_pos, const std::string &open_bracket, const std::string &close_bracket)
{
if(begin_pos + open_bracket.size() > haystack.size())
return std::pair<size_t, size_t>(std::string::npos, std::string::npos);
Expand Down
Loading

0 comments on commit ea7ed6d

Please sign in to comment.