From c201da97363a9a022502931a6bfc7c4c2667e577 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 10 Jul 2022 15:17:18 -0500 Subject: [PATCH] Updated the interface for the TStringGetStringRange function. It will now fill a buffer, leaving a null byte at the end, with as many characters as it can depending on the supplied buffer's size. --- src/main.c | 16 +++++++--------- src/tstring.c | 36 +++++++++++++++--------------------- src/tstring.h | 2 +- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/main.c b/src/main.c index 0d51c43..53dd14c 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ int main(int argc, char** argv) { char input[1024] = { 0 }; char glyph[8] = { 0 }; + char buffer[11] = { 0 }; char* file; size_t bytesRead; @@ -90,11 +91,12 @@ int main(int argc, char** argv) { int count = strtol(input, NULL, 10); - char* text = TStringGetStringRange(num, count, string); + //char* text = TStringGetStringRange(num, count, string); + TStringGetStringRange(string, num, count, sizeof buffer, buffer); - printf("Returned String:\n'%s'\n", text); - - free(text); + printf("Returned String:\n'%s'\n", buffer); + for (int i = 0; i < sizeof buffer; i++) printf("%02X ", buffer[i] & 0xFF); + printf("\n"); continue; } @@ -102,10 +104,6 @@ int main(int argc, char** argv) { TStringAppendText(input, string); } - //fgets(glyph, 10, stdin); - - //glyph[strlen(glyph) - 1] = 0; - - //printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph)); + TStringFree(string); } \ No newline at end of file diff --git a/src/tstring.c b/src/tstring.c index 8f61502..cd8a482 100644 --- a/src/tstring.c +++ b/src/tstring.c @@ -202,35 +202,29 @@ char* TStringGetString(const TString* string) { return text; } -char* TStringGetStringRange(int startIndex, int count, const TString* string) { - if (!string) return NULL; - if (startIndex < 0 || startIndex >= string->Length) return NULL; - if (count > string->Length) return NULL; +void TStringGetStringRange(const TString* string, unsigned int startIndex, unsigned int count, unsigned int bufferSize, char buffer[]) { + if (!string || !buffer) return; + if (count > string->Length) return; - char* text = calloc(string->ByteCount + 1, sizeof(char)); - - if (!text) { - fprintf(stderr, "Failed to calloc memory for TString text. %s.\n", strerror(errno)); - - return NULL; - } - - unsigned int startByte = 0; - unsigned int glyphCount = 0; - - while(glyphCount != startIndex) { + unsigned int glyphCount = 0, startByte = 0; + + while (glyphCount != startIndex) { startByte += GetGlyphByteWidth(string->Characters[startByte]); - + glyphCount++; } unsigned int endByte = startByte; - for (glyphCount = 0; glyphCount < count && endByte <= string->ByteCount; glyphCount++) endByte += GetGlyphByteWidth(string->Characters[endByte]); - - memcpy(text, &string->Characters[startByte], endByte - startByte); + for(glyphCount = 0; glyphCount < count && endByte <= string->ByteCount && endByte - startByte < bufferSize; glyphCount++) { + int width = GetGlyphByteWidth(string->Characters[endByte]); - return text; + if (width + endByte - startByte < bufferSize) endByte += width; + else break; + } + + memset(buffer, 0, bufferSize); + memcpy(buffer, &string->Characters[startByte], endByte - startByte); } void TStringGetGlyph(int index, char buffer[8], const TString* string) { diff --git a/src/tstring.h b/src/tstring.h index 665798e..25bcbcb 100644 --- a/src/tstring.h +++ b/src/tstring.h @@ -15,8 +15,8 @@ void TStringPrintDetails(const TString* string, int statsOnly); void TStringInsertText(const char* text, int index, TString* string); void TStringPop(char buffer[8], TString* string); void TStringRemoveRange(int startIndex, int count, TString* string); +void TStringGetStringRange(const TString* string, unsigned int startIndex, unsigned int count, unsigned int bufferSize, char buffer[]); char* TStringGetString(const TString* string); -char* TStringGetStringRange(int startIndex, int count, const TString* string); int TStringLength(const TString* string);