AuroraRuntime/Include/Aurora/Parse/LineParser.hpp

71 lines
1.8 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 void SplitNewlines(const AuString &in, std::function<void(const AuString &)> lineCallback)
{
AuMach index = 0, startIdx = 0;
2021-06-27 21:25:29 +00:00
2021-06-30 09:28:28 +00:00
while ((index = in.find("\n", startIdx)) != AuString::npos)
2021-06-27 21:25:29 +00:00
{
2021-06-30 09:28:28 +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')
{
line.pop_back();
}
lineCallback(line);
2021-09-06 10:58:08 +00:00
if (startIdx >= in.size())
{
break;
}
2021-06-27 21:25:29 +00:00
}
lineCallback(in.substr(startIdx));
2021-06-27 21:25:29 +00:00
}
2021-09-06 10:58:08 +00:00
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;
}
2021-06-27 21:25:29 +00:00
}