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
24 if constexpr (qStroika_Foundation_Common_Platform_Windows) {
25 static constexpr T kResult_[] = {
'\r',
'\n',
'\0'};
28 else if constexpr (qStroika_Foundation_Common_Platform_POSIX) {
29 static constexpr T kResult_[] = {
'\n',
'\0'};
42 template <
typename CHAR>
43 void AssureHasLineTermination (basic_string<CHAR>* text)
46 const CHAR* eol = kEOL<CHAR>;
47 size_t eolLen = CString::Length (eol);
48 size_t len = text->length ();
51 already = (text->compare (len - eolLen, eolLen, eol) == 0);
63 template <
typename TCHAR>
64 size_t CRLFToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
68 TCHAR* outPtr = outBuf;
69 for (
size_t i = 1; i <= srcTextBytes; ++i) {
70 TCHAR c = srcText[i - 1];
75 if (i < srcTextBytes and srcText[i] ==
'\n') {
82 size_t nBytes = outPtr - outBuf;
83 Assert (nBytes <= outBufSize);
86 template <
typename TCHAR>
87 inline void CRLFToNL (basic_string<TCHAR>* text)
89 size_t origLen = text->length ();
91 Assert (newLen <= origLen);
92 text->resize (newLen);
94 template <
typename TCHAR>
95 inline basic_string<TCHAR>
CRLFToNL (
const basic_string<TCHAR>& text)
97 basic_string<TCHAR> r = text;
107 template <
typename TCHAR>
108 size_t NLToCRLF (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
110 Require (srcText != outBuf);
112 TCHAR* outPtr = outBuf;
113 bool prevCharWasCR =
false;
114 for (
const TCHAR* i = srcText; i < srcText + srcTextBytes; ++i) {
115 Assert (outPtr < outBuf + outBufSize);
117 if (*i ==
'\n' and not prevCharWasCR) {
120 Assert (outPtr < outBuf + outBufSize);
122 prevCharWasCR == (*i ==
'\r');
124 size_t nBytes = outPtr - outBuf;
125 Assert (nBytes <= outBufSize);
128 template <
typename TCHAR>
129 inline basic_string<TCHAR>
NLToCRLF (
const basic_string<TCHAR>& text)
131 size_t outBufSize = (text.length () + 1) * 2;
133 size_t newSize = NLToCRLF<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
134 Assert (newSize < outBufSize);
135 outBuf[newSize] =
'\0';
136 return basic_string<TCHAR> (outBuf);
144 template <
typename TCHAR>
145 size_t NativeToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
147 TCHAR* outPtr = outBuf;
148 for (
size_t i = 1; i <= srcTextBytes; ++i) {
149#if qStroika_Foundation_Common_Platform_MacOS
150 TCHAR c = (srcText[i - 1] ==
'\r') ?
'\n' : srcText[i - 1];
151#elif qStroika_Foundation_Common_Platform_Windows
152 TCHAR c = srcText[i - 1];
157 if (i < srcTextBytes and srcText[i] ==
'\n') {
163 TCHAR c = srcText[i - 1];
167 size_t nBytes = outPtr - outBuf;
168 Assert (nBytes <= outBufSize);
171 template <
typename TCHAR>
172 inline basic_string<TCHAR> NativeToNL (
const basic_string<TCHAR>& text)
174 size_t outBufSize = text.length () + 1;
175 Memory::StackBuffer<TCHAR> outBuf{Memory::eUninitialized, outBufSize};
176 size_t newSize = NativeToNL<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
177 Assert (newSize < outBufSize);
178 outBuf[newSize] =
'\0';
179 return basic_string<TCHAR> (outBuf);
187 template <
typename TCHAR>
188 size_t NLToNative (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
190 Require (srcText != outBuf);
192 TCHAR* outPtr = outBuf;
193 for (
size_t i = 1; i <= srcTextBytes; ++i) {
194 Assert (outPtr < outBuf + outBufSize);
195#if defined(macintosh)
196 TCHAR c = (srcText[i - 1] ==
'\n') ?
'\r' : srcText[i - 1];
197#elif qStroika_Foundation_Common_Platform_Windows
198 TCHAR c = srcText[i - 1];
204 TCHAR c = srcText[i - 1];
208 size_t nBytes = outPtr - outBuf;
209 Assert (nBytes <= outBufSize);
212 template <
typename TCHAR>
213 inline basic_string<TCHAR> NLToNative (
const basic_string<TCHAR>& text)
215 size_t outBufSize = (text.length () + 1) * 2;
216 Memory::StackBuffer<TCHAR> outBuf{Memory::eUninitialized, outBufSize};
217 size_t newSize = NLToNative<TCHAR> (
Containers::Start (text), text.length (), outBuf.begin (), outBufSize);
218 Assert (newSize < outBufSize);
219 outBuf[newSize] =
'\0';
220 return basic_string<TCHAR> (outBuf);
228 template <
typename TCHAR>
229 size_t NormalizeTextToNL (
const TCHAR* srcText,
size_t srcTextBytes, TCHAR* outBuf, [[maybe_unused]]
size_t outBufSize)
231 Require (srcTextBytes == 0 or srcText !=
nullptr);
232 Require (outBufSize == 0 or outBuf !=
nullptr);
236 TCHAR* outPtr = outBuf;
237 for (
size_t i = 0; i < srcTextBytes; ++i) {
238 TCHAR c = srcText[i];
242 if (i + 1 < srcTextBytes and srcText[i + 1] ==
'\n') {
247 Assert (outPtr < outBuf + outBufSize);
250 size_t nBytes = outPtr - outBuf;
251 Assert (nBytes <= outBufSize);
254 template <
typename TCHAR>
258 size_t origLen = text->length ();
261 Assert (newLen <= origLen);
262 text->resize (newLen);
264 template <
typename TCHAR>
267 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 ...