AuroraRuntime/Include/Aurora/Parse/LineParser.hpp
2021-09-06 11:58:08 +01:00

71 lines
1.8 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LineParser.hpp
Date: 2021-6-9
Author: Reece
***/
#pragma once
namespace Aurora::Parse
{
static void SplitNewlines(const AuString &in, std::function<void(const AuString &)> lineCallback)
{
AuMach index = 0, startIdx = 0;
while ((index = in.find("\n", startIdx)) != AuString::npos)
{
auto line = in.substr(startIdx, index - startIdx);
startIdx = index + 1;
if (line[line.size() - 1] == '\r')
{
line.pop_back();
}
lineCallback(line);
if (startIdx >= in.size())
{
break;
}
}
lineCallback(in.substr(startIdx));
}
static AuList<AuString> SplitString(const AuString &in, AuUInt16 characters)
{
AuList<AuString> ret;
for (auto i = 0u; i < in.size(); i += characters)
{
auto start = i;
auto end = std::min(AuUInt32(in.size()), AuUInt32(i + characters));
auto len = end - start;
ret.push_back(in.substr(start, len));
}
return ret;
}
static AuString SplitStringDelm(const AuString &in, const AuString &delm, AuUInt16 characters)
{
AuString ret;
ret.reserve(in.size());
for (auto i = 0u; i < in.size(); i += characters)
{
AuUInt32 start, end, len;
if (i != 0)
{
ret.insert(ret.size(), delm);
}
start = i;
end = std::min(AuUInt32(in.size()), AuUInt32(i + characters));
len = end - start;
ret.insert(ret.size(), in.substr(start, len));
}
return ret;
}
}