1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#pragma once
namespace nall {
auto string::trim(rstring lhs, rstring rhs, long limit) -> string& {
rtrim(rhs, limit);
ltrim(lhs, limit);
return *this;
}
auto string::ltrim(rstring lhs, long limit) -> string& {
if(lhs.size() == 0) return *this;
long matches = 0;
while(matches < limit) {
signed offset = lhs.size() * matches;
signed length = (signed)size() - offset;
if(length < (signed)lhs.size()) break;
if(memory::compare(data() + offset, lhs.data(), lhs.size()) != 0) break;
matches++;
}
if(matches) remove(0, lhs.size() * matches);
return *this;
}
auto string::rtrim(rstring rhs, long limit) -> string& {
if(rhs.size() == 0) return *this;
long matches = 0;
while(matches < limit) {
signed offset = (signed)size() - rhs.size() * (matches + 1);
signed length = (signed)size() - offset;
if(offset < 0 || length < (signed)rhs.size()) break;
if(memory::compare(data() + offset, rhs.data(), rhs.size()) != 0) break;
matches++;
}
if(matches) resize(size() - rhs.size() * matches);
return *this;
}
auto string::itrim(rstring lhs, rstring rhs, long limit) -> string& {
irtrim(rhs, limit);
iltrim(lhs, limit);
return *this;
}
auto string::iltrim(rstring lhs, long limit) -> string& {
if(lhs.size() == 0) return *this;
long matches = 0;
while(matches < limit) {
signed offset = lhs.size() * matches;
signed length = (signed)size() - offset;
if(length < (signed)lhs.size()) break;
if(memory::icompare(data() + offset, lhs.data(), lhs.size()) != 0) break;
matches++;
}
if(matches) remove(0, lhs.size() * matches);
return *this;
}
auto string::irtrim(rstring rhs, long limit) -> string& {
if(rhs.size() == 0) return *this;
long matches = 0;
while(matches < limit) {
signed offset = (signed)size() - rhs.size() * (matches + 1);
signed length = (signed)size() - offset;
if(offset < 0 || length < (signed)rhs.size()) break;
if(memory::icompare(data() + offset, rhs.data(), rhs.size()) != 0) break;
matches++;
}
if(matches) resize(size() - rhs.size() * matches);
return *this;
}
auto string::strip() -> string& {
rstrip();
lstrip();
return *this;
}
auto string::lstrip() -> string& {
unsigned length = 0;
while(length < size()) {
char input = operator[](length);
if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
length++;
}
if(length) remove(0, length);
return *this;
}
auto string::rstrip() -> string& {
unsigned length = 0;
while(length < size()) {
bool matched = false;
char input = operator[](size() - length - 1);
if(input != ' ' && input != '\t' && input != '\r' && input != '\n') break;
length++;
}
if(length) resize(size() - length);
return *this;
}
}
|