39#include "platform.hpp"
52using namespace std::literals;
61 const char* nameSection = #__VA_ARGS__; \
62 std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << reset << '\n'; \
63 std::cout << colors::brightBlue << "[Profiling: " << nameSection << ']' << reset << '\n'; \
64 auto start = std::chrono::high_resolution_clock::now();
71 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start) \
73 std::cout << colors::brightBlue << '[' << nameSection << " took " << duration << "(microseconds) to execute]" \
75 std::cout << colors::brightBlue << std::setw(80) << std::setfill('-'); \
76 std::cout << reset << '\n'; \
82 #define MAX_DECIMALS 50
88 #if (_MSC_VER || __MINGW32__ || __MSVCRT__)
89 #define MS_STDLIB_BUGS 1
91 #define MS_STDLIB_BUGS 0
109 inline DWORD enableVtMode()
112 const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
113 if (hOut == INVALID_HANDLE_VALUE)
117 if (!GetConsoleMode(hOut, &dwMode))
119 const DWORD dwModeOrig = dwMode;
121 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
122 if (!SetConsoleMode(hOut, dwMode))
133 inline bool restoreVtMode(
const DWORD dwModeOrig)
135 const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
136 if (hOut == INVALID_HANDLE_VALUE)
139 if (!SetConsoleMode(hOut, dwModeOrig))
162 SetConsoleOutputCP(CP_UTF8);
163 dwModeOrig = enableVtMode();
173 SetConsoleOutputCP(oldCodePage);
174 restoreVtMode(dwModeOrig);
205 template<
typename NumberT>
208 if (*decimal > MAX_DECIMALS)
211 "The number of decimals ({0}) is more than the accepted {1} digits."s,
212 { std::to_string(*decimal), std::to_string(MAX_DECIMALS) });
230 std::copy_n(str, N,
value);
296 return std::ranges::all_of(
string, [](
const char c) {
return c ==
'0' or c ==
'.' or c ==
'-'; });
305 bool isNumber(
const std::string& s);
319 SplitNumberResult
splitNumber(
const std::string& a,
320 const std::string& b,
321 bool padInteger =
true,
322 bool padDecimal =
true,
323 bool properlyFormat =
true,
324 bool preserveNegative =
false);
332 auto replaceLeadingZeros(
const std::vector<int>& vector) -> std::decay_t<
decltype(vector)>;
340 auto removeLeadingZeros(
const std::vector<int>& vector) -> std::decay_t<
decltype(vector)>;
356 auto removeTrailingZeros(
const std::vector<int>& _vector) -> std::decay_t<
decltype(_vector)>;
380 bool isInteger(
const std::string& number);
388 bool isDecimal(
const std::string& number);
404 bool isOdd(
const std::string& number);
412 bool isEven(
const std::string& number);
428 template<
typename CharT>
429 auto split(std::basic_string<CharT> s,
const CharT separator)
431 std::vector<
decltype(s)> substrings;
432 std::basic_stringstream<CharT> ss(s);
435 while (getline(ss, token, separator))
436 substrings.push_back(token);
448 template<
typename CharT>
449 auto split(std::basic_string_view<CharT> s,
const CharT separator)
451 std::vector<
decltype(s)> result;
452 auto left = s.begin();
453 for (
auto it = left; it != s.end(); ++it)
455 if (*it == separator)
457 result.emplace_back(&*left, it - left);
462 result.emplace_back(&*left, s.end() - left);
480 template<
typename CharT>
481 auto rReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
483 std::basic_string<CharT> out = s;
484 typename std::basic_string<CharT>::size_type count = 0;
485 while (out.back() == t)
490 if (replacement !=
'\0')
491 out += std::basic_string<CharT>(count, replacement);
506 template<
typename CharT>
507 auto lReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
509 std::basic_string<CharT> out = s;
510 typename std::basic_string<CharT>::size_type count = 0;
513 out.erase(out.begin());
516 if (replacement !=
'\0')
517 out = std::basic_string<CharT>(count, replacement) + out;
535 template<
typename CharT>
536 auto bothEndsReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
550 auto join(
const std::vector<T>& vector,
const std::string& delimiter)
552 std::stringstream result;
553 for (
const auto& item : vector)
554 result << item << delimiter;
564 std::string
makeWider(
const std::string& orig);
572 [[maybe_unused]]
int utf8ToUnicode(
const std::string& utf8_code);
590 std::vector<std::string>
duplicates(
const std::vector<std::string>& vector);
592 template<
typename ValueType>
595 std::ostringstream out;
597 for (
size_t i = 0; i < vector.size(); ++i)
600 if (i != vector.size() - 1)
A class that sets the console output code page to UTF-8 and enables VT mode.
Definition util.hpp:191
Utf8CodePage()
Definition util.hpp:193
Utilities to operate numbers.
Definition factors.cpp:35
bool isEven(const std::string &number)
Checks if a number is even.
Definition util.cpp:300
auto removeLeadingZeros(const std::vector< int > &vector) -> std::decay_t< decltype(vector)>
Definition util.cpp:191
std::string simplifyPolarity(const std::string &_string)
Simplifies a string with non-standard polarity (e.g., –1, —1, etc.).
Definition util.cpp:77
std::string simplifyZeroPolarity(const std::string &string)
Simplifies a string that represents a number with non-standard polarity.
Definition util.cpp:69
auto removeTrailingZeros(const std::vector< int > &_vector) -> std::decay_t< decltype(_vector)>
Definition util.cpp:220
constexpr bool isZeroString(const std::string &string)
Checks if a given string is a zero string.
Definition util.hpp:294
bool isOdd(const std::string &number)
Checks if a number is odd.
Definition util.cpp:289
bool isPowerOfTen(const std::string &_number)
Determines whether the number is a power of 10.
Definition util.cpp:273
long long determineScale(const std::string &number)
Determines the scale of a number, i.e., the power of 10 when it is expressed in scientific notation.
Definition util.cpp:244
bool isInteger(const std::string &number)
Determines whether the number is an integer or not.
Definition util.cpp:264
bool isNumber(const std::string &s)
Checks if a given string is a valid number.
Definition util.cpp:42
auto replaceLeadingZeros(const std::vector< int > &vector) -> std::decay_t< decltype(vector)>
Definition util.cpp:177
std::string standardizeNumber(const std::string &_number)
Standardizes a number string.
Definition util.cpp:85
SplitNumberResult splitNumber(const std::string &_a, const std::string &_b, const bool padInteger, const bool padDecimal, bool properlyFormat, bool preserveNegative)
Definition util.cpp:103
bool isDecimal(const std::string &number)
Determines whether the number is a decimal or not.
Definition util.cpp:271
Utilities to operate strings.
Definition util.cpp:312
auto split(std::basic_string< CharT > s, const CharT separator)
Splits a string into substrings based on a separator.
Definition util.hpp:429
std::string vectorToString(const std::vector< ValueType > &vector)
Definition util.hpp:593
std::string unicodeToUtf8(const int unicode)
Converts a Unicode character to UTF-8 encoding.
Definition util.cpp:342
auto join(const std::vector< T > &vector, const std::string &delimiter)
Joins a vector of elements into a single string using a delimiter.
Definition util.hpp:550
auto lReplace(const std::basic_string< CharT > s, const CharT t, const CharT replacement='\0')
Replaces the leading occurrences of a character in a string with a replacement character.
Definition util.hpp:507
auto rReplace(const std::basic_string< CharT > s, const CharT t, const CharT replacement='\0')
Replaces the trailing occurrences of a character in a string with another character.
Definition util.hpp:481
std::string makeWider(const std::string &orig)
Makes the given string wider by adding 2 spaces between each character.
Definition util.cpp:313
std::vector< std::string > duplicates(const std::vector< std::string > &vector)
Gets the duplicates in a vector of strings.
auto bothEndsReplace(const std::basic_string< CharT > s, const CharT t, const CharT replacement='\0')
Replaces the leading and trailing occurrences of a character in a string with a replacement character...
Definition util.hpp:536
int utf8ToUnicode(const std::string &utf8_code)
Converts a UTF-8 encoded string to a Unicode string.
Definition util.cpp:325
The namespace containing utility functions for the Steppable library.
Definition argParse.cpp:40
void programSafeExit(const int status)
Exit the program safely.
Definition platform.hpp:59
void checkDecimalArg(const NumberT *decimal)
Checks whether the decimal number is correctly specified.
Definition util.hpp:206
void error(const std::string &name, const std::basic_string< T > &msg, const std::vector< std::string > &args={})
Prints an error message.
Definition output.hpp:80
A structure for storing split numbers.
Definition util.hpp:254
bool aIsNegative
Definition util.hpp:256
std::array< std::string, 4 > splitNumberArray
Definition util.hpp:255
bool bIsNegative
Definition util.hpp:256
char value[N]
The value of the wrapper class. Stores the string object.
Definition util.hpp:236
constexpr StringLiteral(const char(&str)[N])
Initializes the wrapper class.
Definition util.hpp:228