Browse Source

add operator""_CRLF() to ease definition of NET-ASCII/NET-Unicode test data strings.

master
roker 1 year ago
parent
commit
6f4ac9e572
2 changed files with 45 additions and 0 deletions
  1. +26
    -0
      src/crlf.cc
  2. +19
    -0
      src/crlf.hh

+ 26
- 0
src/crlf.cc View File

@ -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

+ 19
- 0
src/crlf.hh View File

@ -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 <string>
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

Loading…
Cancel
Save