AuroraRuntime/Include/Aurora/Parse/LineParser.hpp

84 lines
2.1 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 AuString SplitNewlines(const AuString &in, AuFunction<void(const AuString &)> lineCallback, bool returnRemaining)
{
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;
}
}
if (returnRemaining)
{
return in.substr(startIdx);
}
else
{
lineCallback(in.substr(startIdx));
return {};
}
}
static void SplitNewlines(const AuString &in, AuFunction<void(const AuString &)> lineCallback)
{
SplitNewlines(in, lineCallback, false);
}
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 = AuMin(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 = AuMin(AuUInt32(in.size()), AuUInt32(i + characters));
len = end - start;
ret.insert(ret.size(), in.substr(start, len));
}
return ret;
}
}