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, typename CharType>
11class TStrings : public TVector<T>
12{
13 public:
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, CharType sep, CharType 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 {
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
Counted vector having function names compatible with Borland C++ templates.
Definition TVector.h:398
Counted vector of strings.
Definition TStrings.h:12
TIterator< value_type > iter_type
Iteration type of the template.
Definition TStrings.h:29
TStrings & explode(const T &str, const T &separator)
Splits all entries using the passed separator.
Definition TStrings.h:107
TStrings()=default
Default constructor.
TStrings(std::initializer_list< value_type > list)
Initializer list constructor.
Definition TStrings.h:50
TVector< T > base_type
Base type of this template .
Definition TStrings.h:17
base_type::value_type value_type
Value type contained by this vector template.
Definition TStrings.h:25
TStrings & split(const T &s, CharType sep, CharType delimiter=0)
Appends all split entries. Splits' the passed string using a separator and text delimiter.
Definition TStrings.h:137
TStrings(const base_type &list)
Initializer list constructor.
Definition TStrings.h:57
base_type::size_type size_type
Size type of this template.
Definition TStrings.h:21
const iter_type const_iter_type
Iteration const type of the template.
Definition TStrings.h:33
T join(const T &glue)
Joins all entries using the passed glue.
Definition TStrings.h:84
TStrings(InputIterator first, InputIterator last)
Initializing constructor using an iterator.
Definition TStrings.h:43
TStrings(const T &str, const T &separator=",")
Exploding constructor.
Definition TStrings.h:74
TStrings(size_type sz)
Initializing constructor.
Definition TStrings.h:65
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
base_type::size_type size_type
Size type of this template.
Definition TVector.h:34
T & first()
Gets the first element of the vector.
Definition TVector.h:631
TVector & append(const T &t)
Appends an entry to the vectors items at the end of the vector.
Definition TVector.h:490
base_type::value_type value_type
Value type contained by this vector template.
Definition TVector.h:38
T & last()
Gets the last element of the vector.
Definition TVector.h:292
Definition Application.h:10
TStrings< std::wstring, std::wstring::value_type > wstrings
Vector of std::wstrings with additional functionality.
Definition TStrings.h:197
TStrings< std::string, std::string::value_type > strings
Vector of std::strings with additional functionality.
Definition TStrings.h:193