Added a function to dump some debug details about the TString.

This commit is contained in:
2022-06-30 22:24:13 -05:00
parent 3564c43ff4
commit 0874579e92
3 changed files with 15 additions and 83 deletions
+3 -79
View File
@@ -3,20 +3,6 @@
#include <string.h>
#include "tstring.h"
// #define DEFAULT_STRING_SIZE 32
// typedef struct __string {
// char* Characters;
// int Length;
// int ByteCount;
// int Capacity;
// } TString;
// int GetGlyphByteWidth(const char* glyph);
// void AppendGlyph(const char* glyph, TString* string);
// void GetCharByIndex(int index, char characters[8], const TString* string);
// TString* CreateString(void);
// const unsigned char First = 0x70;
// const unsigned char Teo = 0xE0;
// const unsigned char Three = 0xF0;
@@ -33,7 +19,7 @@ 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 text";
char* test3 = " Just some extra texts";
TStringAppendText(test, string);
TStringAppendText(test2, string);
@@ -47,73 +33,11 @@ int main(int argc, char** argv) {
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);
//fgets(glyph, 10, stdin);
//glyph[strlen(glyph) - 1] = 0;
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
}
// TString* CreateString(void) {
// TString* string = malloc(sizeof(TString));
// string->Characters = calloc(DEFAULT_STRING_SIZE, sizeof(char));
// string->Capacity = DEFAULT_STRING_SIZE;
// string->ByteCount = 0;
// string->Length = 0;
// return string;
// }
// void AppendGlyph(const char* glyph, TString* string) {
// int length = strlen(glyph);
// memcpy(&string->Characters[string->ByteCount], glyph, length);
// string->ByteCount += length;
// string->Length++;
// }
// void GetCharByIndex(int index, char characters[8], const TString* string) {
// int glyphCount = 0;
// for(int i = 0; i < string->ByteCount;) {
// if (glyphCount == index) {
// memcpy(characters, &string->Characters[i], GetGlyphByteWidth(&string->Characters[i]));
// return;
// }
// glyphCount++;
// i += GetGlyphByteWidth(&string->Characters[i]);
// }
// }
// int GetGlyphByteWidth(const char* glyph) {
// printf("Bytes for '%s': ", glyph);
// for(size_t i = 0; i < strlen(glyph); i++) {
// printf("%02X ", 0xFF & glyph[i]);
// }
// printf("\n");
// unsigned char result = *glyph & 0xFF;
// 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;
// }
// fprintf(stderr, "Failed to get glyph %02X\n", 0xFF & *glyph);
// exit(-1);
// }
}
+8 -3
View File
@@ -1,7 +1,4 @@
#include "tstring.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
int GetGlyphByteWidth(const char* glyph);
void TStringCopyGlyph(const char* glyph, int length, TString* string);
@@ -140,4 +137,12 @@ void TStringCopyGlyph(const char* glyph, int length, TString* string) {
string->ByteCount += length;
string->Length++;
}
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("%s", TStringGetString(string));
printf("\n");
}
+4 -1
View File
@@ -13,10 +13,13 @@
typedef struct __tstring TString;
TString* TStringCreate(void);
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);
void TStringPrintDetails(const TString* string);
char* TStringGetString(const TString* string);
int TStringLength(const TString* string);
#endif