6#include "Stroika/Foundation/Containers/Common.h"
16 template <IPossibleCharacterRepresentation T>
17 [[deprecated (
"Since Stroika v3.0d7 - use kEOL")]]
inline
18#if __cpp_constexpr >= 202211L
25 if constexpr (qStroika_Foundation_Common_Platform_Windows) {
26 static constexpr T kResult_[] = {
'\r',
'\n',
'\0'};
29 else if constexpr (qStroika_Foundation_Common_Platform_POSIX) {
30 static constexpr T kResult_[] = {
'\n',
'\0'};
43 template <
typename CHAR>
44 void AssureHasLineTermination (basic_string<CHAR>* text)
47 const CHAR* eol = kEOL<CHAR>;
48 size_t eolLen = CString::Length (eol);
49 size_t len = text->length ();
52 already = (text->compare (len - eolLen, eolLen, eol) == 0);
64 template <
typename TCHAR>
65 size_t CRLFToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
69 TCHAR* outPtr = outBuf;
70 for (
size_t i = 1; i <= srcTextBytes; ++i) {
71 TCHAR c = srcText[i - 1];
76 if (i < srcTextBytes and srcText[i] ==
'\n') {
83 size_t nBytes = outPtr - outBuf;
84 Assert (nBytes <= outBufSize);
87 template <
typename TCHAR>
88 inline void CRLFToNL (basic_string<TCHAR>* text)
90 size_t origLen = text->length ();
92 Assert (newLen <= origLen);
93 text->resize (newLen);
95 template <
typename TCHAR>
96 inline basic_string<TCHAR>
CRLFToNL (
const basic_string<TCHAR>& text)
98 basic_string<TCHAR> r = text;
108 template <
typename TCHAR>
109 size_t NLToCRLF (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
111 Require (srcText != outBuf);
113 TCHAR* outPtr = outBuf;
114 bool prevCharWasCR =
false;
115 for (
const TCHAR* i = srcText; i < srcText + srcTextBytes; ++i) {
116 Assert (outPtr < outBuf + outBufSize);
118 if (*i ==
'\n' and not prevCharWasCR) {
121 Assert (outPtr < outBuf + outBufSize);
123 prevCharWasCR == (*i ==
'\r');
125 size_t nBytes = outPtr - outBuf;
126 Assert (nBytes <= outBufSize);
129 template <
typename TCHAR>
130 inline basic_string<TCHAR>
NLToCRLF (
const basic_string<TCHAR>& text)
132 size_t outBufSize = (text.length () + 1) * 2;
134 size_t newSize = NLToCRLF<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
135 Assert (newSize < outBufSize);
136 outBuf[newSize] =
'\0';
137 return basic_string<TCHAR> (outBuf);
145 template <
typename TCHAR>
146 size_t NativeToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
148 TCHAR* outPtr = outBuf;
149 for (
size_t i = 1; i <= srcTextBytes; ++i) {
150#if qStroika_Foundation_Common_Platform_MacOS
151 TCHAR c = (srcText[i - 1] ==
'\r') ?
'\n' : srcText[i - 1];
152#elif qStroika_Foundation_Common_Platform_Windows
153 TCHAR c = srcText[i - 1];
158 if (i < srcTextBytes and srcText[i] ==
'\n') {
164 TCHAR c = srcText[i - 1];
168 size_t nBytes = outPtr - outBuf;
169 Assert (nBytes <= outBufSize);
172 template <
typename TCHAR>
173 inline basic_string<TCHAR> NativeToNL (
const basic_string<TCHAR>& text)
175 size_t outBufSize = text.length () + 1;
176 Memory::StackBuffer<TCHAR> outBuf{Memory::eUninitialized, outBufSize};
177 size_t newSize = NativeToNL<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
178 Assert (newSize < outBufSize);
179 outBuf[newSize] =
'\0';
180 return basic_string<TCHAR> (outBuf);
188 template <
typename TCHAR>
189 size_t NLToNative (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
191 Require (srcText != outBuf);
193 TCHAR* outPtr = outBuf;
194 for (
size_t i = 1; i <= srcTextBytes; ++i) {
195 Assert (outPtr < outBuf + outBufSize);
196#if defined(macintosh)
197 TCHAR c = (srcText[i - 1] ==
'\n') ?
'\r' : srcText[i - 1];
198#elif qStroika_Foundation_Common_Platform_Windows
199 TCHAR c = srcText[i - 1];
205 TCHAR c = srcText[i - 1];
209 size_t nBytes = outPtr - outBuf;
210 Assert (nBytes <= outBufSize);
213 template <
typename TCHAR>
214 inline basic_string<TCHAR> NLToNative (
const basic_string<TCHAR>& text)
216 size_t outBufSize = (text.length () + 1) * 2;
217 Memory::StackBuffer<TCHAR> outBuf{Memory::eUninitialized, outBufSize};
218 size_t newSize = NLToNative<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
219 Assert (newSize < outBufSize);
220 outBuf[newSize] =
'\0';
221 return basic_string<TCHAR> (outBuf);
229 template <
typename TCHAR>
230 size_t NormalizeTextToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
232 Require (srcTextBytes == 0 or srcText !=
nullptr);
233 Require (outBufSize == 0 or outBuf !=
nullptr);
237 TCHAR* outPtr = outBuf;
238 for (
size_t i = 0; i < srcTextBytes; ++i) {
239 TCHAR c = srcText[i];
243 if (i + 1 < srcTextBytes and srcText[i + 1] ==
'\n') {
248 Assert (outPtr < outBuf + outBufSize);
251 size_t nBytes = outPtr - outBuf;
252 Assert (nBytes <= outBufSize);
255 template <
typename TCHAR>
259 size_t origLen = text->length ();
262 Assert (newLen <= origLen);
263 text->resize (newLen);
265 template <
typename TCHAR>
268 basic_string<TCHAR> r = text;
#define AssertNotImplemented()
#define RequireNotNull(p)
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
size_t NormalizeTextToNL(const TCHAR *srcText, size_t srcTextBytes, TCHAR *outBuf, size_t outBufSize)
size_t NLToCRLF(const TCHAR *srcText, size_t srcTextBytes, TCHAR *outBuf, size_t outBufSize)
Convert the argument srcText buffer from NL format line endings, to CRLF format line endings....
size_t CRLFToNL(const TCHAR *srcText, size_t srcTextBytes, TCHAR *outBuf, size_t outBufSize)
Convert the argument srcText buffer from CRLF format line endings, to NL format line endings.
CONTAINER::value_type * Start(CONTAINER &c)
For a contiguous container (such as a vector or basic_string) - find the pointer to the start of the ...