Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #ifndef SPLIT_H
  2. #define SPLIT_H
  3.  
  4. #include <string>
  5. #include <type_traits>
  6.  
  7. /* split function python-like */
  8. namespace Aurea {
  9.     template<class InputType>
  10.     InputType *split_by(const InputType &input, char condition, size_t input_length) {
  11.         /* check if input is valid */
  12.         static_assert(std::is_same<InputType, std::string>::value ||
  13.                       std::is_same<InputType, const char*>::value ||
  14.                       std::is_same<InputType, char*>::value,
  15.                       "split_by can be initialized only with std::string, const char* or char*");
  16.  
  17.         /* split data to array */
  18.         int cnd_amount = 0;
  19.        
  20.         /* calculate tab size */
  21.         for (const auto &c : input) {
  22.             if (c == condition) {
  23.                 cnd_amount++;
  24.             }
  25.         }
  26.  
  27.         /* create tab */
  28.         InputType *tab = new InputType[cnd_amount + 1];
  29.  
  30.         size_t index = 0;
  31.  
  32.         for (size_t i = 0; i < input_length; ++i) {
  33.             if (input[i] != condition) {
  34.                 tab[index] += input[i];
  35.             } else {
  36.                 if (i != 0 && i != input_length - 1) {
  37.                     ++index;
  38.                 }
  39.             }
  40.         }
  41.         return tab;
  42.     }
  43. }
  44.  
  45. #endif // !SPLIT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement