Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TStrings.h
Go to the documentation of this file.
1#pragma once
2#include <misc/gen/TVector.h>
3
4namespace sf
5{
6
10template<typename T>
11class TStrings : public TVector<T>
12{
13 public:
21 typedef typename base_type::size_type size_type;
25 typedef typename base_type::value_type value_type;
37 TStrings() = default;
38
42 template<typename InputIterator>
43 TStrings(InputIterator first, InputIterator last)
45 {}
46
50 explicit TStrings(std::initializer_list<value_type> list)
51 : base_type(list)
52 {}
53
57 explicit TStrings(const base_type& list)
58 : base_type(list)
59 {}
60
65 explicit TStrings(size_type sz)
66 : base_type(sz)
67 {}
68
74 explicit TStrings(const T& str, const T& separator = ",")
75 {
76 explode(str, separator);
77 }
78
84 T join(const T& glue)
85 {
86 T rv;
87 // Iterate through the vector.
88 auto it = TVector<T>::begin();
89 while (it < TVector<T>::end())
90 {
91 rv.append(*it++);
92 // When not the end entry add the glue.
93 if (it < TVector<T>::end())
94 {
95 rv.append(glue);
96 }
97 }
98 return rv;
99 }
100
107 TStrings& explode(const T& str, const T& separator)
108 {
109 size_t ofs = 0, found = str.find_first_of(separator, ofs);
110 if (found != std::string::npos)
111 {
112 do
113 {
114 base_type::push_back(str.substr(ofs, found - ofs));
115 ofs = found + 1;
116 if (ofs == T::npos)
117 {
118 break;
119 }
120 found = str.find_first_of(separator, ofs);
121 } while (found != T::npos);
122 }
123 if (str.length() > ofs)
124 {
125 base_type::push_back(str.substr(ofs, str.length()));
126 }
127 return *this;
128 }
129
137 TStrings& split(const T& s, typename T::value_type sep, typename T::value_type delimiter = 0)
138 {
139
140 bool d = false;
141 size_type p = 0, l = 0, i = 0;
142 for (; i < s.length(); ++i)
143 {
144 // When the character is a separator
145 if (s[i] == sep && !d)
146 {
147 // First field is empty.
148 if (i == 0)
149 {
150 base_type::append({});
151 }
152 else
153 {
154 p += i - l;
155 base_type::append(s.substr(p, l));
156 l = 0;
157 p = 0;
158 }
159 }
160 else
161 {
162 // if text delimiter was given.
163 if (delimiter && s[i] == delimiter)
164 {
165 // Toggle delimiter flag when flag is encountered.
166 d = !d;
167 // Skip
168 if (d)
169 {
170 --p;
171 }
172 }
173 else
174 {
175 // Count the number of characters for the field.
176 ++l;
177 }
178 }
179 }
180 // Check for characters after last separator;
181 if (l)
182 {
183 p += i - l;
184 base_type::append(s.substr(p, l));
185 }
186 return *this;
187 }
188};
189
198
199}// namespace sf
Definition TVector.h:10
Counted vector of strings.
Definition TStrings.h:12
TIterator< value_type > iter_type
Iteration type of the template.
Definition TStrings.h:29
TStrings(const T &str, const T &separator=",")
Exploding constructor.
Definition TStrings.h:74
TStrings(InputIterator first, InputIterator last)
Initializing constructor using an iterator.
Definition TStrings.h:43
TVector< T > base_type
Base type of this template .
Definition TStrings.h:17
const iter_type const_iter_type
Iteration const type of the template.
Definition TStrings.h:33
TStrings(const base_type &list)
Initializer list constructor.
Definition TStrings.h:57
TStrings()=default
Default constructor.
TStrings(size_type sz)
Initializing constructor.
Definition TStrings.h:65
TStrings(std::initializer_list< value_type > list)
Initializer list constructor.
Definition TStrings.h:50
TStrings & explode(const T &str, const T &separator)
Splits all entries using the passed separator.
Definition TStrings.h:107
TStrings & split(const T &s, typename T::value_type sep, typename T::value_type delimiter=0)
Appends all split entries. Splits' the passed string using a separator and text delimiter.
Definition TStrings.h:137
base_type::value_type value_type
Value type contained by this vector template.
Definition TStrings.h:25
base_type::size_type size_type
Size type of this template.
Definition TStrings.h:21
T join(const T &glue)
Joins all entries using the passed glue.
Definition TStrings.h:84
Counted vector having additional methods and operators for ease of usage. This template class extends...
Definition TVector.h:20
T & first()
Gets the first element of the vector.
T & last()
Gets the last element of the vector.
Definition Application.h:10
TStrings< std::wstring > wstrings
Vector of std::wstrings with additional functionality.
Definition TStrings.h:197
TStrings< std::string > strings
Vector of std::strings with additional functionality.
Definition TStrings.h:193