Commit 1e4876b4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 410621dd
......@@ -174,6 +174,29 @@ bool has_prefix(const string &s, char prefix) {
return (s.size() >= 1 && s[0] == prefix);
}
bool has_suffix(const string &s, const string &suffix) {
return (s.size() >= suffix.size() &&
s.compare(s.size() - suffix.size(), suffix.size(), suffix));
}
bool has_suffix(const string &s, char suffix) {
return (s.size() >= 1 && s[s.size()-1] == suffix);
}
// XXX trim_prefix
string trim_suffix(const string &s, const string &suffix) {
if (!has_suffix(s, suffix))
return s;
return s.substr(0, s.size()-suffix.size());
}
string trim_suffix(const string &s, char suffix) {
if (!has_suffix(s, suffix))
return s;
return s.substr(0, s.size()-1);
}
vector<string> split(const string &s, char sep) {
vector<string> r;
int psep_prev=-1;
......
......@@ -132,6 +132,13 @@ namespace strings {
bool has_prefix(const string &s, const string &prefix);
bool has_prefix(const string &s, char prefix);
bool has_suffix(const string &s, const string &suffix);
bool has_suffix(const string &s, char suffix);
string trim_prefix(const string &s, const string &prefix);
string trim_prefix(const string &s, char prefix);
string trim_suffix(const string &s, const string &suffix);
string trim_suffix(const string &s, char suffix);
vector<string> split(const string &s, char sep);
} // strings::
......
......@@ -388,8 +388,10 @@ error rxPkt::from_string(const string &rx) {
auto sp = rx.find(' ');
if (sp == string::npos)
return fmt::errorf("invalid pkt: no SP");
if (!strings::has_suffix(rx, '\n'))
return fmt::errorf("invalid pkt: no LF");
string sid = rx.substr(0, sp);
string smsg = rx.substr(sp+1);
string smsg = strings::trim_suffix(rx.substr(sp+1), '\n');
error err;
tie(pkt.stream, err) = xstrconv::parseUint(sid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment