mirror of https://gitlab.com/rnger/amath
new AppendNumber method
This commit is contained in:
parent
f8cf191a46
commit
3a52c27a9c
|
@ -24,7 +24,7 @@
|
||||||
*
|
*
|
||||||
* Project homepage:
|
* Project homepage:
|
||||||
* https://amath.innolan.net
|
* https://amath.innolan.net
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "amath.h"
|
#include "amath.h"
|
||||||
|
@ -66,7 +66,7 @@ void CharBuffer::ClearBuffer()
|
||||||
{
|
{
|
||||||
if (buf != nullptr)
|
if (buf != nullptr)
|
||||||
{
|
{
|
||||||
delete [] buf;
|
delete[] buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = nullptr;
|
buf = nullptr;
|
||||||
|
@ -78,7 +78,7 @@ void CharBuffer::ClearBuffer()
|
||||||
* @brief Release memory, allocate and copy source.
|
* @brief Release memory, allocate and copy source.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void CharBuffer::ClearAndCopy(const char* source)
|
void CharBuffer::ClearAndCopy(const char *source)
|
||||||
{
|
{
|
||||||
ClearBuffer();
|
ClearBuffer();
|
||||||
cursize = AllocAndCopy(&buf, source);
|
cursize = AllocAndCopy(&buf, source);
|
||||||
|
@ -125,7 +125,7 @@ void CharBuffer::EnsureSize(unsigned int size)
|
||||||
}
|
}
|
||||||
else if (buf == ptr)
|
else if (buf == ptr)
|
||||||
{ // Already allocated but buffer is empty.
|
{ // Already allocated but buffer is empty.
|
||||||
delete [] buf;
|
delete[] buf;
|
||||||
buf = new char[cursize];
|
buf = new char[cursize];
|
||||||
ptr = buf;
|
ptr = buf;
|
||||||
}
|
}
|
||||||
|
@ -134,9 +134,9 @@ void CharBuffer::EnsureSize(unsigned int size)
|
||||||
// Make at least double size
|
// Make at least double size
|
||||||
cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize;
|
cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize;
|
||||||
unsigned int offset = (unsigned int)(ptr - buf);
|
unsigned int offset = (unsigned int)(ptr - buf);
|
||||||
char* temp = new char[cursize];
|
char *temp = new char[cursize];
|
||||||
MemCopy(temp, buf, tempsize);
|
MemCopy(temp, buf, tempsize);
|
||||||
delete [] buf;
|
delete[] buf;
|
||||||
buf = temp;
|
buf = temp;
|
||||||
ptr = buf + offset;
|
ptr = buf + offset;
|
||||||
}
|
}
|
||||||
|
@ -156,9 +156,9 @@ void CharBuffer::EnsureSize(unsigned int blocksize, unsigned int blocks)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
unsigned int tptr = (unsigned int)(ptr - buf);
|
unsigned int tptr = (unsigned int)(ptr - buf);
|
||||||
char* temp = new char[blocksize * blocks];
|
char *temp = new char[blocksize * blocks];
|
||||||
MemCopy(temp, buf, cursize);
|
MemCopy(temp, buf, cursize);
|
||||||
delete [] buf;
|
delete[] buf;
|
||||||
cursize = blocksize * blocks;
|
cursize = blocksize * blocks;
|
||||||
buf = temp;
|
buf = temp;
|
||||||
ptr = buf + tptr;
|
ptr = buf + tptr;
|
||||||
|
@ -173,7 +173,7 @@ void CharBuffer::EnsureGrowth(unsigned int size)
|
||||||
|
|
||||||
bool CharBuffer::IsEmpty() const
|
bool CharBuffer::IsEmpty() const
|
||||||
{
|
{
|
||||||
char* i = buf;
|
char *i = buf;
|
||||||
|
|
||||||
if (i == nullptr || buf == ptr)
|
if (i == nullptr || buf == ptr)
|
||||||
return true;
|
return true;
|
||||||
|
@ -185,20 +185,19 @@ bool CharBuffer::IsEmpty() const
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
} while (i != ptr);
|
||||||
while (i != ptr);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CharBuffer::Is(const char* string) const
|
bool CharBuffer::Is(const char *string) const
|
||||||
{
|
{
|
||||||
return StrIsEqual(GetString(), string);
|
return StrIsEqual(GetString(), string);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CharBuffer::Contains(const char c) const
|
bool CharBuffer::Contains(const char c) const
|
||||||
{
|
{
|
||||||
char* i = buf;
|
char *i = buf;
|
||||||
|
|
||||||
if (i == nullptr || buf == ptr)
|
if (i == nullptr || buf == ptr)
|
||||||
return false;
|
return false;
|
||||||
|
@ -209,8 +208,7 @@ bool CharBuffer::Contains(const char c) const
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
} while (i != ptr);
|
||||||
while (i != ptr);
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -230,14 +228,15 @@ void CharBuffer::DeleteLastChar()
|
||||||
ptr--;
|
ptr--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CharBuffer::Copy(CharBuffer* source)
|
void CharBuffer::Copy(CharBuffer *source)
|
||||||
{
|
{
|
||||||
EnsureSize(source->cursize);
|
EnsureSize(source->cursize);
|
||||||
const char* s = source->GetString();
|
const char *s = source->GetString();
|
||||||
|
|
||||||
ptr = buf;
|
ptr = buf;
|
||||||
// ReSharper disable once CppPossiblyErroneousEmptyStatements
|
// ReSharper disable once CppPossiblyErroneousEmptyStatements
|
||||||
while ((*ptr++ = *s++));
|
while ((*ptr++ = *s++))
|
||||||
|
;
|
||||||
|
|
||||||
ptr--;
|
ptr--;
|
||||||
}
|
}
|
||||||
|
@ -259,14 +258,49 @@ void CharBuffer::Append(const char c, unsigned int count)
|
||||||
*ptr++ = c;
|
*ptr++ = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CharBuffer::Append(const char* source)
|
void CharBuffer::Append(const char *source)
|
||||||
{
|
{
|
||||||
// ReSharper disable once CppPossiblyErroneousEmptyStatements
|
// ReSharper disable once CppPossiblyErroneousEmptyStatements
|
||||||
while ((*ptr++ = *source++));
|
while ((*ptr++ = *source++))
|
||||||
|
;
|
||||||
|
|
||||||
ptr--;
|
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)
|
bool CharBuffer::RemoveTrailing(const char c)
|
||||||
{
|
{
|
||||||
if (ptr == buf)
|
if (ptr == buf)
|
||||||
|
@ -283,10 +317,10 @@ bool CharBuffer::RemoveTrailing(const char c)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CharBuffer::RemoveTrailing(const char* string)
|
bool CharBuffer::RemoveTrailing(const char *string)
|
||||||
{
|
{
|
||||||
int len = StrLen(string) * sizeof(char);
|
int len = StrLen(string) * sizeof(char);
|
||||||
char* s = ptr - len;
|
char *s = ptr - len;
|
||||||
if (s < buf)
|
if (s < buf)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -303,8 +337,21 @@ bool CharBuffer::RemoveTrailing(const char* string)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* CharBuffer::GetString() const
|
char *CharBuffer::GetString() const
|
||||||
{
|
{
|
||||||
*ptr = '\0';
|
*ptr = '\0';
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CharBuffer::CopyTo(char *string)
|
||||||
|
{
|
||||||
|
char *q = buf;
|
||||||
|
char *p = string;
|
||||||
|
|
||||||
|
*ptr = '\0';
|
||||||
|
|
||||||
|
while ((*p++ = *q++))
|
||||||
|
;
|
||||||
|
|
||||||
|
*p = '\0';
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
*
|
*
|
||||||
* Project homepage:
|
* Project homepage:
|
||||||
* https://amath.innolan.net
|
* https://amath.innolan.net
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef AMATH_CHAR_BUFFER_H
|
#ifndef AMATH_CHAR_BUFFER_H
|
||||||
|
@ -69,11 +69,14 @@ public:
|
||||||
void Append(const char* source);
|
void Append(const char* source);
|
||||||
void Append(const char c);
|
void Append(const char c);
|
||||||
void Append(const char c, unsigned int count);
|
void Append(const char c, unsigned int count);
|
||||||
|
void AppendNumber(signed long value);
|
||||||
|
|
||||||
void DeleteLastChar();
|
void DeleteLastChar();
|
||||||
bool RemoveTrailing(const char c);
|
bool RemoveTrailing(const char c);
|
||||||
bool RemoveTrailing(const char* string);
|
bool RemoveTrailing(const char* string);
|
||||||
char* GetString() const;
|
char* GetString() const;
|
||||||
|
void CopyTo(char *string);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class AnsiConoleEngine;
|
friend class AnsiConoleEngine;
|
||||||
|
|
Loading…
Reference in New Issue