From b1d79d7b7e17f19bdc720c58d6ad44edf5d11be2 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 5 Jul 2022 23:33:44 -0500 Subject: [PATCH] Added a function to get a range of characters from the TString object. --- main.c | 22 ++++++++++++++++++++++ tstring.c | 36 +++++++++++++++++++++++++++++++----- tstring.h | 1 + 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index dda35e3..0d51c43 100644 --- a/main.c +++ b/main.c @@ -78,6 +78,28 @@ int main(int argc, char** argv) { continue; } + if (input[0] == 'g') { + if (input[1] != '\0') { + printf("Select index: "); + fgets(input, sizeof input, stdin); + + int num = strtol(input, NULL, 10); + printf("Enter count: "); + + fgets(input, sizeof input, stdin); + + int count = strtol(input, NULL, 10); + + char* text = TStringGetStringRange(num, count, string); + + printf("Returned String:\n'%s'\n", text); + + free(text); + + continue; + } + } + TStringAppendText(input, string); } //fgets(glyph, 10, stdin); diff --git a/tstring.c b/tstring.c index 16d278a..09ded1a 100644 --- a/tstring.c +++ b/tstring.c @@ -5,7 +5,6 @@ #include void TStringCopyGlyph(const char* glyph, int length, TString* string); -void TStringCompactString(unsigned int startByte, unsigned int endByte, TString* string); int ResizeTStringCapacity(int newTextLength, TString* string); int GetGlyphByteWidth(char glyph); @@ -171,10 +170,6 @@ void TStringInsertText(const char* text, int index, TString* string) { string->ByteCount += insertionTextLength; } -void TStringCompactString(unsigned int startByte, unsigned int endByte, TString* string) { - -} - void TStringAppendText(const char* text, TString* string) { if (!text || !string) return; @@ -214,6 +209,37 @@ 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; + + 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) { + 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); + + return text; +} + void TStringGetGlyph(int index, char buffer[8], const TString* string) { if (!buffer || !string) return; diff --git a/tstring.h b/tstring.h index 6e8e33d..665798e 100644 --- a/tstring.h +++ b/tstring.h @@ -16,6 +16,7 @@ void TStringInsertText(const char* text, int index, TString* string); void TStringPop(char buffer[8], TString* string); void TStringRemoveRange(int startIndex, int count, TString* string); char* TStringGetString(const TString* string); +char* TStringGetStringRange(int startIndex, int count, const TString* string); int TStringLength(const TString* string);