Jamie Reece Wilson
d99c1032da
[*] Harden SplitString & SplitStringDelm, do not splice UTF8 sequences
98 lines
2.7 KiB
C++
98 lines
2.7 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 AuROString SplitNewlines(const AuROString &in,
|
|
AuConsumer<const AuROString &> lineCallback,
|
|
bool bReturnRemaining = false)
|
|
{
|
|
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 = line.RemoveSuffix(1);
|
|
}
|
|
|
|
lineCallback(line);
|
|
|
|
if (startIdx >= in.Size())
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (bReturnRemaining)
|
|
{
|
|
return in.Substr(startIdx);
|
|
}
|
|
else
|
|
{
|
|
lineCallback(in.Substr(startIdx));
|
|
return {};
|
|
}
|
|
}
|
|
|
|
static AuList<AuROString> SplitString(const AuROString &in, AuUInt16 characters)
|
|
{
|
|
AuList<AuROString> ret;
|
|
for (AuUInt i = 0u; i < in.Size(); )
|
|
{
|
|
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));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static AuString SplitStringDelm(const AuROString &in, const AuROString &delm, AuUInt16 characters)
|
|
{
|
|
AuString ret;
|
|
ret.reserve(in.Size());
|
|
for (AuUInt i = 0u; i < in.Size(); )
|
|
{
|
|
AuUInt start, end, len;
|
|
|
|
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;
|
|
}
|
|
len = end - start;
|
|
i += len;
|
|
|
|
ret.insert(ret.size(), in.Substr(start, len));
|
|
}
|
|
return ret;
|
|
}
|
|
} |