forked from lix-project/lix
Merge "Move DebugChar
into its own file" into main
This commit is contained in:
commit
877750b7c5
22
src/libutil/escape-char.cc
Normal file
22
src/libutil/escape-char.cc
Normal 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
src/libutil/escape-char.hh
Normal file
22
src/libutil/escape-char.hh
Normal 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
|
|
@ -8,6 +8,7 @@ libutil_sources = files(
|
||||||
'config.cc',
|
'config.cc',
|
||||||
'english.cc',
|
'english.cc',
|
||||||
'error.cc',
|
'error.cc',
|
||||||
|
'escape-char.cc',
|
||||||
'exit.cc',
|
'exit.cc',
|
||||||
'experimental-features.cc',
|
'experimental-features.cc',
|
||||||
'filesystem.cc',
|
'filesystem.cc',
|
||||||
|
@ -50,6 +51,7 @@ libutil_headers = files(
|
||||||
'config.hh',
|
'config.hh',
|
||||||
'english.hh',
|
'english.hh',
|
||||||
'error.hh',
|
'error.hh',
|
||||||
|
'escape-char.hh',
|
||||||
'exit.hh',
|
'exit.hh',
|
||||||
'experimental-features.hh',
|
'experimental-features.hh',
|
||||||
'experimental-features-json.hh',
|
'experimental-features-json.hh',
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include "test-session.hh"
|
#include "test-session.hh"
|
||||||
#include "util.hh"
|
#include "util.hh"
|
||||||
#include "tests/debug-char.hh"
|
#include "escape-char.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ std::ostream & operator<<(std::ostream & os, ReplOutputParser::State s)
|
||||||
void ReplOutputParser::transition(State new_state, char responsible_char, bool wasPrompt)
|
void ReplOutputParser::transition(State new_state, char responsible_char, bool wasPrompt)
|
||||||
{
|
{
|
||||||
if constexpr (DEBUG_REPL_PARSER) {
|
if constexpr (DEBUG_REPL_PARSER) {
|
||||||
std::cerr << "transition " << new_state << " for " << DebugChar{responsible_char}
|
std::cerr << "transition " << new_state << " for " << MaybeHexEscapedChar{responsible_char}
|
||||||
<< (wasPrompt ? " [prompt]" : "") << "\n";
|
<< (wasPrompt ? " [prompt]" : "") << "\n";
|
||||||
}
|
}
|
||||||
state = new_state;
|
state = new_state;
|
||||||
|
@ -118,7 +118,7 @@ bool TestSession::waitForPrompt()
|
||||||
});
|
});
|
||||||
|
|
||||||
if constexpr (DEBUG_REPL_PARSER) {
|
if constexpr (DEBUG_REPL_PARSER) {
|
||||||
std::cerr << "raw " << DebugChar{buf[i]} << (wasEaten ? " [eaten]" : "") << "\n";
|
std::cerr << "raw " << MaybeHexEscapedChar{buf[i]} << (wasEaten ? " [eaten]" : "") << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "cli-literate-parser.hh"
|
#include "cli-literate-parser.hh"
|
||||||
#include "libexpr/print.hh"
|
#include "libexpr/print.hh"
|
||||||
#include "debug-char.hh"
|
#include "escape-char.hh"
|
||||||
#include "types.hh"
|
#include "types.hh"
|
||||||
#include "util.hh"
|
#include "util.hh"
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
@ -77,7 +77,7 @@ CLILiterateParser::CLILiterateParser(std::string prompt, size_t indent)
|
||||||
void CLILiterateParser::feed(char c)
|
void CLILiterateParser::feed(char c)
|
||||||
{
|
{
|
||||||
if constexpr (DEBUG_PARSER) {
|
if constexpr (DEBUG_PARSER) {
|
||||||
std::cout << stateDebug(state_) << " " << DebugChar{c} << "\n";
|
std::cout << stateDebug(state_) << " " << MaybeHexEscapedChar{c} << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
///@file
|
|
||||||
#include <ostream>
|
|
||||||
#include <boost/io/ios_state.hpp>
|
|
||||||
|
|
||||||
namespace nix {
|
|
||||||
|
|
||||||
struct DebugChar
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
};
|
|
||||||
|
|
||||||
inline std::ostream & operator<<(std::ostream & s, DebugChar c)
|
|
||||||
{
|
|
||||||
boost::io::ios_flags_saver _ifs(s);
|
|
||||||
|
|
||||||
if (isprint(c.c)) {
|
|
||||||
s << static_cast<char>(c.c);
|
|
||||||
} else {
|
|
||||||
s << std::hex << "0x" << (static_cast<unsigned int>(c.c) & 0xff);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "terminal-code-eater.hh"
|
#include "terminal-code-eater.hh"
|
||||||
#include "debug-char.hh"
|
#include "escape-char.hh"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -14,7 +14,7 @@ void TerminalCodeEater::feed(char c, std::function<void(char)> on_char)
|
||||||
auto isIntermediateChar = [](char v) -> bool { return v >= 0x20 && v <= 0x2f; };
|
auto isIntermediateChar = [](char v) -> bool { return v >= 0x20 && v <= 0x2f; };
|
||||||
auto isFinalChar = [](char v) -> bool { return v >= 0x40 && v <= 0x7e; };
|
auto isFinalChar = [](char v) -> bool { return v >= 0x40 && v <= 0x7e; };
|
||||||
if constexpr (DEBUG_EATER) {
|
if constexpr (DEBUG_EATER) {
|
||||||
std::cerr << "eater" << DebugChar{c} << "\n";
|
std::cerr << "eater" << MaybeHexEscapedChar{c} << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
@ -28,7 +28,7 @@ void TerminalCodeEater::feed(char c, std::function<void(char)> on_char)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if constexpr (DEBUG_EATER) {
|
if constexpr (DEBUG_EATER) {
|
||||||
std::cerr << "eater uneat" << DebugChar{c} << "\n";
|
std::cerr << "eater uneat" << MaybeHexEscapedChar{c} << "\n";
|
||||||
}
|
}
|
||||||
on_char(c);
|
on_char(c);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue