1 #include <samchon/WeakString.hpp> 7 #include <samchon/library/Math.hpp> 13 const vector<std::string> WeakString::SPACE_ARRAY = {
" ",
"\t",
"\r",
"\n" };
18 WeakString::WeakString()
20 this->data_ =
nullptr;
23 WeakString::WeakString(
const char *data,
size_t size)
29 WeakString::WeakString(
const char *begin,
const char *end)
40 this->
size_ = std::char_traits<char>::length(data);
50 this->
data_ =
nullptr;
52 this->
data_ = il.begin();
54 this->
size_ = il.size();
58 this->
data_ = str.data();
59 this->
size_ = str.size();
81 return *(
data_ + index);
85 return *(
data_ + index);
95 for (
size_t i = startIndex; i <
size_; i++)
96 if (
data_[i] != delim[j++])
98 else if (j == delim.size())
99 return i - delim.size() + 1;
108 size_t j = delim.size() - 1;
110 for (
long long i = std::min(endIndex - 1,
size_ - 1); i >= 0; i--)
111 if (
data_[(
size_t)i] != delim[j])
112 j = delim.size() - 1;
123 std::vector<WeakString> wdelims(delims.size());
124 for (
size_t i = 0; i < delims.size(); i++)
125 wdelims[i] = delims[i];
127 return finds(wdelims, startIndex);
131 std::vector<size_t> positionVector;
132 positionVector.reserve(delims.size());
134 for (
size_t i = 0; i < delims.size(); i++)
135 positionVector.push_back(
find(delims[i], startIndex));
143 std::vector<WeakString> wdelims(delims.size());
144 for (
size_t i = 0; i < delims.size(); i++)
145 wdelims[i] = delims[i];
147 return rfinds(wdelims, endIndex);
151 vector<size_t> positionVector;
152 positionVector.reserve(delims.size());
156 for (
size_t i = 0; i < delims.size(); i++)
158 position =
rfind(delims[i], endIndex);
160 if (position != wstring::npos)
161 positionVector.push_back(position);
164 if (positionVector.empty() ==
true)
183 if (startIndex > endIndex)
184 swap(startIndex, endIndex);
186 if (startIndex == endIndex || startIndex >
size_ - 1)
189 if (endIndex >
size_)
196 if (start.empty() ==
true && end.empty() ==
true)
198 else if (start.empty() ==
true)
200 else if (end.empty() ==
true)
204 size_t startIndex =
find(start);
207 startIndex + start.size(),
208 find(end, startIndex + start.size())
214 size_t startIndex = 0;
218 std::queue<std::pair<size_t, size_t>> quoteList;
219 while ((x =
find(delim, startIndex)) !=
npos)
221 quoteList.push({ startIndex, x });
222 startIndex = x + delim.size();
224 quoteList.push({ startIndex,
size() });
227 std::vector<WeakString> vec;
228 vec.reserve(quoteList.size());
230 while (quoteList.empty() ==
false)
232 vec.push_back(
substring(quoteList.front().first, quoteList.front().second));
239 std::vector<WeakString> vec;
241 if (start.empty() ==
true && end.empty() ==
true)
243 else if (start == end)
245 std::queue<std::pair<size_t, size_t>> quoteList;
247 size_t x, prevX = -1, n = 0;
248 while ((x =
find(start, prevX + 1)) !=
npos)
251 quoteList.push({ prevX, x });
255 if (quoteList.size() == 0)
256 vec.push_back(*
this);
259 vec.reserve(quoteList.size());
260 while (quoteList.empty() ==
false)
262 std::pair<size_t, size_t> "e = quoteList.front();
263 vec.push_back(
substring(quote.first + start.size()));
272 vec.erase(vec.begin());
274 if (end.empty() ==
false)
275 for (
long long i = (
long long)vec.size() - 1; i >= 0; i--)
276 if (vec.at((
size_t)i).find(end) ==
npos)
277 vec.erase(vec.begin() + (size_t)i);
279 vec[(size_t)i] = vec[(
size_t)i].between(
"", end);
287 auto WeakString::trim(
const std::vector<WeakString> &delims)
const ->
WeakString 289 return ltrim(delims).rtrim(delims);
291 auto WeakString::ltrim(
const std::vector<WeakString> &delims)
const->
WeakString 296 while (str.
empty() ==
false)
298 std::vector<size_t> indexVec;
299 indexVec.reserve(delims.size());
301 for (
size_t i = 0; i < delims.size(); i++)
302 indexVec.push_back(str.
find(delims[i]));
304 indexPair = Math::minimum(indexVec);
318 auto WeakString::rtrim(
const std::vector<WeakString> &delims)
const->
WeakString 323 while (str.
empty() ==
false)
325 std::vector<size_t> indexVec;
326 indexVec.reserve(delims.size());
328 for (
size_t i = 0; i < delims.size(); i++)
330 size_t index = str.
rfind(delims[i]);
332 indexVec.push_back(index);
334 if (indexVec.empty() ==
true)
337 pairIndex = Math::maximum(indexVec);
363 return trim(vector<WeakString>({ delim }));
367 return ltrim(vector<WeakString>({ delim }));
371 return rtrim(vector<WeakString>({ delim }));
374 auto WeakString::trim(
const std::vector<std::string> &delims)
const ->
WeakString 376 std::vector<WeakString> wdelims(delims.size());
377 for (
size_t i = 0; i < delims.size(); i++)
378 wdelims[i] = delims[i];
380 return trim(wdelims);
382 auto WeakString::ltrim(
const std::vector<std::string> &delims)
const ->
WeakString 384 std::vector<WeakString> wdelims(delims.size());
385 for (
size_t i = 0; i < delims.size(); i++)
386 wdelims[i] = delims[i];
388 return ltrim(wdelims);
390 auto WeakString::rtrim(
const std::vector<std::string> &delims)
const ->
WeakString 392 std::vector<WeakString> wdelims(delims.size());
393 for (
size_t i = 0; i < delims.size(); i++)
394 wdelims[i] = delims[i];
396 return rtrim(wdelims);
404 size_t index =
find(before);
409 str.reserve(
size() - before.size() + after.size());
412 str.append(after.str());
413 str.append(
substr(index + before.size()).
str());
425 std::vector<std::pair<WeakString, WeakString>> wPairs(pairs.size());
426 for (
size_t i = 0; i < pairs.size(); i++)
427 wPairs[i] = { pairs[i].first, pairs[i].second };
431 auto WeakString::replaceAll(
const std::vector<std::pair<WeakString, WeakString>> &pairs)
const -> std::string
433 if (pairs.empty() ==
true)
436 std::list<std::pair<size_t, size_t>> foundPairList;
445 for (i = 0; i < pairs.size(); i++)
451 index =
find(pairs[i].first, index);
455 size -= pairs[i].first.size();
456 size += pairs[i].second.size();
458 foundPairList.push_back({ index++, i });
462 if (foundPairList.empty() ==
true)
465 foundPairList.sort();
469 str.reserve((
size_t)size);
473 while (foundPairList.empty() ==
false)
475 auto it = foundPairList.begin();
476 auto &before = pairs[it->second].first;
477 auto &after = pairs[it->second].second;
479 str.append(
substring(index, it->first).str());
480 str.append(after.str());
482 index = it->first + before.size();
483 foundPairList.pop_front();
485 if (index <= this->
size() - 1)
493 std::string &
str = this->
str();
494 for (
size_t i = 0; i < str.size(); i++)
495 if (
'A' <= str[i] && str[i] <=
'Z')
496 str[i] = tolower(str[i]);
502 std::string &
str = this->
str();
503 for (
size_t i = 0; i < str.size(); i++)
504 if (
'a' <= str[i] && str[i] <=
'z')
505 str[i] = toupper(str[i]);
513 auto WeakString::operator==(
const WeakString &
str)
const ->
bool 518 for (
size_t i = 0; i <
size(); i++)
519 if (this->
at(i) != str[i])
524 auto WeakString::operator<(
const WeakString &str)
const ->
bool 526 size_t minSize = std::min(
size(), str.
size());
528 for (
size_t i = 0; i < minSize; i++)
529 if (this->
at(i) == str[i])
531 else if (this->
at(i) < str[i])
536 if (this->
size() == minSize && this->
size() != str.
size())
550 WeakString::operator std::string()
auto substr(size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
Generates a substring.
auto empty() const -> bool
Tests wheter string is emtpy.
auto size() const -> size_t
Returns size of the characters which are being referenced.
auto toLowerCase() const -> std::string
Convert uppercase letters to lowercase.
auto getValue() -> T &
Get reference of value.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
auto str() const -> std::string
Get the string content.
auto operator[](size_t index) const -> const char &
Get character of string Returns a const reference to the character at the specified position...
static const std::vector< std::string > SPACE_ARRAY
An array containing whitespaces.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
static const size_t npos
Maximum value for size_t.
const char * data_
Referenced characters's pointer of begining position.
auto ltrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning of the specified string.
auto rfinds(const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) const -> IndexPair< WeakString >
Finds last occurence in string.
auto rfind(const WeakString &delim, size_t endIndex=SIZE_MAX) const -> size_t
Finds last occurence in string.
auto getIndex() const -> size_t
Get index.
auto finds(const std::vector< std::string > &delims, size_t startIndex=0) const -> IndexPair< WeakString >
Finds first occurence in string.
auto toUpperCase() const -> std::string
Convert uppercase letters to lowercase.
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
WeakString()
Default Constructor does not reference any character.
auto substring(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
auto at(size_t index) const -> const char &
Get character of string Returns a const reference to the character at the specified position...
auto rtrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the end of the specified string.
auto replaceAll(const WeakString &before, const WeakString &after) const -> std::string
Returns a string specified word is replaced.
auto replace(const WeakString &before, const WeakString &after) const -> std::string
Replace portion of string once.
size_t size_
(Specified) size of referenced characters
A pair of index and its value(T)
auto betweens(const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
Generates substrings.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
Top level namespace of products built from samchon.
A string class only references characeters, reference only.
auto data() const -> const char *
Get string data; referenced characeters.