From d8a3ccc553ce616be5a77dde1294528d6524ad03 Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 17:16:19 +0200 Subject: [PATCH 1/5] rename from soi.h to soi, including macros --- README.md | 8 ++++---- include/{ => bits}/prettyprint.hpp | 8 ++++---- include/{ => bits}/soi-dbg.hpp | 14 +++++++------- include/{ => bits}/soi-deprecate.hpp | 0 include/{ => bits}/soi-pretty.hpp | 6 +++--- include/soi | 6 +++--- 6 files changed, 21 insertions(+), 21 deletions(-) rename include/{ => bits}/prettyprint.hpp (99%) rename include/{ => bits}/soi-dbg.hpp (97%) rename include/{ => bits}/soi-deprecate.hpp (100%) rename include/{ => bits}/soi-pretty.hpp (97%) diff --git a/README.md b/README.md index ba9bbb6..0c94b40 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ You can debug any expressions using the macro dbg(). You can set the following environment variables to customize the behaviour. -`SOI_H_COLOR`: By default, colored output is shown in case a terminal is connected. -You can force colors with `SOI_H_COLOR=1` and shut them off with `SOI_H_COLOR=0`. +`SOI_COLOR`: By default, colored output is shown in case a terminal is connected. +You can force colors with `SOI_COLOR=1` and shut them off with `SOI_COLOR=0`. -`SOI_H_EOFCHECK`: By default, it is *not* checked, whether you have read all the +`SOI_EOFCHECK`: By default, it is *not* checked, whether you have read all the input. In case you pipe the input from a file or want to issue a proper EOF character (Control D under linux or Control Z under Windows), you can enable -this check using `SOI_H_COLOR=1`. +this check using `SOI_EOFCHECK=1`. ## Template Using this header is as simple as: diff --git a/include/prettyprint.hpp b/include/bits/prettyprint.hpp similarity index 99% rename from include/prettyprint.hpp rename to include/bits/prettyprint.hpp index 5c8d127..a480c97 100644 --- a/include/prettyprint.hpp +++ b/include/bits/prettyprint.hpp @@ -7,8 +7,8 @@ // // The global operator<< overload hs been removed -#ifndef SOI_H_PRETTY_PRINT -#define SOI_H_PRETTY_PRINT +#ifndef SOI_PRETTY_PRINT +#define SOI_PRETTY_PRINT #include #include @@ -88,7 +88,7 @@ namespace pretty_print struct delimiters { using type = delimiters_values; - static const type values; + static const type values; }; @@ -441,4 +441,4 @@ namespace std } */ -#endif // SOI_H_PRETTY_PRINT +#endif // SOI_PRETTY_PRINT diff --git a/include/soi-dbg.hpp b/include/bits/soi-dbg.hpp similarity index 97% rename from include/soi-dbg.hpp rename to include/bits/soi-dbg.hpp index a24b149..bafd1a2 100644 --- a/include/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -7,8 +7,8 @@ // initialize with dbg_init() to enable colorized output // -#ifndef SOI_H_DBG -#define SOI_H_DBG +#ifndef SOI_DBG +#define SOI_DBG #include #include @@ -165,7 +165,7 @@ sanitized_type_name() { t[11] == 'g'; return static_string_no_std(is_debug, is_std, t); } - + // ---------------------------------------------------------------------------- // colorized output @@ -178,7 +178,7 @@ bool tty_supports_colors() { } int has_environment_color_overwrite() { - if (const char* color_enabled = std::getenv("SOI_H_COLOR")) { + if (const char* color_enabled = std::getenv("SOI_COLOR")) { if (!std::strcmp(color_enabled, "1")) return 1; if (!std::strcmp(color_enabled, "0")) @@ -261,7 +261,7 @@ auto dbg_print(const char (&msg)[N], char const* file, int line, char const* function_name, - char const* expression) -> decltype(msg) { + char const* expression) -> decltype(msg) { std::cerr << ANSI_DEBUG << "[" << file << ":" << line << " (" << function_name << ")] " << ANSI_RESET @@ -279,7 +279,7 @@ T&& identity(T&& t) { } // end namespace dbg_macro -#ifdef SOI_H_RELEASE +#ifdef SOI_RELEASE #define dbg(...) dbg_macro::identity(__VA_ARGS__) #else #define dbg(...) \ @@ -291,4 +291,4 @@ T&& identity(T&& t) { #__VA_ARGS__) #endif -#endif // SOI_H_DBG +#endif // SOI_DBG diff --git a/include/soi-deprecate.hpp b/include/bits/soi-deprecate.hpp similarity index 100% rename from include/soi-deprecate.hpp rename to include/bits/soi-deprecate.hpp diff --git a/include/soi-pretty.hpp b/include/bits/soi-pretty.hpp similarity index 97% rename from include/soi-pretty.hpp rename to include/bits/soi-pretty.hpp index a2266f3..116bdf5 100644 --- a/include/soi-pretty.hpp +++ b/include/bits/soi-pretty.hpp @@ -6,8 +6,8 @@ // pretty printing with c++ // -#ifndef SOI_H_PRETTY -#define SOI_H_PRETTY +#ifndef SOI_PRETTY +#define SOI_PRETTY #include "prettyprint.hpp" @@ -73,4 +73,4 @@ pretty_print(std::basic_ostream & stream, const T& x) { } -#endif // SOI_H_PRETTY +#endif // SOI_PRETTY diff --git a/include/soi b/include/soi index 1836aec..2c6aa5a 100644 --- a/include/soi +++ b/include/soi @@ -28,7 +28,7 @@ #include #include -#include "soi-dbg.hpp" +#include "bits/soi-dbg.hpp" namespace soi_h { @@ -56,7 +56,7 @@ void noninteractive_check_eof() { } bool should_check_for_eof() { - if (const char* eofcheck_enabled = std::getenv("SOI_H_EOFCHECK")) { + if (const char* eofcheck_enabled = std::getenv("SOI_EOFCHECK")) { if (!std::strcmp(eofcheck_enabled, "1")) return true; if (!std::strcmp(eofcheck_enabled, "0")) @@ -96,7 +96,7 @@ soi_h_initializer soi_h_initializer_{false}; } // end namespace soi_h -#include "soi-deprecate.hpp" +#include "bits/soi-deprecate.hpp" #define int int64_t using namespace std; From 3d1c648b4ff7124d76a192c2cde8a49f88c64c47 Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 17:19:10 +0200 Subject: [PATCH 2/5] put include all into its own header --- include/bits/include-all.hpp | 39 ++++++++++++++++++++++++++++++++++++ include/bits/soi-dbg.hpp | 2 ++ include/soi | 35 +++++++------------------------- 3 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 include/bits/include-all.hpp diff --git a/include/bits/include-all.hpp b/include/bits/include-all.hpp new file mode 100644 index 0000000..639d3df --- /dev/null +++ b/include/bits/include-all.hpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __cplusplus >= 201703L +#include +#include +#include +#include +#endif diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index bafd1a2..7bc3da9 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) #include diff --git a/include/soi b/include/soi index 2c6aa5a..5e4d9e6 100644 --- a/include/soi +++ b/include/soi @@ -5,33 +5,11 @@ have to understand every concept all at once. */ -#include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "bits/soi-dbg.hpp" namespace soi_h { - + void check_for_eof() { if (!(std::cin >> std::ws).eof()) std::cerr << "WARNING: didn't read the whole input\n"; @@ -69,14 +47,14 @@ void initialize_debug() { std::cout << std::unitbuf; // enable automatic flushing std::cin.exceptions(std::ifstream::failbit | std::ifstream::badbit); std::ios::sync_with_stdio(false); - + if (should_check_for_eof() && std::atexit(noninteractive_check_eof) != 0) { - std::cerr << "WARNING: soi.h -- registration of sanity check at exit failed\n"; + std::cerr << "WARNING: soi.h -- registration of sanity check at exit failed\n"; } soi_h::detail::dbg_init(); } - + struct soi_h_initializer { soi_h_initializer(bool release) { if (release) { @@ -93,11 +71,12 @@ soi_h_initializer soi_h_initializer_{true}; #else soi_h_initializer soi_h_initializer_{false}; #endif - + } // end namespace soi_h #include "bits/soi-deprecate.hpp" +#include "bits/include-all.hpp" + #define int int64_t using namespace std; - From 475a7f791008db62718f1fcdfe867fdf68ea5ad3 Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 17:33:18 +0200 Subject: [PATCH 3/5] improve handling of std:: prefixes --- include/bits/soi-dbg.hpp | 62 +++++++++++----------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index 7bc3da9..dc5e2e1 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -90,16 +90,6 @@ public: } }; -struct static_string_no_std { - bool is_debug; - bool is_std; - static_string type; - CONSTEXPR11_TN static_string_no_std(bool is_debug, - bool is_std, - static_string type) - : is_debug(is_debug), is_std(is_std), type(type) {} -}; - inline std::ostream& operator<<(std::ostream& os, static_string const& s) @@ -107,20 +97,6 @@ operator<<(std::ostream& os, static_string const& s) return os.write(s.data(), s.size()); } -inline -std::ostream& -operator<<(std::ostream& os, static_string_no_std const& s) { - if (s.is_debug) { - const int k = sizeof("std::__debug::") - 1; - return os.write(s.type.data()+k, s.type.size()-k); - } else if (s.is_std) { - const int k = sizeof("std::") - 1; - return os.write(s.type.data()+k, s.type.size()-k); - } else { - return os.write(s.type.data(), s.type.size()); - } -} - template CONSTEXPR14_TN static_string @@ -143,29 +119,25 @@ type_name() #endif } +constexpr bool is_prefix_of(char const *suffix, char const *s) { + return suffix[0]=='\0' || (suffix[0] == s[0] && is_prefix_of(suffix + 1, s + 1)); +} + +static char const* const type_string = "string"; + template CONSTEXPR14_TN -static_string_no_std +static_string sanitized_type_name() { CONSTEXPR14_TN static_string t = type_name(); - CONSTEXPR14_TN bool is_std = - t.size() >= sizeof("std::") && - t[ 0] == 's' && - t[ 1] == 't' && - t[ 2] == 'd' && - t[ 3] == ':' && - t[ 4] == ':'; - CONSTEXPR14_TN bool is_debug = - is_std && - t.size() >= sizeof("std::__debug::") && - t[ 5] == '_' && - t[ 6] == '_' && - t[ 7] == 'd' && - t[ 8] == 'e' && - t[ 9] == 'b' && - t[10] == 'u' && - t[11] == 'g'; - return static_string_no_std(is_debug, is_std, t); + CONSTEXPR14_TN std::size_t offset = + is_prefix_of("std::__debug::", t.data()) ? sizeof("std::__debug::")-1 : + is_prefix_of("std::", t.data()) ? sizeof("std::")-1 : + 0; + return + is_prefix_of("std::__cxx11::basic_string", t.data()) ? + static_string(type_string, sizeof(type_string)-2) : + static_string(t.data()+offset, t.size()-offset); } // ---------------------------------------------------------------------------- @@ -236,7 +208,7 @@ void dbg_init() { template T&& dbg_print(T&& value, - static_string_no_std const& type, + static_string const& type, char const* file, int line, char const* function_name, @@ -259,7 +231,7 @@ T&& dbg_print(T&& value, template auto dbg_print(const char (&msg)[N], - static_string_no_std const&, + static_string const&, char const* file, int line, char const* function_name, From d594be1ac0795ad768e52938e05020fcbd24cd7b Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 17:36:04 +0200 Subject: [PATCH 4/5] tweak colors --- include/bits/soi-dbg.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index dc5e2e1..b45b678 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -182,11 +182,11 @@ static const char* ANSI_RESET = ""; void dbg_init(bool with_colors) { if (with_colors) { bool colors_enabled = true; - ANSI_DEBUG = "\x1b[02m"; + ANSI_DEBUG = "\x1b[37m"; ANSI_EXPRESSION = "\x1b[36m"; ANSI_VALUE = "\x1b[01m"; ANSI_TYPE = "\x1b[32m"; - ANSI_MESSAGE = "\x1b[31m\x1b[01m"; + ANSI_MESSAGE = "\x1b[31;01m"; ANSI_RESET = "\x1b[0m"; } else { bool colors_enabled = false; From 69a51698415c63463281f3b7e62c0377a0bf9ada Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Sun, 6 Oct 2019 17:40:46 +0200 Subject: [PATCH 5/5] improve readme --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c94b40..bb11005 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,13 @@ Include soi and you're ready to go! -## dbg(...) +## Features -You can debug any expressions using the macro dbg(). +- includes all headers (a `bits/stdc++.h` that works on all platforms) +- has a powerful `dbg()` macro +- disables output buffering +- checks whether all input has been read +- removes evil functions from the standard library such as new and printf. ## Configuration