Set up a LUI to interact with the TString object. Refactored some code and improved the string capcity growth algorithm.

This commit is contained in:
2022-07-03 00:59:02 -05:00
parent 338835b998
commit 053f0ea584
2 changed files with 97 additions and 49 deletions
+32 -25
View File
@@ -4,9 +4,9 @@
#include <string.h>
#include <errno.h>
int GetGlyphByteWidth(char glyph);
void TStringCopyGlyph(const char* glyph, int length, TString* string);
int ResizeTStringCapacity(int newTextLength, TString* string);
int GetGlyphByteWidth(char glyph);
struct __tstring {
char* Characters;
@@ -50,8 +50,11 @@ int TStringLength(const TString* string) {
int ResizeTStringCapacity(int newTextLength, TString* string) {
if (!string) return 0;
if (string->ByteCount + newTextLength < string->Capacity) return 1;
unsigned int newCapacity = string->Capacity;
while (newTextLength > newCapacity) newCapacity = string->Capacity * 2;
//TODO: This is a bad assumption considering the additional length could overflow without a check.
char* buffer = realloc(string->Characters, string->Capacity * 2);
if (!buffer) {
@@ -68,18 +71,18 @@ int ResizeTStringCapacity(int newTextLength, TString* string) {
void TStringPop(char buffer[8], TString* string) {
if (!string) return;
memset(buffer, '\0', 8);
unsigned int offset = 0;
if (string->ByteCount == 0) return;
for (int i = string->ByteCount; i >= 0; i--) {
unsigned int width = GetGlyphByteWidth(string->Characters[i]);
char c = string->Characters[i];
if (c > 0x7F) continue;
if (width) {
if (buffer) memcpy(buffer, &string->Characters[i], width);
//If we've been provided a buffer, then populate it with the glyph we're removing.
if (buffer) {
memset(buffer, '\0', 8);
memcpy(buffer, &string->Characters[i], width);
}
memset(&string->Characters[i], '\0', width);
string->Length--;
string->ByteCount -= width;
@@ -167,7 +170,7 @@ void TStringGetGlyph(int index, char buffer[8], const TString* string) {
int glyphCount = 0;
for(int i = 0; i < string->ByteCount;) {
for (int i = 0; i < string->ByteCount;) {
int glyphWidth = GetGlyphByteWidth(string->Characters[i]);
if (glyphCount == index) {
@@ -191,19 +194,20 @@ void TStringFree(TString* string) {
int GetGlyphByteWidth(char glyph) {
if (!glyph) return 0;
if ((glyph & 0x80) == 0) {
return 1;
}
else if ((glyph & 0xE0) == 0xC0) {
return 2;
}
else if ((glyph & 0xF0) == 0xE0) {
return 3;
}
else if ((glyph & 0xF8) == 0xF0) {
return 4;
}
//ANDing 0x80 (1000 0000) should result in 0 if this code point is 1 byte long.
if ((glyph & 0x80) == 0) return 1;
//ANDing 0xE0 (1110 0000) will result in 0xC0 (1100 0000) if this code point is 2 bytes long.
if ((glyph & 0xE0) == 0xC0) return 2;
//ANDing 0xF0 (1111 0000) will result in 0xE0 (1110 0000) if this code point is 3 bytes long.
if ((glyph & 0xF0) == 0xE0) return 3;
//ANDing 0xE0 (1111 1000) will result in 0xF0 (1111 0000) if this code point is 4 bytes long.
if ((glyph & 0xF8) == 0xF0) return 4;
//Else we either have a continuation point (pattern: 10XX XXXX) or its just a junk value
//as far as we're concerned.
return 0;
}
@@ -222,7 +226,7 @@ void TStringCopyGlyph(const char* glyph, int length, TString* string) {
void TStringPrintDetails(const TString* string) {
if (!string) return;
printf("Length: %d Size: %d byte(s) Capacity: %d bytes\n", string->Length, string->ByteCount, string->Capacity);
printf("Stats: Length %d; Size %d byte(s); Capacity: %d bytes; Stored String:\n\n", string->Length, string->ByteCount, string->Capacity);
if (string->Characters) {
char* text = TStringGetString(string);
@@ -230,7 +234,10 @@ void TStringPrintDetails(const TString* string) {
free(text);
}
printf("Bytes:");
for(int i = 0; i < string->Capacity; i++) printf(" %02X", string->Characters[i] & 0xFF);
printf("\nBytes:\n");
for(int i = 0; i < string->Capacity; i++) {
if (i != 0 && i % 80 == 0) printf("\n");
printf("%02X ", string->Characters[i] & 0xFF);
}
printf("\n");
}