Class printable_string
Synopsis
#include "breeze/text/printable_string.hpp"
class printable_string
Description
A string with non-printable chars escaped.
Represents a string that prints to an ostream
without non-printable characters.
If a character has a corresponding simple-escape-sequence (such as \a
), it is displayed using that escape sequence. Otherwise, if it is in the range [0x20, 0x7e]
, it is displayed directly. Otherwise an escape sequence of the form \x...
is used. The goal is, for instance, to send the output to a log and then read it from another machine. The number of digits in any hexadecimal escape sequence is the ceiling of the quotient between CHAR_BIT
and 4
(they are left-padded with zeroes); note that this is different from character and string literals, where there's no limit to the number of hexadecimal digits, and that e.g. \0
is represented, when CHAR_BIT == 8
, as \x00
. In general, to read a printable_string
unambiguously you need to know the value of CHAR_BIT
on the source machine.
A printable_string
can be constructed "from a non-string" (i.e. from a null pointer): this is useful for debugging purposes. In this special case, only, the printable_string
outputs without quotes. Otherwise, it is always enclosed in double quotes and a double quote character in the string is escaped, as is usual in C++.
Instances of this class maintain a copy of the string, so the user does not have to worry about the lifetime of the object given to the constructor.
Methods
printable_string overload | ||
to_string |
Source
Lines 59-74 in breeze/text/printable_string.hpp.
class printable_string
{
public:
explicit printable_string( char const * ) ;
explicit printable_string( std::string const & ) ;
std::string to_string() const ;
private:
friend std::ostream &
operator <<( std::ostream & dest,
printable_string const & ) ;
maybe< std::string >
m_value ;
} ;