From 983891ec41b024ff15b5d1d3dfb69a724aa3c068 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 10 Oct 2023 16:39:45 +0200 Subject: [PATCH] string: add crlf2nl Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index 81fd4131d..ea0d3a8aa 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -862,6 +862,49 @@ namespace stdex return i; } + /// + /// Convert CRLF to LF + /// + /// \param[in] dst Destination string + /// \param[in] src Source string. Must not be dst.c_str(). + /// + template, class _Ax = std::allocator<_Elem>> + inline void crlf2nl(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &dst, _In_z_ const _Elem* src) + { + _Assume_(src); + _Assume_(src != dst.c_str()); + dst.clear(); + dst.reserve(strlen(src)); + for (size_t j = 0; src[j];) { + if (src[j] != '\r' || src[j + 1] != '\n') + dst += src[j++]; + else { + dst += '\n'; + j += 2; + } + } + } + + /// + /// Convert CRLF to LF + /// + /// \param[in] str String to convert + /// + template, class _Ax = std::allocator<_Elem>> + inline void crlf2nl(_Inout_ std::basic_string<_Elem, _Traits, _Ax>& str) + { + size_t i, j, n; + for (i = j = 0, n = str.size(); j < n;) { + if (str[j] != '\r' || str[j + 1] != '\n') + str[i++] = str[j++]; + else { + str[i++] = '\n'; + j += 2; + } + } + str.resize(i); + } + /// \cond internal template inline T_bin strtoint(