From e79d4aa00ad46f7387ea0d95f7bafff9a61bf74b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 30 Jun 2022 21:31:28 -0500 Subject: [PATCH] Initial commit --- .gitignore | 2 + main.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tstring.c | 40 ++++++++++++++++++ tstring.h | 18 +++++++++ 4 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 main.c create mode 100644 tstring.c create mode 100644 tstring.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5012110 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +utf8 diff --git a/main.c b/main.c new file mode 100644 index 0000000..42c8447 --- /dev/null +++ b/main.c @@ -0,0 +1,117 @@ +#include +#include +#include + +#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; +const unsigned char Four = 0xF7; + +int main(int argc, char** argv) { + char glyph[10] = { 0 }; + char characters[8] = { 0 }; + + int index = 2; + TString* string = CreateString(); +//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\0 extra text"; + + AppendGlyph(test, string); + AppendGlyph(test2, string); + AppendGlyph(test4, string); + AppendGlyph(test5, string); + + GetCharByIndex(index, characters, string); + GetCharByIndex(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("Full string: '%s'\n", string->Characters); + + //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); +} \ No newline at end of file diff --git a/tstring.c b/tstring.c new file mode 100644 index 0000000..439d134 --- /dev/null +++ b/tstring.c @@ -0,0 +1,40 @@ +#include "tstring.h" + +struct __tstring { + char* Characters; + int Length; + int ByteCount; + int Capacity; +}; + +TString* TStringCreate(void) { + TString* string = malloc(sizeof(TString)); + + string->Characters = calloc(DEFAULT_TSTRING_BYTE_BUFFER, sizeof(char)); + + string->Capacity = DEFAULT_TSTRING_BYTE_BUFFER; + string->ByteCount = 0; + string->Length = 0; + + return string; +} + +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) { + if (!string) return; + + if (string->Characters) free(string->Characters); + + free(string); +} \ No newline at end of file diff --git a/tstring.h b/tstring.h new file mode 100644 index 0000000..63608c3 --- /dev/null +++ b/tstring.h @@ -0,0 +1,18 @@ +#ifndef TSTRING_H +#define TSTRING_H + +#include "stdlib.h" + +#ifndef DEFAULT_TSTRING_BYTE_BUFFER +#define DEFAULT_TSTRING_BYTE_BUFFER 32 +#endif + +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); + +#endif \ No newline at end of file