From 0a61197cb6b062db198c1a119fee220f3863dd18 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 5 Jul 2022 19:32:58 -0500 Subject: [PATCH] Added a way to delete a range of characters at any index and count. --- main.c | 15 +++++++++++++++ tstring.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tstring.h | 1 + 3 files changed, 65 insertions(+) diff --git a/main.c b/main.c index edb90d4..dda35e3 100644 --- a/main.c +++ b/main.c @@ -58,6 +58,21 @@ int main(int argc, char** argv) { } if (input[0] == 'r') { + 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); + + TStringRemoveRange(num, count, string); + + continue; + } TStringPop(glyph, string); printf("Popped '%s'\n", glyph); continue; diff --git a/tstring.c b/tstring.c index 0fd8f37..20ccfd5 100644 --- a/tstring.c +++ b/tstring.c @@ -5,6 +5,7 @@ #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); @@ -91,6 +92,50 @@ void TStringPop(char buffer[8], TString* string) { } } +void TStringRemoveRange(int startIndex, int count, TString* string) { + if (!string) return; + if (startIndex < 0 || startIndex > string->Length) return; + if (count <= 0 || count > string->Length - startIndex) return; + + char buffer[256] = { 0 }; + 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; glyphCount++) endByte += GetGlyphByteWidth(string->Characters[endByte]); + + memset(&string->Characters[startByte], '\0', endByte - startByte); + + if (endByte == string->ByteCount) { + string->Length -= glyphCount; + string->ByteCount -= endByte - startByte; + return; + } + + unsigned int fullBuffers = (string->ByteCount - endByte) / sizeof buffer; + unsigned int remainder = (string->ByteCount - endByte) - fullBuffers * sizeof buffer; + + for (int i = fullBuffers; i > 0; i--) { + memcpy(buffer, &string->Characters[string->ByteCount - i * sizeof buffer], sizeof buffer); + memcpy(&string->Characters[endByte - sizeof buffer * i], buffer, sizeof buffer); + } + + if (remainder > 0) { + memcpy(buffer, &string->Characters[endByte], remainder); + memcpy(&string->Characters[startByte], buffer, remainder); + } + + string->Length -= glyphCount; + string->ByteCount -= endByte - startByte; +} + void TStringInsertText(const char* text, int index, TString* string) { if (!text || !string) return; if (index < 0 || index > string->Length) index = string->Length; @@ -124,6 +169,10 @@ 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; diff --git a/tstring.h b/tstring.h index 7c549f9..6e8e33d 100644 --- a/tstring.h +++ b/tstring.h @@ -14,6 +14,7 @@ void TStringFree(TString* string); 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); char* TStringGetString(const TString* string); int TStringLength(const TString* string);