Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2022-05-21 13:45:41 +0300
committerDavid Crocker <dcrocker@eschertech.com>2022-05-21 13:45:41 +0300
commit780a8c210a69de6ab722171b4ab23da58dbdaa2e (patch)
treee346a985c95c7aacf2116426dbcc61eb33eaf711
parent71004ac8c9e457afdd209c22813438be0c46b044 (diff)
Code corrections and tidy up of OutputBuffer class
-rw-r--r--src/Platform/OutputMemory.cpp62
-rw-r--r--src/Platform/OutputMemory.h1
2 files changed, 29 insertions, 34 deletions
diff --git a/src/Platform/OutputMemory.cpp b/src/Platform/OutputMemory.cpp
index b2ac7433..65ab215c 100644
--- a/src/Platform/OutputMemory.cpp
+++ b/src/Platform/OutputMemory.cpp
@@ -22,26 +22,29 @@ void OutputBuffer::Append(OutputBuffer *other) noexcept
if (other != nullptr)
{
last->next = other;
- last = other->last;
+ OutputBuffer * const newLast = other->last;
if (other->hadOverflow)
{
hadOverflow = true;
}
- for (OutputBuffer *item = Next(); item != other; item = item->Next())
+ OutputBuffer *item = this;
+ do
{
- item->last = last;
+ item->last = newLast;
+ item = item->Next();
}
+ while (item != other);
}
}
void OutputBuffer::IncreaseReferences(size_t refs) noexcept
{
- if (refs > 0)
+ if (refs != 0)
{
TaskCriticalSectionLocker lock;
- for(OutputBuffer *item = this; item != nullptr; item = item->Next())
+ for (OutputBuffer *item = this; item != nullptr; item = item->Next())
{
item->references += refs;
item->isReferenced = true;
@@ -59,20 +62,6 @@ size_t OutputBuffer::Length() const noexcept
return totalLength;
}
-char &OutputBuffer::operator[](size_t index) noexcept
-{
- // Get the right buffer to access
- OutputBuffer *itemToIndex = this;
- while (index >= itemToIndex->DataLength())
- {
- index -= itemToIndex->DataLength();
- itemToIndex = itemToIndex->Next();
- }
-
- // Return the char reference
- return itemToIndex->data[index];
-}
-
char OutputBuffer::operator[](size_t index) const noexcept
{
// Get the right buffer to access
@@ -89,7 +78,7 @@ char OutputBuffer::operator[](size_t index) const noexcept
const char *OutputBuffer::Read(size_t len) noexcept
{
- size_t offset = bytesRead;
+ const size_t offset = bytesRead;
bytesRead += len;
return data + offset;
}
@@ -120,7 +109,7 @@ size_t OutputBuffer::printf(const char *_ecv_array fmt, ...) noexcept
{
va_list vargs;
va_start(vargs, fmt);
- size_t ret = vprintf(fmt, vargs);
+ const size_t ret = vprintf(fmt, vargs);
va_end(vargs);
return ret;
}
@@ -138,7 +127,7 @@ size_t OutputBuffer::catf(const char *_ecv_array fmt, ...) noexcept
{
va_list vargs;
va_start(vargs, fmt);
- size_t ret = vcatf(fmt, vargs);
+ const size_t ret = vcatf(fmt, vargs);
va_end(vargs);
return ret;
}
@@ -146,9 +135,13 @@ size_t OutputBuffer::catf(const char *_ecv_array fmt, ...) noexcept
size_t OutputBuffer::lcatf(const char *_ecv_array fmt, ...) noexcept
{
size_t extra = 0;
- if (Length() != 0 && operator[](Length() - 1) != '\n')
+ if (last->dataLength != 0 && last->data[last->dataLength - 1] != '\n')
{
extra = cat('\n');
+ if (extra == 0)
+ {
+ return 0;
+ }
}
va_list vargs;
@@ -235,8 +228,7 @@ size_t OutputBuffer::cat(const char *_ecv_array src, size_t len) noexcept
}
nextBuffer->references = references;
last->next = nextBuffer;
- last = nextBuffer->last;
- for (OutputBuffer *item = Next(); item != nextBuffer; item = item->Next())
+ for (OutputBuffer *item = this; item != nextBuffer; item = item->Next())
{
item->last = last;
}
@@ -251,11 +243,17 @@ size_t OutputBuffer::cat(const char *_ecv_array src, size_t len) noexcept
size_t OutputBuffer::lcat(const char *_ecv_array src, size_t len) noexcept
{
- if (Length() != 0)
+ size_t extra = 0;
+ if (last->dataLength != 0 && last->data[last->dataLength - 1] != '\n')
{
- cat('\n');
+ extra = cat('\n');
+ if (extra == 0)
+ {
+ return 0;
+ }
}
- return cat(src, len);
+
+ return cat(src, len) + extra;
}
size_t OutputBuffer::cat(StringRef &str) noexcept
@@ -294,13 +292,11 @@ size_t OutputBuffer::EncodeChar(char c) noexcept
if (esc != 0)
{
- cat('\\');
- cat(esc);
- return 2;
+ const size_t written = cat('\\');
+ return (written == 0) ? written : written + cat(esc);
}
- cat(c);
- return 1;
+ return cat(c);
}
size_t OutputBuffer::EncodeReply(OutputBuffer *src) noexcept
diff --git a/src/Platform/OutputMemory.h b/src/Platform/OutputMemory.h
index c8a3f59b..0ef8cd53 100644
--- a/src/Platform/OutputMemory.h
+++ b/src/Platform/OutputMemory.h
@@ -35,7 +35,6 @@ public:
size_t DataLength() const noexcept { return dataLength; } // How many bytes have been written to this instance?
size_t Length() const noexcept; // How many bytes have been written to the whole chain?
- char& operator[](size_t index) noexcept;
char operator[](size_t index) const noexcept;
const char *_ecv_array Read(size_t len) noexcept;
void Taken(size_t len) noexcept { bytesRead += len; }