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:
@@ -9,36 +9,77 @@
|
||||
// const unsigned char Four = 0xF7;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
char input[1024] = { 0 };
|
||||
char glyph[8] = { 0 };
|
||||
char characters[8] = { 42 };
|
||||
// char characters[8] = { 42 };
|
||||
|
||||
// int index = 2;
|
||||
// TString* string = TStringCreate();
|
||||
// //0xF0 0xA0 0xBA 0x8C
|
||||
// char* test = u8"\xE5\x87\x84";
|
||||
// char* test2 = u8"\xE3\x81\x84";
|
||||
// char* test4 = u8"\xF0\xA0\xBA\x8C";
|
||||
// char* test5 = "B";
|
||||
// //char* test3 = " Just some extra texts";
|
||||
|
||||
// TStringAppendText(test, string);
|
||||
// TStringAppendText(test2, string);
|
||||
// TStringAppendText(test4, string);
|
||||
// //TStringAppendText(test5, string);
|
||||
// //TStringAppendText(test3, string);
|
||||
|
||||
// //TStringGetGlyph(index, characters, string);
|
||||
// TStringGetGlyph(3, glyph, string);
|
||||
// //TStringPrintDetails(string);
|
||||
// TStringInsertText("END", 2, string);
|
||||
// TStringPrintDetails(string);
|
||||
// TStringPop(glyph, string);
|
||||
// printf("%s\n", glyph);
|
||||
// TStringPrintDetails(string);
|
||||
// TStringPop(glyph, string);
|
||||
// TStringPrintDetails(string);
|
||||
// printf("%s\n", glyph);
|
||||
|
||||
int index = 2;
|
||||
TString* string = TStringCreate();
|
||||
//0xF0 0xA0 0xBA 0x8C
|
||||
char* test = u8"\xE5\x87\x84";
|
||||
char* test2 = u8"\xE3\x81\x84";
|
||||
char* test4 = u8"\xF0\xA0\xBA\x8C";
|
||||
char* test5 = "B";
|
||||
//char* test3 = " Just some extra texts";
|
||||
|
||||
TStringAppendText(test, string);
|
||||
TStringAppendText(test2, string);
|
||||
TStringAppendText(test4, string);
|
||||
//TStringAppendText(test5, string);
|
||||
//TStringAppendText(test3, string);
|
||||
while(1) {
|
||||
printf("> ");
|
||||
fgets(input, sizeof input, stdin);
|
||||
|
||||
//TStringGetGlyph(index, characters, string);
|
||||
TStringGetGlyph(3, glyph, string);
|
||||
//TStringPrintDetails(string);
|
||||
TStringInsertText("END", 2, string);
|
||||
TStringPrintDetails(string);
|
||||
TStringPop(glyph, string);
|
||||
printf("%s\n", glyph);
|
||||
TStringPrintDetails(string);
|
||||
TStringPop(glyph, string);
|
||||
TStringPrintDetails(string);
|
||||
printf("%s\n", glyph);
|
||||
input[strlen(input) - 1] = '\0';
|
||||
|
||||
if (input[0] == 'q') break;
|
||||
|
||||
if (input[0] == 'p') {
|
||||
TStringPrintDetails(string);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input[0] == 'i') {
|
||||
printf("Select index: ");
|
||||
fgets(input, sizeof input, stdin);
|
||||
|
||||
int num = strtol(input, NULL, 10);
|
||||
|
||||
printf("Enter text to insert at index %d: ", num);
|
||||
|
||||
fgets(input, sizeof input, stdin);
|
||||
|
||||
input[strlen(input) - 1] = '\0';
|
||||
|
||||
TStringInsertText(input, num, string);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input[0] == 'r') {
|
||||
TStringPop(glyph, string);
|
||||
printf("Popped '%s'\n", glyph);
|
||||
continue;
|
||||
}
|
||||
|
||||
TStringAppendText(input, string);
|
||||
}
|
||||
//fgets(glyph, 10, stdin);
|
||||
|
||||
//glyph[strlen(glyph) - 1] = 0;
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user