arsenalgear-cpp
A library containing general purpose C++ utils.
utils.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
11 //====================================================
12 // Preprocessor settings
13 //====================================================
14 #pragma once
15 #ifndef ARSENALGEAR_UTILS_HPP
16 #define ARSENALGEAR_UTILS_HPP
17 
18 //====================================================
19 // Headers
20 //====================================================
21 
22 // STD headers
23 #include <stdexcept>
24 #include <type_traits>
25 #include <vector>
26 #include <string>
27 #include <codecvt>
28 #include <locale>
29 #include <sstream>
30 #include <stdlib.h>
31 #include <string_view>
32 #include <cmath>
33 
34 namespace agr
35  {
36  //====================================================
37  // Enum classes
38  //====================================================
39  enum class ANSI { first, generic };
40 
41  //====================================================
42  // Functions declaration
43  //====================================================
44  extern std::string multi( const std::string& element, unsigned int n_times );
45  extern std::vector <std::string> split_string( const std::string& input, const std::string& regex );
46 
47  //====================================================
48  // Functions definition
49  //====================================================
50 
51  // except_error_func
60  template <typename T_err = std::runtime_error>
61  inline T_err except_error_func( const std::string& beg = "", std::string var = nullptr, const std::string& end = "" )
62  {
63  using namespace std::literals::string_literals;
64 
65  static std::ostringstream oss;
66  oss.str( "" );
67  oss.clear();
68  oss << "\033[31m" << beg << " \""s << "\033[1m" << var << "\033[22m" << "\" "s << end << "\033[39m";
69 
70  return T_err( oss.str() );
71  }
72 
73  // isFloatingPoint
79  template <typename T>
80  inline bool isFloatingPoint( const T& )
81  {
82  return std::is_floating_point <T>::value;
83  }
84 
85  // one
92  template <typename T>
93  inline T one( const T& iterating_var )
94  {
95  std::vector<T> counter_;
96 
97  if( isFloatingPoint( iterating_var ) )
98  {
99  if( counter_.size() < 2 ) counter_.push_back( iterating_var );
100  if( counter_.size() == 2 ) return abs( abs( counter_.front() ) - abs( counter_.back() ) );
101  return static_cast <T> ( 0 );
102  }
103 
104  counter_.clear();
105  return 1;
106  }
107 
108  // StringConverter
116  template <class CharT>
117  std::conditional_t<std::is_same_v<CharT, char>, const std::basic_string<CharT>&, std::basic_string<CharT>>
118  StringConverter( const std::string& input_str )
119  {
120  if constexpr( std::is_same_v <CharT, char> )
121  {
122  return input_str;
123  }
124  else if constexpr( std::is_same_v <CharT, wchar_t> )
125  {
126  static std::wstring_convert <std::codecvt_utf8_utf16 <wchar_t>> converter_wchar_t;
127  return converter_wchar_t.from_bytes( input_str );
128  }
129  #ifndef __APPLE__
130  #if ( __cplusplus >= 202002L )
131  else if constexpr( std::is_same_v <CharT, char8_t> )
132  {
133  return reinterpret_cast <const char8_t*>( input_str.c_str() );
134  }
135  #endif
136  else if constexpr( std::is_same_v <CharT, char16_t> )
137  {
138  static std::wstring_convert <std::codecvt_utf8_utf16 <char16_t>, char16_t> converter_16_t;
139  return converter_16_t.from_bytes( input_str );
140  }
141  else if constexpr( std::is_same_v <CharT, char32_t> )
142  {
143  static std::wstring_convert <std::codecvt_utf8_utf16 <char32_t>, char32_t> converter_32_t;
144  return converter_32_t.from_bytes( input_str );
145  }
146  #endif
147  else
148  {
149  return StringConverter<CharT>( "" );
150  }
151  }
152 
153  // is_escape
163  template <typename T>
164  bool is_escape( const T& str, const ANSI& flag )
165  {
166  if constexpr( std::is_convertible_v <T, std::string_view> && ! std::is_same_v<T, std::nullptr_t> )
167  {
168  switch( flag )
169  {
170  case( ANSI::first ):
171  {
172  return ( std::string_view( str ).length() < 7 ) && ( str[0] == '\033' );
173  }
174  case( ANSI::generic ):
175  {
176  return ( std::string_view( str ).find( '\033' ) != std::string_view::npos );
177  }
178  }
179  }
180  return false;
181  }
182  }
183 
184 #endif
Definition: stream.cpp:22
std::conditional_t< std::is_same_v< CharT, char >, const std::basic_string< CharT > &, std::basic_string< CharT > > StringConverter(const std::string &input_str)
Function used to convert an std::string into other string types (std::wstring etc....
Definition: utils.hpp:118
std::string multi(const std::string &element, unsigned int n_times)
Function used to multiply a string for n times.
Definition: utils.cpp:36
bool isFloatingPoint(const T &)
Function to check if an expression (not a type) is a floating point or not. I know this function is a...
Definition: utils.hpp:80
T_err except_error_func(const std::string &beg="", std::string var=nullptr, const std::string &end="")
Function used to throw customized stdexception error. This function is extremely specific to my purpo...
Definition: utils.hpp:61
T one(const T &iterating_var)
Function to find the incremented unit of a loop. Not easy to understand its purpose without context,...
Definition: utils.hpp:93
bool is_escape(const T &str, const ANSI &flag)
This method is used to check if an input variable is an ANSI escape sequency or not.
Definition: utils.hpp:164
ANSI
Definition: utils.hpp:39
std::vector< std::string > split_string(const std::string &input, const std::string &regex)
Function used to split a string based on a certain reges.
Definition: utils.cpp:53