added trim operand function.
This commit is contained in:
parent
5fd02a7a44
commit
c40e288a67
21
ZString.cpp
21
ZString.cpp
@ -362,6 +362,12 @@ namespace coreutils {
|
||||
return cursor >= data + length;
|
||||
}
|
||||
|
||||
int ZString::goeod() {
|
||||
char *temp = cursor;
|
||||
cursor = data + length;
|
||||
return cursor - temp;
|
||||
}
|
||||
|
||||
bool ZString::startsWith(const char *value) {
|
||||
return strncmp(cursor, value, strlen(value)) == 0;
|
||||
}
|
||||
@ -455,6 +461,19 @@ namespace coreutils {
|
||||
return len;
|
||||
}
|
||||
|
||||
int ZString::trimTrailingWhitespace() {
|
||||
goeod();
|
||||
int len = 0;
|
||||
--cursor;
|
||||
while((cursor > data) && ((*cursor == ' ') || (*cursor == '\n') || (*cursor == '\t'))) {
|
||||
--cursor;
|
||||
--length;
|
||||
++len;
|
||||
}
|
||||
++cursor;
|
||||
return len;
|
||||
}
|
||||
|
||||
bool ZString::lineIsWhitespace() {
|
||||
char *end = data + length;
|
||||
char *temp = cursor;
|
||||
@ -496,6 +515,8 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
ZString ZString::trim() {
|
||||
trimTrailingWhitespace();
|
||||
reset();
|
||||
skipWhitespace();
|
||||
return unparsed();
|
||||
}
|
||||
|
16
ZString.h
16
ZString.h
@ -248,6 +248,14 @@ namespace coreutils {
|
||||
///
|
||||
|
||||
bool eod();
|
||||
|
||||
///
|
||||
/// Go to the end of data.
|
||||
///
|
||||
/// Return number of bytes the cursor was moved.
|
||||
///
|
||||
|
||||
int goeod();
|
||||
|
||||
///
|
||||
///
|
||||
@ -312,7 +320,7 @@ namespace coreutils {
|
||||
|
||||
///
|
||||
/// Advance the cursor through the ZString for each whitespace character
|
||||
/// encountered.
|
||||
/// encountered.
|
||||
///
|
||||
|
||||
int skipWhitespace();
|
||||
@ -321,6 +329,12 @@ namespace coreutils {
|
||||
///
|
||||
///
|
||||
|
||||
int trimTrailingWhitespace();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
bool lineIsWhitespace();
|
||||
|
||||
///
|
||||
|
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils
|
||||
|
Binary file not shown.
@ -122,5 +122,8 @@ int main(int argc, char **argv) {
|
||||
std::cout << "integer '53524534' is [" << test24 << "]" << std::endl;
|
||||
coreutils::ZString test25("-543");
|
||||
std::cout << "integer '-543' is [" << test25 << "]" << std::endl;
|
||||
|
||||
coreutils::ZString test26(" this is a trim test ");
|
||||
std::cout << "trimmed [" << test26.trim() << "]" << std::endl;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user