Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libnntpchan/sanitize.cpp

50 lines
1.2 KiB
C++
Raw Normal View History

2017-10-11 18:48:27 +05:00
#include <nntpchan/sanitize.hpp>
#include <algorithm>
#include <regex>
2017-10-11 18:48:27 +05:00
#include <cctype>
namespace nntpchan
{
2017-10-11 18:48:27 +05:00
std::string NNTPSanitizeLine(const std::string & str)
{
2017-10-11 18:48:27 +05:00
if(str == ".") return " .";
std::string sane;
sane += str;
const char ch = ' ';
std::replace_if(sane.begin(), sane.end(), [](unsigned char ch) -> bool { return iscntrl(ch); } , ch);
return sane;
}
std::string ToLower(const std::string & str)
{
std::string lower = str;
std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char ch) -> unsigned char { return std::tolower(ch); } );
return lower;
}
static const std::regex re_ValidMessageID("^<[a-zA-Z0-9$\\._]{2,128}@[a-zA-Z0-9\\-\\.]{2,63}>$");
bool IsValidMessageID(const std::string & msgid)
{
return std::regex_search(msgid, re_ValidMessageID) == 1;
}
2017-10-11 18:48:27 +05:00
static const std::regex re_ValidNewsgroup("^[a-zA-Z][a-zA-Z0-9.]{1,128}$");
bool IsValidNewsgroup(const std::string & msgid)
{
return std::regex_search(msgid, re_ValidNewsgroup) == 1;
}
2017-10-13 16:58:41 +05:00
std::string StripWhitespaces(const std::string & str)
{
std::string stripped;
for(const auto & ch : str)
if(!(std::isspace(ch)||std::iscntrl(ch)))
stripped += ch;
return stripped;
}
}