stdTools.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 #include <vector>
10 #include <sstream>
11 #include <iostream>
12 #include <string>
13 #include <iomanip>
14 #include <algorithm>
15 #include <chrono>
16 
20 namespace u32char {
21  extern const char32_t Null;
22  extern const char32_t Return;
23  extern const char32_t CarrierReturn;
24  extern const char32_t Tabulation;
25  extern const char32_t Suppress;
26  extern const char32_t Delete;
27  extern const char32_t Space;
28  extern const char32_t Escape;
29 
35  bool isWhiteChar(char32_t _val);
42  bool isSpecialChar(char32_t _val);
49  bool isInteger(char32_t _val);
55  int32_t toInt(char32_t _val);
61  char32_t changeOrder(char32_t _val);
68  int8_t convertUtf8(char32_t _val, char _output[5]);
69  #if __CPP_VERSION__ >= 2011
70  std::string convertToUtf8(const std::u32string& _input);
71  #endif
72 };
73 
77 namespace utf8 {
83  int8_t theoricLen(const char _input);
89  bool theoricFirst(const char _input);
95  char32_t convertChar32(const char* _input);
96  #if __CPP_VERSION__ >= 2011
97  std::u32string convertUnicode(const std::string& _input);
98  #endif
99 
102  class iterator {
103  private:
104  char32_t m_value;
105  std::string* m_data;
106  int64_t m_current;
107  public:
112  m_value(u32char::Null),
113  m_data(nullptr),
114  m_current(0) {
115  // nothing to do ...
116  };
121  iterator(std::string& _str) :
122  m_value(u32char::Null),
123  m_data(&_str),
124  m_current(0) {
125  // nothing to do ...
126  };
132  iterator(std::string& _str, const std::string::iterator& _pos) :
133  m_value(u32char::Null),
134  m_data(&_str),
135  m_current(0) {
136  if (m_data != nullptr) {
137  m_current = std::distance(m_data->begin(), _pos);
138  }
139  };
145  iterator(std::string& _str, size_t _pos) :
146  m_value(u32char::Null),
147  m_data(&_str),
148  m_current(0) {
149  if (m_data != nullptr) {
150  if (_pos > m_data->size()) {
151  m_current = m_data->size();
152  } else {
153  m_current = _pos;
154  }
155  }
156  };
162  iterator(std::string* _str, const std::string::iterator& _pos) :
163  m_value(u32char::Null),
164  m_data(_str),
165  m_current(0) {
166  if (m_data != nullptr) {
167  m_current = std::distance(m_data->begin(), _pos);
168  }
169  };
175  iterator(std::string* _str, size_t _pos) :
176  m_value(u32char::Null),
177  m_data(_str),
178  m_current(0) {
179  if (m_data != nullptr) {
180  if (_pos > m_data->size()) {
181  m_current = m_data->size();
182  } else {
183  m_current = _pos;
184  }
185  }
186  };
191  iterator(const iterator& _obj):
192  m_value(u32char::Null),
193  m_data(_obj.m_data),
194  m_current(_obj.m_current) {
195  // nothing to do ...
196  };
202  iterator& operator=(const iterator & _obj) {
203  m_current = _obj.m_current;
204  m_data = _obj.m_data;
205  m_value = u32char::Null;
206  return *this;
207  };
211  virtual ~iterator() {
212  m_current = 0;
213  m_data = nullptr;
214  m_value = u32char::Null;
215  };
220  operator size_t () const {
221  if (m_data == nullptr) {
222  return 0;
223  }
224  if (m_current < 0) {
225  return 0;
226  }
227  if (m_current > (int64_t)m_data->size()) {
228  return m_data->size();
229  }
230  return (size_t)m_current;
231  };
236  iterator& operator++ ();
241  iterator& operator-- ();
246  iterator operator++ (int32_t) {
247  iterator it(*this);
248  ++(*this);
249  return it;
250  };
255  iterator operator-- (int32_t) {
256  iterator it(*this);
257  --(*this);
258  return it;
259  };
265  bool operator== (const iterator& _obj) const {
266  if ( m_current == _obj.m_current
267  && m_data == _obj.m_data) {
268  return true;
269  }
270  return false;
271  };
277  bool operator!= (const iterator& _obj) const {
278  if ( m_current != _obj.m_current
279  || m_data != _obj.m_data) {
280  return true;
281  }
282  return false;
283  };
289  bool operator<= (const iterator& _obj) const {
290  if (m_data != _obj.m_data) {
291  return false;
292  }
293  if (m_current <= _obj.m_current) {
294  return true;
295  }
296  return false;
297  };
303  bool operator>= (const iterator& _obj) const {
304  if (m_data != _obj.m_data) {
305  return false;
306  }
307  if (m_current >= _obj.m_current) {
308  return true;
309  }
310  return false;
311  };
317  bool operator< (const iterator& _obj) const {
318  if (m_data != _obj.m_data) {
319  return false;
320  }
321  if (m_current < _obj.m_current) {
322  return true;
323  }
324  return false;
325  };
331  bool operator> (const iterator& _obj) const {
332  if (m_data != _obj.m_data) {
333  return false;
334  }
335  if (m_current > _obj.m_current) {
336  return true;
337  }
338  return false;
339  };
344  char32_t operator* ();
349  size_t getPos() const {
350  if (m_data == nullptr) {
351  return 0;
352  }
353  if (m_current < 0) {
354  return 0;
355  }
356  if (m_current >= (int64_t)m_data->size()) {
357  return m_data->size()-1;
358  }
359  return (size_t)m_current;
360  };
366  iterator operator+ (const int64_t _val) const {
367  iterator tmpp(*this);
368  for (int64_t iii=0; iii<_val; ++iii) {
369  ++tmpp;
370  }
371  return tmpp;
372  };
376  iterator operator+ (const int32_t _val) const {
377  iterator tmpp(*this);
378  for (int64_t iii=0; iii<_val; ++iii) {
379  ++tmpp;
380  }
381  return tmpp;
382  };
386  iterator operator+ (const size_t _val) const {
387  iterator tmpp(*this);
388  for (int64_t iii=0; iii<(int64_t)_val; ++iii) {
389  ++tmpp;
390  }
391  return tmpp;
392  };
398  iterator operator- (const int64_t _val) const {
399  iterator tmpp(*this);
400  for (int64_t iii=0; iii<_val; ++iii) {
401  --tmpp;
402  }
403  return tmpp;
404  };
408  iterator operator- (const int32_t _val) const {
409  iterator tmpp(*this);
410  for (int64_t iii=0; iii<_val; ++iii) {
411  --tmpp;
412  }
413  return tmpp;
414  };
418  iterator operator- (const size_t _val) const {
419  iterator tmpp(*this);
420  for (int64_t iii=0; iii<(int64_t)_val; ++iii) {
421  --tmpp;
422  }
423  return tmpp;
424  };
425  /*
426  iterator begin() const {
427  return iterator(m_data);
428  }
429  iterator end() const {
430  return --iterator(m_data, m_data.end());
431  }
432  */
433  };
434 };
435 
436 namespace std {
437  #if (defined(__TARGET_OS__MacOs) || defined(__TARGET_OS__Windows))
438  using u32string = std::basic_string<char32_t>;
439  #endif
440  #if (defined(__TARGET_OS__Android))
441  std::string to_string(int _val);
444  std::string to_string(long _val);
446  std::string to_string(long long _val);
448  std::string to_string(unsigned _val);
450  std::string to_string(unsigned long _val);
452  std::string to_string(unsigned long long _val);
454  std::string to_string(float _val);
456  std::string to_string(double _val);
458  std::string to_string(long double _val);
460  double stod(const std::string& _str, size_t* _idx = 0);
462  float stof(const std::string& _str, size_t* _idx = 0);
464  int stoi(const std::string& _str, size_t* _idx = 0, int _base = 10);
466  long stol(const std::string& _str, size_t* _idx = 0, int _base = 10);
468  long double stold(const std::string& _str, size_t* _idx = 0);
470  long long stoll(const std::string& _str, size_t* _idx = 0, int _base = 10);
472  unsigned long stoul(const std::string& _str, size_t* _idx = 0, int _base = 10);
474  unsigned long long stoull(const std::string& _str, size_t* _idx = 0, int _base = 10);
475  #endif
476 };
477 namespace etk {
478  // these declaration is to prevent some under template declaration of unknown type
484  template <class TYPE>
485  std::string to_string(const TYPE& _variable);
491  template <class TYPE>
492  std::string to_string(const std::vector<TYPE>& _list) {
493  std::string out = "{";
494  for (size_t iii=0; iii<_list.size(); ++iii) {
495  if (iii!=0) {
496  out += ";";
497  }
498  out+= etk::to_string(_list[iii]);
499  }
500  out += "}";
501  return out;
502  }
503  #if __CPP_VERSION__ >= 2011
504  template <class TYPE>
505  std::u32string to_u32string(const TYPE& _variable);
506  #endif
507  // these declaration is to prevent some under template declaration of unknown type
514  template <class TYPE>
515  bool from_string(TYPE& _variableRet, const std::string& _value);
516  #if __CPP_VERSION__ >= 2011
517  template <class TYPE>
518  bool from_string(TYPE& _variableRet, const std::u32string& _value);
519  #endif
520 
521  // TODO : Change this in :
522  // TODO : template <typename TYPE> TYPE string_to<TYPE>(const std::u32string& _value); ==> check exceptions ...
524  long double string_to_long_double(const std::string& _str);
525  #if __CPP_VERSION__ >= 2011
526  long double string_to_long_double(const std::u32string& _str);
527  #endif
528  double string_to_double(const std::string& _str);
530  #if __CPP_VERSION__ >= 2011
531  double string_to_double(const std::u32string& _str);
532  #endif
533  float string_to_float(const std::string& _str);
535  #if __CPP_VERSION__ >= 2011
536  float string_to_float(const std::u32string& _str);
537  #endif
538  int8_t string_to_int8_t(const std::string& _str, int _base = 10);
540  #if __CPP_VERSION__ >= 2011
541  int8_t string_to_int8_t(const std::u32string& _str, int _base = 10);
542  #endif
543  int16_t string_to_int16_t(const std::string& _str, int _base = 10);
545  #if __CPP_VERSION__ >= 2011
546  int16_t string_to_int16_t(const std::u32string& _str, int _base = 10);
547  #endif
548  int32_t string_to_int32_t(const std::string& _str, int _base = 10);
550  #if __CPP_VERSION__ >= 2011
551  int32_t string_to_int32_t(const std::u32string& _str, int _base = 10);
552  #endif
553  int64_t string_to_int64_t(const std::string& _str, int _base = 10);
555  #if __CPP_VERSION__ >= 2011
556  int64_t string_to_int64_t(const std::u32string& _str, int _base = 10);
557  #endif
558  uint8_t string_to_uint8_t(const std::string& _str, int _base = 10);
560  #if __CPP_VERSION__ >= 2011
561  uint8_t string_to_uint8_t(const std::u32string& _str, int _base = 10);
562  #endif
563  uint16_t string_to_uint16_t(const std::string& _str, int _base = 10);
565  #if __CPP_VERSION__ >= 2011
566  uint16_t string_to_uint16_t(const std::u32string& _str, int _base = 10);
567  #endif
568  uint32_t string_to_uint32_t(const std::string& _str, int _base = 10);
570  #if __CPP_VERSION__ >= 2011
571  uint32_t string_to_uint32_t(const std::u32string& _str, int _base = 10);
572  #endif
573  uint64_t string_to_uint64_t(const std::string& _str, int _base = 10);
575  #if __CPP_VERSION__ >= 2011
576  uint64_t string_to_uint64_t(const std::u32string& _str, int _base = 10);
577  #endif
578  bool string_to_bool(const std::string& _str);
580  #if __CPP_VERSION__ >= 2011
581  bool string_to_bool(const std::u32string& _str);
582  #endif
583  std::string tolower(std::string _obj);
585  #if __CPP_VERSION__ >= 2011
586  std::u32string tolower(std::u32string _obj);
588  #endif
589  std::string toupper(std::string _obj);
591  #if __CPP_VERSION__ >= 2011
592  std::u32string toupper(std::u32string _obj);
594  #endif
595  bool compare_no_case(const std::string& _obj, const std::string& _val);
597  #if __CPP_VERSION__ >= 2011
598  bool compare_no_case(const std::u32string& _obj, const std::u32string& _val);
600  #endif
601  bool end_with(const std::string& _obj, const std::string& _val, bool _caseSensitive = true);
603  #if __CPP_VERSION__ >= 2011
604  bool end_with(const std::u32string& _obj, const std::u32string& _val, bool _caseSensitive = true);
606  #endif
607  bool start_with(const std::string& _obj, const std::string& _val, bool _caseSensitive = true);
609  #if __CPP_VERSION__ >= 2011
610  bool start_with(const std::u32string& _obj, const std::u32string& _val, bool _caseSensitive = true);
612  #endif
613  std::string replace(const std::string& _obj, char _val, char _replace);
615  #if __CPP_VERSION__ >= 2011
616  std::u32string replace(const std::u32string& _obj, char32_t _val, char32_t _replace);
618  #endif
619  std::string extract_line(const std::string& _obj, int32_t _pos);
621  #if __CPP_VERSION__ >= 2011
622  std::u32string extract_line(const std::u32string& _obj, int32_t _pos);
624  #endif
625  std::vector<std::string> split(const std::string& _input, char _val);
627  #if __CPP_VERSION__ >= 2011
628  std::vector<std::u32string> split(const std::u32string& _input, char32_t _val);
630  #endif
631  void sort(std::vector<std::string *>& _list);
633  #if __CPP_VERSION__ >= 2011
634  void sort(std::vector<std::u32string *>& _list);
636  #endif
637  template<typename T, typename T2>
639  bool isIn(const T& _val, const std::vector<T2>& _list) {
640  for (size_t iii=0; iii<_list.size(); ++iii) {
641  if (_list[iii] == _val) {
642  return true;
643  }
644  }
645  return false;
646  }
647 };
648 
649 namespace std {
657  template <class TYPE> const TYPE& avg(const TYPE& _min, const TYPE& _val, const TYPE& _max) {
658  return std::min(std::max(_min,_val),_max);
659  }
660 };
661 
662 namespace etk {
670  template <class TYPE> const TYPE& avg(const TYPE& _min, const TYPE& _val, const TYPE& _max) {
671  return std::min(std::max(_min,_val),_max);
672  }
673 };
674 
675 namespace std {
677  std::ostream& operator <<(std::ostream& _os, const std::string& _obj);
679  std::ostream& operator <<(std::ostream& _os, const std::vector<std::string>& _obj);
680  #if __CPP_VERSION__ >= 2011
681  std::ostream& operator <<(std::ostream& _os, const std::u32string& _obj);
684  std::ostream& operator <<(std::ostream& _os, const std::vector<std::u32string>& _obj);
685  #endif
686  std::ostream& operator <<(std::ostream& _os, const std::vector<float>& _obj);
689  std::ostream& operator <<(std::ostream& _os, const std::vector<double>& _obj);
691  std::ostream& operator <<(std::ostream& _os, const std::vector<int64_t>& _obj);
693  std::ostream& operator <<(std::ostream& _os, const std::vector<uint64_t>& _obj);
695  std::ostream& operator <<(std::ostream& _os, const std::vector<int32_t>& _obj);
697  std::ostream& operator <<(std::ostream& _os, const std::vector<uint32_t>& _obj);
699  std::ostream& operator <<(std::ostream& _os, const std::vector<int16_t>& _obj);
701  std::ostream& operator <<(std::ostream& _os, const std::vector<uint16_t>& _obj);
703  std::ostream& operator <<(std::ostream& _os, const std::vector<int8_t>& _obj);
705  std::ostream& operator <<(std::ostream& _os, const std::vector<uint8_t>& _obj);
707  std::ostream& operator <<(std::ostream& _os, const std::chrono::system_clock::time_point& _obj);
709  std::ostream& operator <<(std::ostream& _os, const std::chrono::steady_clock::time_point& _obj);
710 }
711 
717 int32_t strlen(const char32_t* _data);
718 
719 #if (defined(__TARGET_OS__Windows))
720  #define M_PI 3.14159265358979323846
721 #endif
722 
723 
const char32_t Suppress
Value BS (SUPPRESS)
iterator(const iterator &_obj)
Recopy constructor.
Definition: stdTools.hpp:191
iterator()
Basic constructor that is not link on a string.
Definition: stdTools.hpp:111
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
STL namespace.
bool theoricFirst(const char _input)
When parsing a string in a reverse mode, we need to know if we get the first char.
char32_t changeOrder(char32_t _val)
Change order of the value to have an order of display with A->Z and after a->z and after 0->9 and aft...
const char32_t CarrierReturn
Value &#39;\r&#39; CR.
bool isSpecialChar(char32_t _val)
check if the current element is NOT [a-zA-Z0-9]
const char32_t Return
Value &#39;\n&#39;.
int32_t strlen(const char32_t *_data)
Claculate the size of a string (unicode)
size_t getPos() const
Get the position in the buffer.
Definition: stdTools.hpp:349
int32_t toInt(char32_t _val)
Convert char32_t in an interfer.
const TYPE & avg(const TYPE &_min, const TYPE &_val, const TYPE &_max)
in std, we have min, max but not avg ==> it is missing... the Defineing avg template.
Definition: stdTools.hpp:670
Unicode simple wrapper interface.
Definition: stdTools.hpp:20
const char32_t Tabulation
Value &#39;\t&#39; TAB.
Iterator on a simple std::string that contain utf8 value.
Definition: stdTools.hpp:102
virtual ~iterator()
Basic destructor.
Definition: stdTools.hpp:211
iterator & operator=(const iterator &_obj)
Asignation operator.
Definition: stdTools.hpp:202
iterator(std::string &_str)
Basic begin constructor link at the start of the string.
Definition: stdTools.hpp:121
char32_t convertChar32(const char *_input)
Convert a char* in a unicode value.
iterator(std::string *_str, size_t _pos)
Basic position constructor link at the _pos position of the string.
Definition: stdTools.hpp:175
UTF-8 simple wrapper interface.
Definition: stdTools.hpp:77
const char32_t Null
Value &#39;\0&#39;.
iterator(std::string &_str, size_t _pos)
Basic position constructor link at the _pos position of the string.
Definition: stdTools.hpp:145
const char32_t Delete
Value DEL.
int8_t theoricLen(const char _input)
Get the size of an utf8 char with his first char.
std::string to_string(const std::vector< TYPE > &_list)
Template to declare convertion from std::vector<anything> in std::string.
Definition: stdTools.hpp:492
bool from_string(TYPE &_variableRet, const std::string &_value)
Template to declare convertion from string to anything.
int8_t convertUtf8(char32_t _val, char _output[5])
Conver unicode in UTF8 value.
iterator(std::string &_str, const std::string::iterator &_pos)
Basic position constructor link at the _pos position of the string.
Definition: stdTools.hpp:132
std::string to_string(const TYPE &_variable)
Template to declare convertion from anything in std::string.
const char32_t Space
Value &#39; &#39; SPACE.
bool isWhiteChar(char32_t _val)
check if the current element is white or not : &#39;\t&#39; &#39;\n&#39; &#39;\r&#39; &#39; &#39;
const char32_t Escape
Value ESC Escape.
iterator(std::string *_str, const std::string::iterator &_pos)
Basic position constructor link at the _pos position of the string.
Definition: stdTools.hpp:162