From 3a52c27a9cdf8f0aa457bbaef850789964b8fd50 Mon Sep 17 00:00:00 2001 From: Carsten Larsen Date: Mon, 11 Jan 2021 20:32:07 +0100 Subject: [PATCH] new AppendNumber method --- src/lib/charbuf.cpp | 93 ++++++++++++++++++++++++++++++++++----------- src/lib/charbuf.h | 5 ++- 2 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/lib/charbuf.cpp b/src/lib/charbuf.cpp index 4af9b48a..6ac8a3f2 100644 --- a/src/lib/charbuf.cpp +++ b/src/lib/charbuf.cpp @@ -24,7 +24,7 @@ * * Project homepage: * https://amath.innolan.net - * + * */ #include "amath.h" @@ -66,7 +66,7 @@ void CharBuffer::ClearBuffer() { if (buf != nullptr) { - delete [] buf; + delete[] buf; } buf = nullptr; @@ -78,7 +78,7 @@ void CharBuffer::ClearBuffer() * @brief Release memory, allocate and copy source. * */ -void CharBuffer::ClearAndCopy(const char* source) +void CharBuffer::ClearAndCopy(const char *source) { ClearBuffer(); cursize = AllocAndCopy(&buf, source); @@ -125,7 +125,7 @@ void CharBuffer::EnsureSize(unsigned int size) } else if (buf == ptr) { // Already allocated but buffer is empty. - delete [] buf; + delete[] buf; buf = new char[cursize]; ptr = buf; } @@ -134,9 +134,9 @@ void CharBuffer::EnsureSize(unsigned int size) // Make at least double size cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize; unsigned int offset = (unsigned int)(ptr - buf); - char* temp = new char[cursize]; + char *temp = new char[cursize]; MemCopy(temp, buf, tempsize); - delete [] buf; + delete[] buf; buf = temp; ptr = buf + offset; } @@ -156,9 +156,9 @@ void CharBuffer::EnsureSize(unsigned int blocksize, unsigned int blocks) else { unsigned int tptr = (unsigned int)(ptr - buf); - char* temp = new char[blocksize * blocks]; + char *temp = new char[blocksize * blocks]; MemCopy(temp, buf, cursize); - delete [] buf; + delete[] buf; cursize = blocksize * blocks; buf = temp; ptr = buf + tptr; @@ -173,7 +173,7 @@ void CharBuffer::EnsureGrowth(unsigned int size) bool CharBuffer::IsEmpty() const { - char* i = buf; + char *i = buf; if (i == nullptr || buf == ptr) return true; @@ -185,20 +185,19 @@ bool CharBuffer::IsEmpty() const return false; i++; - } - while (i != ptr); + } while (i != ptr); return true; } -bool CharBuffer::Is(const char* string) const +bool CharBuffer::Is(const char *string) const { return StrIsEqual(GetString(), string); } bool CharBuffer::Contains(const char c) const { - char* i = buf; + char *i = buf; if (i == nullptr || buf == ptr) return false; @@ -209,8 +208,7 @@ bool CharBuffer::Contains(const char c) const return true; i++; - } - while (i != ptr); + } while (i != ptr); return false; } @@ -230,14 +228,15 @@ void CharBuffer::DeleteLastChar() ptr--; } -void CharBuffer::Copy(CharBuffer* source) +void CharBuffer::Copy(CharBuffer *source) { EnsureSize(source->cursize); - const char* s = source->GetString(); + const char *s = source->GetString(); ptr = buf; // ReSharper disable once CppPossiblyErroneousEmptyStatements - while ((*ptr++ = *s++)); + while ((*ptr++ = *s++)) + ; ptr--; } @@ -259,14 +258,49 @@ void CharBuffer::Append(const char c, unsigned int count) *ptr++ = c; } -void CharBuffer::Append(const char* source) +void CharBuffer::Append(const char *source) { // ReSharper disable once CppPossiblyErroneousEmptyStatements - while ((*ptr++ = *source++)); + while ((*ptr++ = *source++)) + ; ptr--; } +void CharBuffer::AppendNumber(signed long value) +{ + static const char *alphaNumerics = "0123456789"; + unsigned int count = 0; + unsigned long current = value; + char chars[12]; + char *p = chars; + bool negative = false; + + if (value < 0) + { + current = -value; + negative = true; + } + + do + { + unsigned long remainder = current % 10; + *p++ = alphaNumerics[remainder]; + current /= 10; + count++; + } while (current >= 1); + + p--; + + if (negative) + { + *ptr++ = '-'; + } + + while (count-- != 0) + *ptr++ = *p--; +} + bool CharBuffer::RemoveTrailing(const char c) { if (ptr == buf) @@ -283,10 +317,10 @@ bool CharBuffer::RemoveTrailing(const char c) return false; } -bool CharBuffer::RemoveTrailing(const char* string) +bool CharBuffer::RemoveTrailing(const char *string) { int len = StrLen(string) * sizeof(char); - char* s = ptr - len; + char *s = ptr - len; if (s < buf) { return false; @@ -303,8 +337,21 @@ bool CharBuffer::RemoveTrailing(const char* string) return false; } -char* CharBuffer::GetString() const +char *CharBuffer::GetString() const { *ptr = '\0'; return buf; } + +void CharBuffer::CopyTo(char *string) +{ + char *q = buf; + char *p = string; + + *ptr = '\0'; + + while ((*p++ = *q++)) + ; + + *p = '\0'; +} diff --git a/src/lib/charbuf.h b/src/lib/charbuf.h index 638687b2..7dd52a70 100644 --- a/src/lib/charbuf.h +++ b/src/lib/charbuf.h @@ -24,7 +24,7 @@ * * Project homepage: * https://amath.innolan.net - * + * */ #ifndef AMATH_CHAR_BUFFER_H @@ -69,11 +69,14 @@ public: void Append(const char* source); void Append(const char c); void Append(const char c, unsigned int count); + void AppendNumber(signed long value); void DeleteLastChar(); bool RemoveTrailing(const char c); bool RemoveTrailing(const char* string); char* GetString() const; + void CopyTo(char *string); + private: friend class AnsiConoleEngine;