chore: trim csv reader values

This commit is contained in:
Jan 2024-10-06 17:10:50 +02:00
parent d814fe7b95
commit d2b95b4ebe
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
3 changed files with 53 additions and 6 deletions

View File

@ -1,5 +1,7 @@
#include "CsvStream.h" #include "CsvStream.h"
#include "Utils/StringUtils.h"
#include <cstdlib> #include <cstdlib>
#include <sstream> #include <sstream>
@ -110,13 +112,17 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
auto c = m_stream.get(); auto c = m_stream.get();
const auto isEof = c == EOF; const auto isEof = c == EOF;
std::ostringstream col; std::ostringstream col;
auto content = false;
while (c != EOF) while (c != EOF)
{ {
if (c == CSV_SEPARATOR) if (c == CSV_SEPARATOR)
{ {
cb(col.str()); auto value = col.str();
utils::StringTrimR(value);
cb(std::move(value));
col.clear(); col.clear();
col.str(std::string()); col.str(std::string());
content = false;
} }
else if (c == '\r') else if (c == '\r')
{ {
@ -129,8 +135,14 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
{ {
break; break;
} }
else if (isspace(c))
{
if (content)
col << static_cast<char>(c);
}
else else
{ {
content = true;
col << static_cast<char>(c); col << static_cast<char>(c);
} }
@ -139,7 +151,9 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
if (!isEof) if (!isEof)
{ {
cb(col.str()); auto value = col.str();
utils::StringTrimR(value);
cb(std::move(value));
} }
return !isEof; return !isEof;

View File

@ -1,6 +1,7 @@
#include "StringUtils.h" #include "StringUtils.h"
#include <iostream> #include <algorithm>
#include <cctype>
#include <sstream> #include <sstream>
namespace utils namespace utils
@ -102,13 +103,41 @@ namespace utils
c = static_cast<char>(toupper(static_cast<unsigned char>(c))); c = static_cast<char>(toupper(static_cast<unsigned char>(c)));
} }
std::vector<std::string> StringSplit(const std::string& str, const char delim) void StringTrimL(std::string& str)
{
str.erase(str.begin(),
std::ranges::find_if(str,
[](const unsigned char ch)
{
return !std::isspace(ch);
}));
}
void StringTrimR(std::string& str)
{
str.erase(std::find_if(str.rbegin(),
str.rend(),
[](const unsigned char ch)
{
return !std::isspace(ch);
})
.base(),
str.end());
}
void StringTrim(std::string& str)
{
StringTrimR(str);
StringTrimL(str);
}
std::vector<std::string> StringSplit(const std::string& str, const char delimiter)
{ {
std::vector<std::string> strings; std::vector<std::string> strings;
std::istringstream stream(str); std::istringstream stream(str);
std::string s; std::string s;
while (std::getline(stream, s, delim)) while (std::getline(stream, s, delimiter))
{ {
strings.emplace_back(std::move(s)); strings.emplace_back(std::move(s));
} }

View File

@ -16,5 +16,9 @@ namespace utils
void MakeStringLowerCase(std::string& str); void MakeStringLowerCase(std::string& str);
void MakeStringUpperCase(std::string& str); void MakeStringUpperCase(std::string& str);
std::vector<std::string> StringSplit(const std::string& str, const char delim); void StringTrimL(std::string& str);
void StringTrimR(std::string& str);
void StringTrim(std::string& str);
std::vector<std::string> StringSplit(const std::string& str, char delimiter);
} // namespace utils } // namespace utils