Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SPLIT_H
- #define SPLIT_H
- #include <string>
- #include <type_traits>
- /* split function python-like */
- namespace Aurea {
- template<class InputType>
- InputType *split_by(const InputType &input, char condition, size_t input_length) {
- /* check if input is valid */
- static_assert(std::is_same<InputType, std::string>::value ||
- std::is_same<InputType, const char*>::value ||
- std::is_same<InputType, char*>::value,
- "split_by can be initialized only with std::string, const char* or char*");
- /* split data to array */
- int cnd_amount = 0;
- /* calculate tab size */
- for (const auto &c : input) {
- if (c == condition) {
- cnd_amount++;
- }
- }
- /* create tab */
- InputType *tab = new InputType[cnd_amount + 1];
- size_t index = 0;
- for (size_t i = 0; i < input_length; ++i) {
- if (input[i] != condition) {
- tab[index] += input[i];
- } else {
- if (i != 0 && i != input_length - 1) {
- ++index;
- }
- }
- }
- return tab;
- }
- }
- #endif // !SPLIT_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement