From 3564c43ff416e08c9d6cf3d46b68b45fa9e43877 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 30 Jun 2022 22:09:30 -0500 Subject: [PATCH] Added code to handle appending multiple characters. --- main.c | 15 ++++++++------- tstring.c | 32 ++++++++++++++++++++++++++++++++ tstring.h | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index a326f56..32e7945 100644 --- a/main.c +++ b/main.c @@ -23,8 +23,8 @@ // const unsigned char Four = 0xF7; int main(int argc, char** argv) { - char glyph[10] = { 0 }; - char characters[8] = { 0 }; + char glyph[8] = { 0 }; + char characters[8] = { 42 }; int index = 2; TString* string = TStringCreate(); @@ -33,18 +33,19 @@ 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\0 extra text"; + char* test3 = " Just some extra text"; TStringAppendText(test, string); TStringAppendText(test2, string); TStringAppendText(test4, string); TStringAppendText(test5, string); + TStringAppendText(test3, string); - //GetCharByIndex(index, characters, string); - //GetCharByIndex(3, glyph, string); + TStringGetGlyph(index, characters, string); + TStringGetGlyph(3, glyph, string); - // printf("From string index %d: '%s' (string length is %d)\n", index, characters, string->Length); - // printf("And one English character: '%s'\n", glyph); + 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)); //fgets(glyph, 10, stdin); diff --git a/tstring.c b/tstring.c index 01d6c42..7dc2033 100644 --- a/tstring.c +++ b/tstring.c @@ -25,6 +25,12 @@ TString* TStringCreate(void) { return string; } +int TStringLength(const TString* string) { + if (!string) return - 1; + + return string->Length; +} + void TStringAppendText(const char* text, TString* string) { int totalLength = strlen(text); int glyphLength = GetGlyphByteWidth(text); @@ -36,6 +42,14 @@ void TStringAppendText(const char* text, TString* string) { return; } + + //If we've made it here, than that means we have a full on string of characters. + for(int i = 0; i < totalLength;) { + TStringCopyGlyph(&text[i], glyphLength, string); + + i += glyphLength; + glyphLength = GetGlyphByteWidth(&text[i]); + } } char* TStringGetString(const TString* string) { @@ -55,7 +69,23 @@ char* TStringGetString(const TString* string) { } void TStringGetGlyph(int index, char buffer[8], const TString* string) { + if (!buffer || !string) return; + memset(buffer, '\0', sizeof *buffer); + + int glyphCount = 0; + + for(int i = 0; i < string->ByteCount;) { + int glyphWidth = GetGlyphByteWidth(&string->Characters[i]); + + if (glyphCount == index) { + memcpy(buffer, &string->Characters[i], glyphWidth); + return; + } + + glyphCount++; + i += glyphWidth; + } } void TStringFree(TString* string) { @@ -91,6 +121,8 @@ int GetGlyphByteWidth(const char* glyph) { } void TStringCopyGlyph(const char* glyph, int length, TString* string) { + if (length < 1) return; + if (string->ByteCount + length > string->Capacity) { char* buffer = realloc(string->Characters, string->Capacity * 2); diff --git a/tstring.h b/tstring.h index 95108de..982fa3d 100644 --- a/tstring.h +++ b/tstring.h @@ -17,5 +17,6 @@ void TStringAppendText(const char* text, TString* string); char* TStringGetString(const TString* string); void TStringGetGlyph(int index, char buffer[8], const TString* string); void TStringFree(TString* string); +int TStringLength(const TString* string); #endif \ No newline at end of file