8 #include <samchon/IndexPair.hpp> 10 #include <samchon/library/IOperator.hpp> 11 #include <samchon/library/Math.hpp> 56 static const size_t npos = -1;
79 this->data_ =
nullptr;
146 this->size_ = std::char_traits<char>::length(data);
172 this->data_ =
nullptr;
174 this->data_ = il.begin();
176 this->size_ = il.size();
188 this->data_ = str.data();
189 this->size_ = str.size();
206 auto data() const -> const
char*
233 return data_ ==
nullptr || size_ == 0;
242 auto at(
size_t index)
const ->
const char&
245 throw std::out_of_range(
"out of range.");
247 return *(data_ + index);
255 return *(data_ + index);
277 for (
size_t i = startIndex; i <
size_; i++)
278 if (data_[i] != delim[j++])
280 else if (j == delim.
size())
281 return i - delim.
size() + 1;
299 if (
empty() ==
true || endIndex == 0)
302 size_t j = delim.
size() - 1;
304 for (
long long i = std::min<size_t>(endIndex - 1, size_ - 1); i >= 0; i--)
305 if (data_[(
size_t)i] != delim[j])
306 j = delim.
size() - 1;
332 std::vector<WeakString> wdelims(delims.size());
333 for (
size_t i = 0; i < delims.size(); i++)
334 wdelims[i] = delims[i];
336 return finds(wdelims, startIndex);
341 std::vector<size_t> positionVector;
342 positionVector.reserve(delims.size());
344 for (
size_t i = 0; i < delims.size(); i++)
345 positionVector.push_back(
find(delims[i], startIndex));
368 std::vector<WeakString> wdelims(delims.size());
369 for (
size_t i = 0; i < delims.size(); i++)
370 wdelims[i] = delims[i];
372 return rfinds(wdelims, endIndex);
377 std::vector<size_t> positionVector;
378 positionVector.reserve(delims.size());
382 for (
size_t i = 0; i < delims.size(); i++)
384 position =
rfind(delims[i], endIndex);
386 if (position != std::string::npos)
387 positionVector.push_back(position);
390 if (positionVector.empty() ==
true)
421 if (
size > size_ || startIndex +
size > size_)
422 size = size_ - startIndex;
445 if (startIndex > endIndex)
446 std::swap(startIndex, endIndex);
448 if (startIndex == endIndex || startIndex > size_ - 1)
451 if (endIndex > size_)
454 return WeakString(data_ + startIndex, data_ + endIndex);
477 if (start.empty() ==
true && end.
empty() ==
true)
479 else if (start.empty() ==
true)
481 else if (end.
empty() ==
true)
485 size_t startIndex =
find(start);
489 startIndex + start.size(),
490 find(end, startIndex + start.
size())
504 size_t startIndex = 0;
508 std::queue<std::pair<size_t, size_t>> quoteList;
509 while ((x =
find(delim, startIndex)) != npos)
511 quoteList.push({ startIndex, x });
512 startIndex = x + delim.size();
514 quoteList.push({ startIndex,
size() });
517 std::vector<WeakString> vec;
518 vec.reserve(quoteList.size());
520 while (quoteList.empty() ==
false)
522 vec.push_back(
substring(quoteList.front().first, quoteList.front().second));
550 std::vector<WeakString> vec;
552 if (start.empty() ==
true && end.
empty() ==
true)
554 else if (start == end)
556 std::queue<std::pair<size_t, size_t>> quoteList;
558 size_t x, prevX = -1, n = 0;
559 while ((x =
find(start, prevX + 1)) !=
npos)
562 quoteList.push({ prevX, x });
566 if (quoteList.size() == 0)
567 vec.push_back(*
this);
570 vec.reserve(quoteList.size());
571 while (quoteList.empty() ==
false)
573 std::pair<size_t, size_t> "e = quoteList.front();
574 vec.push_back(
substring(quote.first + start.size()));
583 vec.erase(vec.begin());
585 if (end.
empty() ==
false)
586 for (
long long i = (
long long)vec.size() - 1; i >= 0; i--)
587 if (vec.at((
size_t)i).find(end) ==
npos)
588 vec.erase(vec.begin() + (size_t)i);
590 vec[(size_t)i] = vec[(
size_t)i].between(
"", end);
600 return ltrim().rtrim();
604 static const std::vector<std::string> SPACE_ARRAY = {
" ",
"\t",
"\r",
"\n" };
606 return ltrim(SPACE_ARRAY);
610 static const std::vector<std::string> SPACE_ARRAY = {
" ",
"\t",
"\r",
"\n" };
612 return rtrim(SPACE_ARRAY);
617 return ltrim(delim).rtrim(delim);
621 return ltrim(std::vector<WeakString>({delim}));
625 return rtrim(std::vector<WeakString>({ delim }));
636 return ltrim(delims).rtrim(delims);
647 std::vector<WeakString> wdelims(delims.size());
648 for (
size_t i = 0; i < delims.size(); i++)
649 wdelims[i] = delims[i];
651 return ltrim(wdelims);
662 std::vector<WeakString> wdelims(delims.size());
663 for (
size_t i = 0; i < delims.size(); i++)
664 wdelims[i] = delims[i];
666 return rtrim(wdelims);
669 auto trim(
const std::vector<WeakString> &delims)
const ->
WeakString 671 return ltrim(delims).rtrim(delims);
673 auto ltrim(
const std::vector<WeakString> &delims)
const ->
WeakString 678 while (str.
empty() ==
false)
680 std::vector<size_t> indexVec;
681 indexVec.reserve(delims.size());
683 for (
size_t i = 0; i < delims.size(); i++)
684 indexVec.push_back(str.
find(delims[i]));
700 auto rtrim(
const std::vector<WeakString> &delims)
const ->
WeakString 705 while (str.
empty() ==
false)
707 std::vector<size_t> indexVec;
708 indexVec.reserve(delims.size());
710 for (
size_t i = 0; i < delims.size(); i++)
712 size_t index = str.
rfind(delims[i]);
714 indexVec.push_back(index);
716 if (indexVec.empty() ==
true)
742 size_t index =
find(before);
747 str.reserve(
size() - before.size() + after.size());
750 str.append(after.str());
751 str.append(
substr(index + before.size()).
str());
775 auto replaceAll(
const std::vector<std::pair<std::string, std::string>> &pairs) const ->
std::
string 777 std::vector<std::pair<WeakString, WeakString>> wPairs(pairs.size());
778 for (
size_t i = 0; i < pairs.size(); i++)
779 wPairs[i] = { pairs[i].first, pairs[i].second };
784 auto replaceAll(
const std::vector<std::pair<WeakString, WeakString>> &pairs)
const -> std::string
786 if (pairs.empty() ==
true)
789 std::list<std::pair<size_t, size_t>> foundPairList;
798 for (i = 0; i < pairs.size(); i++)
804 index =
find(pairs[i].first, index);
808 size -= pairs[i].first.size();
809 size += pairs[i].second.size();
811 foundPairList.push_back({ index++, i });
815 if (foundPairList.empty() ==
true)
818 foundPairList.sort();
822 str.reserve((
size_t)size);
826 while (foundPairList.empty() ==
false)
828 auto it = foundPairList.begin();
829 auto &before = pairs[it->second].first;
830 auto &after = pairs[it->second].second;
832 str.append(
substring(index, it->first).str());
833 str.append(after.str());
835 index = it->first + before.size();
836 foundPairList.pop_front();
838 if (index <= this->
size() - 1)
853 std::string &
str = this->
str();
855 for (
size_t i = 0; i < str.size(); i++)
856 if (
'A' <= str[i] && str[i] <=
'Z')
857 str[i] = tolower(str[i]);
871 std::string &
str = this->
str();
873 for (
size_t i = 0; i < str.size(); i++)
874 if (
'a' <= str[i] && str[i] <=
'z')
875 str[i] = toupper(str[i]);
885 if (size_ != str.
size_)
887 else if (data_ == str.
data_)
890 for (
size_t i = 0; i <
size(); i++)
891 if (data_[i] != str.
data_[i])
897 auto operator<(
const WeakString &str)
const ->
bool 899 size_t minSize = std::min<size_t>(
size(), str.
size());
901 for (
size_t i = 0; i < minSize; i++)
902 if (this->
at(i) == str[i])
904 else if (this->
at(i) < str[i])
909 if (this->
size() == minSize && this->
size() != str.
size())
926 auto str() const ->
std::
string 928 return std::string(data_, data_ + size_);
931 operator std::string()
auto data() const -> const char *
Get string data; referenced characeters.
auto str() const -> std::string
Get the string content.
WeakString(const std::string &str)
Constructor by string.
WeakString(const char &ch)
Constructor by a single character.
auto empty() const -> bool
Tests wheter string is emtpy.
auto rfinds(const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) const -> IndexPair< WeakString >
Finds last occurence in string.
auto at(size_t index) const -> const char &
Get character of string Returns a const reference to the character at the specified position...
WeakString(const char *data)
Constructor by characters.
auto getValue() -> T &
Get reference of value.
WeakString()
Default Constructor does not reference any character.
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.
auto toLowerCase() const -> std::string
Convert uppercase letters to lowercase.
auto betweens(const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
Generates substrings.
WeakString(std::initializer_list< char > &il)
Constructor by a initializer list.
auto finds(const std::vector< std::string > &delims, size_t startIndex=0) const -> IndexPair< WeakString >
Finds first occurence in string.
static const std::vector< std::string > SPACE_ARRAY
An array containing whitespaces.
static const size_t npos
Maximum value for size_t.
const char * data_
Referenced characters's pointer of begining position.
auto replaceAll(const std::vector< std::pair< std::string, std::string >> &pairs) const -> std::string
Returns a string specified words are replaced.
static auto minimum(const _Cont &container) -> IndexPair< T >
Calculate minimum value with its index.
WeakString(const char *data, size_t size)
Constructor by characters with specified size.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
auto substring(size_t startIndex, size_t endIndex=SIZE_MAX) const -> WeakString
Generates a substring.
auto trim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning and end of the specified string.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
size_t size_
(Specified) size of referenced characters
A pair of index and its value(T)
auto ltrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the beginning of the specified string.
auto substr(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
static auto maximum(const _Cont &container) -> IndexPair< T >
Calculate maximum value with its index.
auto rtrim(const std::vector< std::string > &delims) const -> WeakString
Removes all designated characters from the end of the specified string.
auto operator[](size_t index) const -> const char &
Get character of string Returns a const reference to the character at the specified position...
auto yoUpperCase() const -> std::string
Convert uppercase letters to lowercase.
WeakString(const char *begin, const char *end)
Constructor by characters of begin and end.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
auto size() const -> size_t
Returns size of the characters which are being referenced.
auto rfind(const WeakString &delim, size_t endIndex=SIZE_MAX) const -> size_t
Finds last occurence in string.
auto get_index() const -> size_t
Get index.
A string class only references characeters, reference only.