diff --git a/src/crlf.cc b/src/crlf.cc new file mode 100644 index 0000000..1d9a3a9 --- /dev/null +++ b/src/crlf.cc @@ -0,0 +1,26 @@ +#include "crlf.hh" + +namespace pEp +{ + +std::string operator""_CRLF(const char* str, size_t length) +{ + static const std::string CRLF{"\r\n"}; + + std::string ret; + ret.reserve(length + ((length+29)/30) + 2 ); // rough guess for average line length of 30. + const char* end = str + length; + + // N.B.: Loop could be more optimized, but not necessary because it is only used for string literals. + for(; str != end; ++str) + { + if(*str == '\n') + ret += CRLF; + else + ret += *str; + } + + return ret; +} + +} // end of namespace pEp diff --git a/src/crlf.hh b/src/crlf.hh new file mode 100644 index 0000000..8b1b383 --- /dev/null +++ b/src/crlf.hh @@ -0,0 +1,19 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPDATATYPES_CRLF_HH +#define LIBPEPDATATYPES_CRLF_HH + +#include + +namespace pEp +{ + // creates a string where \n ("linefeed" a.k.a. LF) are replaced + // by \r\n ("carriage return + linefeed" a.k.a. CRLF). + // Useful to define strings in NET-ASCII or Net-Unicode (RFC5198) format + // from string literals. + std::string operator""_CRLF(const char* str, size_t length); + +} // end of namespace pEp + +#endif // LIBPEPDATATYPES_CRLF_HH