Visual Computing Library
Loading...
Searching...
No Matches
tokenizer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MISC_TOKENIZER_H
24#define VCL_MISC_TOKENIZER_H
25
26#include <vclib/types.h>
27
28#include <string>
29#include <vector>
30
31namespace vcl {
32
37{
38 std::vector<char> mSeparators = {'\0'};
39
40 std::vector<std::string> mSplitted;
41
42public:
43 using iterator = std::vector<std::string>::const_iterator;
44
45 Tokenizer() = default;
46
47 Tokenizer(const char* string, char separator, bool jumpEmptyTokens = true) :
48 mSeparators({separator})
49 {
50 split(string, jumpEmptyTokens);
51 }
52
54 const char* string,
55 const std::vector<char>& separators,
56 bool jumpEmptyTokens = true) : mSeparators(separators)
57 {
58 split(string, jumpEmptyTokens);
59 }
60
62 const std::string& string,
63 char separator,
64 bool jumpEmptyTokens = true) : mSeparators({separator})
65 {
66 split(string.c_str(), jumpEmptyTokens);
67 }
68
70 const std::string& string,
71 const std::vector<char>& separators,
72 bool jumpEmptyTokens = true) : mSeparators(separators)
73 {
74 split(string.c_str(), jumpEmptyTokens);
75 }
76
77 iterator begin() const { return mSplitted.begin(); }
78
79 iterator end() const { return mSplitted.end(); }
80
81 unsigned long int size() const { return (unsigned long) mSplitted.size(); }
82
83 const std::string& operator[](uint i) const { return mSplitted[i]; }
84
85private:
86 void split(const char* str, bool jumpEmptyTokens = true)
87 {
88 // https://stackoverflow.com/questions/53849/
89 mSplitted.clear();
90 if (*str != '\0') {
91 do {
92 const char* begin = str;
93
94 while (isDiffFromAllSeparators(str) && *str)
95 str++;
96 if (begin != str)
97 mSplitted.push_back(std::string(begin, str));
98 else if (!jumpEmptyTokens) {
99 mSplitted.push_back(std::string());
100 }
101 } while ('\0' != *str++);
102 }
103 }
104
105 bool isDiffFromAllSeparators(const char* str)
106 {
107 bool diffFromAllSeparators = true;
108
109 uint i = 0;
110 while (diffFromAllSeparators && i < mSeparators.size()) {
111 diffFromAllSeparators = *str != mSeparators[i++];
112 }
113
115 }
116};
117
118} // namespace vcl
119
120#endif // VCL_MISC_TOKENIZER_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The Tokenizer class.
Definition tokenizer.h:37