diff --git a/ZString.cpp b/ZString.cpp index 8bd5ae0..469ab2d 100644 --- a/ZString.cpp +++ b/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(); } diff --git a/ZString.h b/ZString.h index c7a4836..8f953c7 100644 --- a/ZString.h +++ b/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(); /// diff --git a/testing/compile b/testing/compile index 03aa841..f380c6d 100755 --- a/testing/compile +++ b/testing/compile @@ -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 diff --git a/testing/jstring_test b/testing/jstring_test index 8142a0d..666ec6e 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index 2684146..744a2ec 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -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; }