Move DebugChar into its own file

Change-Id: Ia40549e5d0b78ece8dd0722c3a5a032b9915f24b
This commit is contained in:
Rebecca Turner
2024-03-28 15:54:12 -07:00
parent 62332c1250
commit 5ec2efb686
7 changed files with 54 additions and 32 deletions
+22
View File
@@ -0,0 +1,22 @@
#include <boost/io/ios_state.hpp>
#include <iomanip>
#include <iostream>
#include "escape-char.hh"
namespace nix {
std::ostream & operator<<(std::ostream & s, MaybeHexEscapedChar c)
{
boost::io::ios_flags_saver _ifs(s);
if (isprint(c.c)) {
s << static_cast<char>(c.c);
} else {
s << "\\x" << std::hex << std::setfill('0') << std::setw(2)
<< (static_cast<unsigned int>(c.c) & 0xff);
}
return s;
}
} // namespace nix
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <ostream>
namespace nix {
/**
* A struct that prints a debug representation of a character, like `\x1f` for
* non-printable characters, or the character itself for other characters.
*
* Note that these are suitable for human readable output, but further care is
* necessary to include them in C++ strings to avoid running into adjacent
* hex-like characters. (`"puppy\x1bdoggy"` parses as `"puppy" "\x1bd" "oggy"`
* and errors because 0x1bd is too big for a `char`.)
*/
struct MaybeHexEscapedChar
{
char c;
};
std::ostream & operator<<(std::ostream & s, MaybeHexEscapedChar c);
} // namespace nix
+2
View File
@@ -8,6 +8,7 @@ libutil_sources = files(
'config.cc',
'english.cc',
'error.cc',
'escape-char.cc',
'exit.cc',
'experimental-features.cc',
'filesystem.cc',
@@ -49,6 +50,7 @@ libutil_headers = files(
'config.hh',
'english.hh',
'error.hh',
'escape-char.hh',
'exit.hh',
'experimental-features.hh',
'experimental-features-json.hh',