mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
chore: trim csv reader values
This commit is contained in:
parent
d814fe7b95
commit
d2b95b4ebe
@ -1,5 +1,7 @@
|
||||
#include "CsvStream.h"
|
||||
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
||||
@ -110,13 +112,17 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
|
||||
auto c = m_stream.get();
|
||||
const auto isEof = c == EOF;
|
||||
std::ostringstream col;
|
||||
auto content = false;
|
||||
while (c != EOF)
|
||||
{
|
||||
if (c == CSV_SEPARATOR)
|
||||
{
|
||||
cb(col.str());
|
||||
auto value = col.str();
|
||||
utils::StringTrimR(value);
|
||||
cb(std::move(value));
|
||||
col.clear();
|
||||
col.str(std::string());
|
||||
content = false;
|
||||
}
|
||||
else if (c == '\r')
|
||||
{
|
||||
@ -129,8 +135,14 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (isspace(c))
|
||||
{
|
||||
if (content)
|
||||
col << static_cast<char>(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
content = true;
|
||||
col << static_cast<char>(c);
|
||||
}
|
||||
|
||||
@ -139,7 +151,9 @@ bool CsvInputStream::EmitNextRow(const std::function<void(std::string)>& cb) con
|
||||
|
||||
if (!isEof)
|
||||
{
|
||||
cb(col.str());
|
||||
auto value = col.str();
|
||||
utils::StringTrimR(value);
|
||||
cb(std::move(value));
|
||||
}
|
||||
|
||||
return !isEof;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "StringUtils.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace utils
|
||||
@ -102,13 +103,41 @@ namespace utils
|
||||
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::istringstream stream(str);
|
||||
|
||||
std::string s;
|
||||
while (std::getline(stream, s, delim))
|
||||
while (std::getline(stream, s, delimiter))
|
||||
{
|
||||
strings.emplace_back(std::move(s));
|
||||
}
|
||||
|
@ -16,5 +16,9 @@ namespace utils
|
||||
void MakeStringLowerCase(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
|
||||
|
Loading…
x
Reference in New Issue
Block a user