/*** 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 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 SplitString(const AuROString &in, AuUInt16 characters) { AuList 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; } }