diff --git a/README.md b/README.md index bb11005..ba9bbb6 100644 --- a/README.md +++ b/README.md @@ -2,25 +2,21 @@ Include soi and you're ready to go! -## Features +## 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. +You can debug any expressions using the macro dbg(). ## Configuration You can set the following environment variables to customize the behaviour. -`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_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_EOFCHECK`: By default, it is *not* checked, whether you have read all the +`SOI_H_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_EOFCHECK=1`. +this check using `SOI_H_COLOR=1`. ## Template Using this header is as simple as: diff --git a/include/bits/include-all.hpp b/include/bits/include-all.hpp deleted file mode 100644 index 639d3df..0000000 --- a/include/bits/include-all.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#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/prettyprint.hpp b/include/prettyprint.hpp similarity index 99% rename from include/bits/prettyprint.hpp rename to include/prettyprint.hpp index a480c97..5c8d127 100644 --- a/include/bits/prettyprint.hpp +++ b/include/prettyprint.hpp @@ -7,8 +7,8 @@ // // The global operator<< overload hs been removed -#ifndef SOI_PRETTY_PRINT -#define SOI_PRETTY_PRINT +#ifndef SOI_H_PRETTY_PRINT +#define SOI_H_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_PRETTY_PRINT +#endif // SOI_H_PRETTY_PRINT diff --git a/include/soi b/include/soi index 5e4d9e6..1836aec 100644 --- a/include/soi +++ b/include/soi @@ -5,11 +5,33 @@ have to understand every concept all at once. */ +#include +#include +#include +#include +#include #include -#include "bits/soi-dbg.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "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"; @@ -34,7 +56,7 @@ void noninteractive_check_eof() { } bool should_check_for_eof() { - if (const char* eofcheck_enabled = std::getenv("SOI_EOFCHECK")) { + if (const char* eofcheck_enabled = std::getenv("SOI_H_EOFCHECK")) { if (!std::strcmp(eofcheck_enabled, "1")) return true; if (!std::strcmp(eofcheck_enabled, "0")) @@ -47,14 +69,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) { @@ -71,12 +93,11 @@ 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" +#include "soi-deprecate.hpp" #define int int64_t using namespace std; + diff --git a/include/bits/soi-dbg.hpp b/include/soi-dbg.hpp similarity index 80% rename from include/bits/soi-dbg.hpp rename to include/soi-dbg.hpp index b45b678..a24b149 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/soi-dbg.hpp @@ -7,15 +7,13 @@ // initialize with dbg_init() to enable colorized output // -#ifndef SOI_DBG -#define SOI_DBG +#ifndef SOI_H_DBG +#define SOI_H_DBG #include #include #include #include -#include -#include #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) #include @@ -90,6 +88,16 @@ 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) @@ -97,6 +105,20 @@ 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 @@ -119,27 +141,31 @@ 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 +static_string_no_std sanitized_type_name() { CONSTEXPR14_TN static_string t = type_name(); - 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); + 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); } - + // ---------------------------------------------------------------------------- // colorized output @@ -152,7 +178,7 @@ bool tty_supports_colors() { } int has_environment_color_overwrite() { - if (const char* color_enabled = std::getenv("SOI_COLOR")) { + if (const char* color_enabled = std::getenv("SOI_H_COLOR")) { if (!std::strcmp(color_enabled, "1")) return 1; if (!std::strcmp(color_enabled, "0")) @@ -182,11 +208,11 @@ static const char* ANSI_RESET = ""; void dbg_init(bool with_colors) { if (with_colors) { bool colors_enabled = true; - ANSI_DEBUG = "\x1b[37m"; + ANSI_DEBUG = "\x1b[02m"; ANSI_EXPRESSION = "\x1b[36m"; ANSI_VALUE = "\x1b[01m"; ANSI_TYPE = "\x1b[32m"; - ANSI_MESSAGE = "\x1b[31;01m"; + ANSI_MESSAGE = "\x1b[31m\x1b[01m"; ANSI_RESET = "\x1b[0m"; } else { bool colors_enabled = false; @@ -208,7 +234,7 @@ void dbg_init() { template T&& dbg_print(T&& value, - static_string const& type, + static_string_no_std const& type, char const* file, int line, char const* function_name, @@ -231,11 +257,11 @@ T&& dbg_print(T&& value, template auto dbg_print(const char (&msg)[N], - static_string const&, + static_string_no_std const&, 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 @@ -253,7 +279,7 @@ T&& identity(T&& t) { } // end namespace dbg_macro -#ifdef SOI_RELEASE +#ifdef SOI_H_RELEASE #define dbg(...) dbg_macro::identity(__VA_ARGS__) #else #define dbg(...) \ @@ -265,4 +291,4 @@ T&& identity(T&& t) { #__VA_ARGS__) #endif -#endif // SOI_DBG +#endif // SOI_H_DBG diff --git a/include/bits/soi-deprecate.hpp b/include/soi-deprecate.hpp similarity index 100% rename from include/bits/soi-deprecate.hpp rename to include/soi-deprecate.hpp diff --git a/include/bits/soi-pretty.hpp b/include/soi-pretty.hpp similarity index 97% rename from include/bits/soi-pretty.hpp rename to include/soi-pretty.hpp index 116bdf5..a2266f3 100644 --- a/include/bits/soi-pretty.hpp +++ b/include/soi-pretty.hpp @@ -6,8 +6,8 @@ // pretty printing with c++ // -#ifndef SOI_PRETTY -#define SOI_PRETTY +#ifndef SOI_H_PRETTY +#define SOI_H_PRETTY #include "prettyprint.hpp" @@ -73,4 +73,4 @@ pretty_print(std::basic_ostream & stream, const T& x) { } -#endif // SOI_PRETTY +#endif // SOI_H_PRETTY