new AppendNumber method

This commit is contained in:
Carsten Sonne Larsen 2021-01-11 20:32:07 +01:00
parent f8cf191a46
commit 3a52c27a9c
2 changed files with 74 additions and 24 deletions

View File

@ -185,8 +185,7 @@ bool CharBuffer::IsEmpty() const
return false;
i++;
}
while (i != ptr);
} while (i != ptr);
return true;
}
@ -209,8 +208,7 @@ bool CharBuffer::Contains(const char c) const
return true;
i++;
}
while (i != ptr);
} while (i != ptr);
return false;
}
@ -237,7 +235,8 @@ void CharBuffer::Copy(CharBuffer* source)
ptr = buf;
// ReSharper disable once CppPossiblyErroneousEmptyStatements
while ((*ptr++ = *s++));
while ((*ptr++ = *s++))
;
ptr--;
}
@ -262,11 +261,46 @@ void CharBuffer::Append(const char c, unsigned int count)
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)
@ -308,3 +342,16 @@ 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';
}

View File

@ -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;