Avoid using substr in splitString()/splitStringSimple()
This commit is contained in:
parent
27e9761f63
commit
6968716d5c
|
@ -140,12 +140,13 @@ Container splitString(const typename Container::value_type &string, const typena
|
||||||
{
|
{
|
||||||
--maxParts;
|
--maxParts;
|
||||||
Container res;
|
Container res;
|
||||||
|
typename Container::value_type *last = nullptr;
|
||||||
bool merge = false;
|
bool merge = false;
|
||||||
for (typename Container::value_type::size_type i = 0, end = string.size(), delimPos; i < end; i = delimPos + delimiter.size()) {
|
for (typename Container::value_type::size_type i = 0, end = string.size(), delimPos; i < end; i = delimPos + delimiter.size()) {
|
||||||
delimPos = string.find(delimiter, i);
|
delimPos = string.find(delimiter, i);
|
||||||
if (!merge && maxParts >= 0 && res.size() == static_cast<typename Container::value_type::size_type>(maxParts)) {
|
if (!merge && maxParts >= 0 && res.size() == static_cast<typename Container::value_type::size_type>(maxParts)) {
|
||||||
if (delimPos == i && emptyPartsRole == EmptyPartsTreat::Merge) {
|
if (delimPos == i && emptyPartsRole == EmptyPartsTreat::Merge) {
|
||||||
if (!res.empty()) {
|
if (last) {
|
||||||
merge = true;
|
merge = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -157,14 +158,14 @@ Container splitString(const typename Container::value_type &string, const typena
|
||||||
}
|
}
|
||||||
if (emptyPartsRole == EmptyPartsTreat::Keep || i != delimPos) {
|
if (emptyPartsRole == EmptyPartsTreat::Keep || i != delimPos) {
|
||||||
if (merge) {
|
if (merge) {
|
||||||
res.back().append(delimiter);
|
last->append(delimiter);
|
||||||
res.back().append(string.substr(i, delimPos - i));
|
last->append(string, i, delimPos - i);
|
||||||
merge = false;
|
merge = false;
|
||||||
} else {
|
} else {
|
||||||
res.emplace_back(string.substr(i, delimPos - i));
|
last = &res.emplace_back(string, i, delimPos - i);
|
||||||
}
|
}
|
||||||
} else if (emptyPartsRole == EmptyPartsTreat::Merge) {
|
} else if (emptyPartsRole == EmptyPartsTreat::Merge) {
|
||||||
if (!res.empty()) {
|
if (last) {
|
||||||
merge = true;
|
merge = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,10 +198,10 @@ Container splitStringSimple(const typename Container::value_type &string, const
|
||||||
#if __cplusplus >= 201709
|
#if __cplusplus >= 201709
|
||||||
if constexpr (requires { res.emplace_back(string); }) {
|
if constexpr (requires { res.emplace_back(string); }) {
|
||||||
#endif
|
#endif
|
||||||
res.emplace_back(string.substr(i, delimPos - i));
|
res.emplace_back(string.data() + i, delimPos - i);
|
||||||
#if __cplusplus >= 201709
|
#if __cplusplus >= 201709
|
||||||
} else {
|
} else {
|
||||||
res.emplace(string.substr(i, delimPos - i));
|
res.emplace(string.data() + i, delimPos - i);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue