From 7c20eef8d11e33dcd10f3a927114c7711a15b7de Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sat, 2 Jul 2022 21:02:11 -0500 Subject: [PATCH] Finished the string insertion function. --- main.c | 11 ++++++----- tstring.c | 52 +++++++++++++++++++++++++++------------------------- tstring.h | 2 +- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/main.c b/main.c index aa5c437..17b8c69 100644 --- a/main.c +++ b/main.c @@ -19,19 +19,20 @@ int main(int argc, char** argv) { char* test2 = u8"\xE3\x81\x84"; char* test4 = u8"\xF0\xA0\xBA\x8C"; char* test5 = "B"; - char* test3 = " Just some extra texts"; + //char* test3 = " Just some extra texts"; TStringAppendText(test, string); TStringAppendText(test2, string); TStringAppendText(test4, string); TStringAppendText(test5, string); - TStringAppendText(test3, string); + //TStringAppendText(test3, string); TStringGetGlyph(index, characters, string); TStringGetGlyph(3, glyph, string); - - printf("From string index %d: '%s' (string length is %d)\n", index, characters, TStringLength(string)); - printf("And one English character: '%s'\n", glyph); + TStringPrintDetails(string); + TStringInsertText("**INS**", 2, string); + //printf("From string index %d: '%s' (string length is %d)\n", index, characters, TStringLength(string)); + //printf("And one English character: '%s'\n", glyph); //printf("Full string: '%s'\n", TStringGetString(string)); TStringPrintDetails(string); diff --git a/tstring.c b/tstring.c index b587524..967657f 100644 --- a/tstring.c +++ b/tstring.c @@ -73,35 +73,33 @@ void TStringRemove(int index, TString* string) { void TStringInsertText(const char* text, int index, TString* string) { if (!text || !string) return; - int textLength = strlen(text); - int glyphCount = 0; - int startByte = 0; + unsigned int insertionTextLength = strlen(text); + unsigned int startByte = 0; + unsigned int endByte = string->ByteCount + insertionTextLength; - if (!ResizeTStringCapacity(textLength, string)) return; - - while(startByte < string->ByteCount) { - if (glyphCount == index) break; - - startByte += GetGlyphByteWidth(&string->Characters[startByte]); - - glyphCount++; - } -} - -void TStringMemSwap(const char* text, int startIndex, int length, TString* string) { - if (!text || !string) return; + if (!ResizeTStringCapacity(insertionTextLength, string)) return; char buffer[256] = { 0 }; - char intermediate[sizeof buffer] = { 0 }; - const char* inputTextStart = text; - const char* characterListStart = &string->Characters[startIndex]; - int blockCount = length / sizeof buffer; - int remainder = length % sizeof buffer; + for (int glyphCount = 0; glyphCount != index; glyphCount++) startByte += GetGlyphByteWidth(&string->Characters[startByte]); + for (unsigned int offset = 0; offset < insertionTextLength; string->Length++) offset += GetGlyphByteWidth(&text[offset]); - for(; blockCount > 0; blockCount--) { - + unsigned int fullBuffers = (string->ByteCount - startByte) / sizeof buffer; + unsigned int remainder = (string->ByteCount - startByte) - 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[startByte], remainder); + memcpy(&string->Characters[insertionTextLength + startByte], buffer, remainder); + } + + memcpy(&string->Characters[startByte], text, insertionTextLength); + + string->ByteCount += insertionTextLength; } void TStringAppendText(const char* text, TString* string) { @@ -172,7 +170,7 @@ void TStringFree(TString* string) { } int GetGlyphByteWidth(const char* glyph) { - if (!glyph) return -1; + if (!glyph) return 0; if ((*glyph & 0x80) == 0) { return 1; @@ -187,7 +185,7 @@ int GetGlyphByteWidth(const char* glyph) { return 4; } - return -1; + return 0; } void TStringCopyGlyph(const char* glyph, int length, TString* string) { @@ -212,4 +210,8 @@ void TStringPrintDetails(const TString* string) { printf("%s\n", text); free(text); } + + printf("Bytes:"); + for(int i = 0; i < string->Capacity; i++) printf(" %02X", string->Characters[i] & 0xFF); + printf("\n"); } \ No newline at end of file diff --git a/tstring.h b/tstring.h index f6fcb0b..866ecad 100644 --- a/tstring.h +++ b/tstring.h @@ -12,7 +12,7 @@ void TStringAppendText(const char* text, TString* string); void TStringGetGlyph(int index, char buffer[8], const TString* string); void TStringFree(TString* string); void TStringPrintDetails(const TString* string); -//void TStringInsertText(const char* text, int index, TString* string); +void TStringInsertText(const char* text, int index, TString* string); //void TStringRemove(int index, TString* string); char* TStringGetString(const TString* string); int TStringLength(const TString* string);