AuroraRuntime/Include/Aurora/Parse/LineParser.hpp

98 lines
2.7 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
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 AuROString SplitNewlines(const AuROString &in,
AuConsumer<const AuROString &> lineCallback,
bool bReturnRemaining = false)
2021-06-27 21:25:29 +00:00
{
AuMach index = 0, startIdx = 0;
2021-06-27 21:25:29 +00:00
while ((index = in.Find("\n", startIdx)) != AuString::npos)
2021-06-27 21:25:29 +00:00
{
auto line = in.Substr(startIdx, index - startIdx);
startIdx = index + 1;
2021-06-27 21:25:29 +00:00
if (line[line.Size() - 1] == '\r')
2021-06-27 21:25:29 +00:00
{
line = line.RemoveSuffix(1);
2021-06-27 21:25:29 +00:00
}
lineCallback(line);
if (startIdx >= in.Size())
2021-09-06 10:58:08 +00:00
{
break;
}
2021-06-27 21:25:29 +00:00
}
if (bReturnRemaining)
{
return in.Substr(startIdx);
}
else
{
lineCallback(in.Substr(startIdx));
return {};
}
}
static AuList<AuROString> SplitString(const AuROString &in, AuUInt16 characters)
2021-09-06 10:58:08 +00:00
{
AuList<AuROString> ret;
for (AuUInt i = 0u; i < in.Size(); )
2021-09-06 10:58:08 +00:00
{
AuUInt start, end, len;
start = i;
end = AuMin(AuUInt(in.size()), AuUInt(i + characters));
if (end - 1)
{
auto uLastCodepointIndex = AuCodepointsFindPreviousValidByteOffsetFromByteOffset(in, end);
auto uLastByteOffset = AuCodepointsNextLength(in.Substr(uLastCodepointIndex));
end = uLastCodepointIndex + uLastByteOffset;
}
len = end - start;
i += len;
ret.push_back(in.Substr(start, len));
2021-09-06 10:58:08 +00:00
}
return ret;
}
static AuString SplitStringDelm(const AuROString &in, const AuROString &delm, AuUInt16 characters)
2021-09-06 10:58:08 +00:00
{
AuString ret;
ret.reserve(in.Size());
for (AuUInt i = 0u; i < in.Size(); )
2021-09-06 10:58:08 +00:00
{
AuUInt start, end, len;
2021-09-06 10:58:08 +00:00
if (i != 0)
{
ret.insert(ret.size(), delm);
}
start = i;
end = AuMin(AuUInt(in.Size()), AuUInt(i + characters));
if (end - 1)
{
auto uLastCodepointIndex = AuCodepointsFindPreviousValidByteOffsetFromByteOffset(in, end);
auto uLastByteOffset = AuCodepointsNextLength(in.Substr(uLastCodepointIndex));
end = uLastCodepointIndex + uLastByteOffset;
}
2021-09-06 10:58:08 +00:00
len = end - start;
i += len;
2021-09-06 10:58:08 +00:00
ret.insert(ret.size(), in.Substr(start, len));
2021-09-06 10:58:08 +00:00
}
return ret;
}
2021-06-27 21:25:29 +00:00
}