Added code to handle appending multiple characters.

This commit is contained in:
2022-06-30 22:09:30 -05:00
parent d4331290e5
commit 3564c43ff4
3 changed files with 41 additions and 7 deletions
+8 -7
View File
@@ -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);
+32
View File
@@ -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);
+1
View File
@@ -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