trivial parser is working

This commit is contained in:
Clément Le Bihan
2021-06-17 01:48:49 +02:00
parent fbe36b2323
commit ed9ae78dd6
6 changed files with 58 additions and 19 deletions
+16 -6
View File
@@ -6,46 +6,56 @@
#include <cctype>
#include <locale>
#include <string>
#include <regex>
#include "Utils.hpp"
namespace BBM
{
inline void Utils::lTrim(std::string &s)
void Utils::lTrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
inline void Utils::rTrim(std::string &s)
void Utils::rTrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
inline void Utils::trim(std::string &s)
void Utils::trim(std::string &s)
{
lTrim(s);
rTrim(s);
}
inline std::string Utils::lTrimCopy(std::string s)
std::string Utils::lTrimCopy(std::string s)
{
lTrim(s);
return s;
}
inline std::string Utils::rTrimCopy(std::string s)
std::string Utils::rTrimCopy(std::string s)
{
rTrim(s);
return s;
}
inline std::string Utils::trimCopy(std::string s)
std::string Utils::trimCopy(std::string s)
{
trim(s);
return s;
}
int Utils::findFrequency(const std::string &s, const std::string &pattern)
{
std::regex c(pattern);
std::smatch m;
ptrdiff_t numberOfMatches = std::distance(std::sregex_iterator(s.begin(), s.end(), c), std::sregex_iterator());
return static_cast<int>(numberOfMatches);
}
}
+9 -6
View File
@@ -9,22 +9,25 @@ namespace BBM
struct Utils
{
//! @brief trim left end
static inline void lTrim(std::string &s);
static void lTrim(std::string &s);
//! @brief trim right end
static inline void rTrim(std::string &s);
static void rTrim(std::string &s);
//! @brief trim from both ends
static inline void trim(std::string &s);
static void trim(std::string &s);
//! @brief trim left end (copying)
static inline std::string lTrimCopy(std::string s);
static std::string lTrimCopy(std::string s);
//! @brief trim right end (copying)
static inline std::string rTrimCopy(std::string s);
static std::string rTrimCopy(std::string s);
//! @brief trim from both ends (copying)
static inline std::string trimCopy(std::string s);
static std::string trimCopy(std::string s);
//! @brief find the frequency of a substring in a string
static int findFrequency(const std::string &s, const std::string &pattern);
};