From bf4ff3fdac92ba60b21171f69ade47b2e58e9b0c Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Mon, 4 Nov 2024 16:26:40 -0800 Subject: [PATCH] Created destructive removeTrailingZeros() method on ZString. --- ZString.cpp | 11 ++++++++++- ZString.h | 6 ++++++ testing/zstring_test.cpp | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ZString.cpp b/ZString.cpp index 25d6d9c..0b83530 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -544,7 +544,16 @@ namespace coreutils { return true; } - + ZString ZString::removeTrailingZeros() { + if(length > 2) { + while((*(data + length - 1)) == '0') + --length; + if(*(data + length - 1) == '.') + --length; + } + return *this; + } + void ZString::nextChar() { if(!eod()) ++cursor; diff --git a/ZString.h b/ZString.h index 9df6061..a8970c2 100644 --- a/ZString.h +++ b/ZString.h @@ -423,6 +423,12 @@ namespace coreutils { /// bool boolValue(); + + /// + /// + /// + + ZString removeTrailingZeros(); protected: char *data; diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index cc51a68..b568634 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -107,5 +107,9 @@ int main(int argc, char **argv) { coreutils::ZString test20(NULL); std::cout << "[" << test20 << "]" << std::endl; std::cout << "Comparisons with null value: " << (test20 == "true") << std::endl; + + coreutils::ZString test21("10.00000000"); + std::cout << "Trailing Zeros: " << test21.removeTrailingZeros() << std::endl; + std::cout << " " << test21 << std::endl; }