43#include "platform.hpp"
44#include "types/concepts.hpp"
58using namespace std::literals;
67 const char* nameSection = #__VA_ARGS__; \
68 std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << reset << '\n'; \
69 std::cout << colors::brightBlue << "[Profiling: " << nameSection << ']' << reset << '\n'; \
70 auto start = std::chrono::high_resolution_clock::now();
77 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start) \
79 std::cout << colors::brightBlue << '[' << nameSection << " took " << duration << "(microseconds) to execute]" \
81 std::cout << colors::brightBlue << std::setw(80) << std::setfill('-'); \
82 std::cout << reset << '\n'; \
88 #define MAX_DECIMALS 50
94 #if (_MSC_VER || __MINGW32__ || __MSVCRT__)
95 #define MS_STDLIB_BUGS 1
97 #define MS_STDLIB_BUGS 0
114 inline DWORD enableVtMode()
117 const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
118 if (hOut == INVALID_HANDLE_VALUE)
122 if (!GetConsoleMode(hOut, &dwMode))
124 const DWORD dwModeOrig = dwMode;
126 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
127 if (!SetConsoleMode(hOut, dwMode))
138 inline bool restoreVtMode(
const DWORD dwModeOrig)
140 const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
141 if (hOut == INVALID_HANDLE_VALUE)
144 if (!SetConsoleMode(hOut, dwModeOrig))
167 SetConsoleOutputCP(CP_UTF8);
168 dwModeOrig = enableVtMode();
178 SetConsoleOutputCP(oldCodePage);
179 restoreVtMode(dwModeOrig);
210 template<
typename NumberT>
213 if (*decimal > MAX_DECIMALS)
216 "The number of decimals ({0}) is more than the accepted {1} digits."s,
217 { std::to_string(*decimal), std::to_string(MAX_DECIMALS) });
235 std::copy_n(str, N,
value);
303 return std::ranges::all_of(
string, [](
const char c) {
return c ==
'0' or c ==
'.' or c ==
'-'; });
312 bool isNumber(
const std::string& s);
327 const std::string& b,
328 bool padInteger =
true,
329 bool padDecimal =
true,
330 bool properlyFormat =
true,
331 bool preserveNegative =
false);
339 auto replaceLeadingZeros(
const std::vector<int>& vector) -> std::decay_t<
decltype(vector)>;
347 auto removeLeadingZeros(
const std::vector<int>& vector) -> std::decay_t<
decltype(vector)>;
363 auto removeTrailingZeros(
const std::vector<int>& _vector) -> std::decay_t<
decltype(_vector)>;
387 bool isInteger(
const std::string& number);
395 bool isDecimal(
const std::string& number);
411 bool isOdd(
const std::string& number);
419 bool isEven(
const std::string& number);
435 template<
typename CharT>
436 auto split(std::basic_string<CharT> s,
const CharT separator)
438 std::vector<
decltype(s)> substrings;
439 std::basic_stringstream<CharT> ss(s);
442 while (getline(ss, token, separator))
443 substrings.push_back(token);
455 template<
typename CharT>
456 auto split(std::basic_string_view<CharT> s,
const CharT separator)
458 std::vector<
decltype(s)> result;
459 auto left = s.begin();
460 for (
auto it = left; it != s.end(); ++it)
462 if (*it == separator)
464 result.emplace_back(&*left, it - left);
469 result.emplace_back(&*left, s.end() - left);
487 template<
typename CharT>
488 auto rReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
490 std::basic_string<CharT> out = s;
491 typename std::basic_string<CharT>::size_type count = 0;
492 while (out.back() == t)
497 if (replacement !=
'\0')
498 out += std::basic_string<CharT>(count, replacement);
513 template<
typename CharT>
514 auto lReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
516 std::basic_string<CharT> out = s;
517 typename std::basic_string<CharT>::size_type count = 0;
520 out.erase(out.begin());
523 if (replacement !=
'\0')
524 out = std::basic_string<CharT>(count, replacement) + out;
542 template<
typename CharT>
543 auto bothEndsReplace(
const std::basic_string<CharT> s,
const CharT t,
const CharT replacement =
'\0')
557 auto join(
const std::vector<T>& vector,
const std::string& delimiter)
559 std::stringstream result;
560 for (
const auto& item : vector)
561 result << item << delimiter;
571 std::string
makeWider(
const std::string& orig);
579 [[maybe_unused]]
int utf8ToUnicode(
const std::string& utf8_code);
597 std::vector<std::string>
duplicates(
const std::vector<std::string>& vector);
599 template<
typename ValueType>
602 std::ostringstream out;
604 for (
size_t i = 0; i < vector.size(); ++i)
607 if (i != vector.size() - 1)
613 template<concepts::Numeric T>
618 if (
auto result = std::from_chars(s.data(), s.data() + s.size(), value); result.ec != std::errc{})
A class that sets the console output code page to UTF-8 and enables VT mode.
Definition util.hpp:196
Utf8CodePage()
Definition util.hpp:198
Utilities to operate numbers.
Definition factors.cpp:35
bool isEven(const std::string &number)
Checks if a number is even.
Definition util.cpp:309
auto removeLeadingZeros(const std::vector< int > &vector) -> std::decay_t< decltype(vector)>
Definition util.cpp:196
std::string simplifyPolarity(const std::string &_string)
Simplifies a string with non-standard polarity (e.g., –1, —1, etc.).
Definition util.cpp:79
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:225
constexpr bool isZeroString(const std::string &string)
Checks if a given string is a zero string.
Definition util.hpp:299
bool isOdd(const std::string &number)
Checks if a number is odd.
Definition util.cpp:298
bool isPowerOfTen(const std::string &_number)
Determines whether the number is a power of 10.
Definition util.cpp:278
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:249
bool isInteger(const std::string &number)
Determines whether the number is an integer or not.
Definition util.cpp:269
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:182
std::string standardizeNumber(const std::string &_number)
Standardizes a number string.
Definition util.cpp:91
SplitNumberResult splitNumber(const std::string &_a, const std::string &_b, const bool padInteger, const bool padDecimal, bool properlyFormat, bool preserveNegative)
Definition util.cpp:114
bool isDecimal(const std::string &number)
Determines whether the number is a decimal or not.
Definition util.cpp:276
Utilities to operate strings.
Definition symbols.cpp:172
T toNumeric(const std::string &s)
Definition util.hpp:614
auto split(std::basic_string< CharT > s, const CharT separator)
Splits a string into substrings based on a separator.
Definition util.hpp:436
std::string vectorToString(const std::vector< ValueType > &vector)
Definition util.hpp:600
std::string unicodeToUtf8(const int unicode)
Converts a Unicode character to UTF-8 encoding.
Definition util.cpp:351
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:557
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:514
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:488
std::string makeWider(const std::string &orig)
Makes the given string wider by adding 2 spaces between each character.
Definition util.cpp:322
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:543
int utf8ToUnicode(const std::string &utf8_code)
Converts a UTF-8 encoded string to a Unicode string.
Definition util.cpp:334
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:61
void checkDecimalArg(const NumberT *decimal)
Checks whether the decimal number is correctly specified.
Definition util.hpp:211
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:259
bool aIsNegative
Definition util.hpp:261
std::array< std::string, 4 > splitNumberArray
Definition util.hpp:260
bool bIsNegative
Definition util.hpp:261
char value[N]
The value of the wrapper class. Stores the string object.
Definition util.hpp:241
constexpr StringLiteral(const char(&str)[N])
Initializes the wrapper class.
Definition util.hpp:233