diff --git a/main.c b/main.c index 17b8c69..63dbe34 100644 --- a/main.c +++ b/main.c @@ -24,17 +24,20 @@ int main(int argc, char** argv) { TStringAppendText(test, string); TStringAppendText(test2, string); TStringAppendText(test4, string); - TStringAppendText(test5, string); + //TStringAppendText(test5, string); //TStringAppendText(test3, string); - TStringGetGlyph(index, characters, string); + //TStringGetGlyph(index, characters, string); TStringGetGlyph(3, glyph, string); + //TStringPrintDetails(string); + TStringInsertText("END", 2, string); TStringPrintDetails(string); - TStringInsertText("**INS**", 2, string); - //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)); + TStringPop(glyph, string); + printf("%s\n", glyph); TStringPrintDetails(string); + TStringPop(glyph, string); + TStringPrintDetails(string); + printf("%s\n", glyph); //fgets(glyph, 10, stdin); diff --git a/tstring.c b/tstring.c index 967657f..77836e6 100644 --- a/tstring.c +++ b/tstring.c @@ -4,7 +4,7 @@ #include #include -int GetGlyphByteWidth(const char* glyph); +int GetGlyphByteWidth(char glyph); void TStringCopyGlyph(const char* glyph, int length, TString* string); int ResizeTStringCapacity(int newTextLength, TString* string); @@ -66,12 +66,31 @@ int ResizeTStringCapacity(int newTextLength, TString* string) { return 1; } -void TStringRemove(int index, TString* string) { +void TStringPop(char buffer[8], TString* string) { + if (!string) return; + + memset(buffer, '\0', 8); + unsigned int offset = 0; + + 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); + memset(&string->Characters[i], '\0', width); + string->Length--; + string->ByteCount -= width; + break; + } + } } void TStringInsertText(const char* text, int index, TString* string) { if (!text || !string) return; + if (index < 0 || index > string->Length) index = string->Length; unsigned int insertionTextLength = strlen(text); unsigned int startByte = 0; @@ -81,8 +100,8 @@ void TStringInsertText(const char* text, int index, TString* string) { char buffer[256] = { 0 }; - for (int glyphCount = 0; glyphCount != index; glyphCount++) startByte += GetGlyphByteWidth(&string->Characters[startByte]); - for (unsigned int offset = 0; offset < insertionTextLength; string->Length++) offset += GetGlyphByteWidth(&text[offset]); + for (int glyphCount = 0; glyphCount != index; glyphCount++) startByte += GetGlyphByteWidth(string->Characters[startByte]); + for (unsigned int offset = 0; offset < insertionTextLength; string->Length++) offset += GetGlyphByteWidth(text[offset]); unsigned int fullBuffers = (string->ByteCount - startByte) / sizeof buffer; unsigned int remainder = (string->ByteCount - startByte) - fullBuffers * sizeof buffer; @@ -106,7 +125,7 @@ void TStringAppendText(const char* text, TString* string) { if (!text || !string) return; int totalLength = strlen(text); - int glyphLength = GetGlyphByteWidth(text); + int glyphLength = GetGlyphByteWidth(text[0]); //If the two lengths are the same then that means we have a single character. //Simply append it to the list and return. @@ -121,7 +140,7 @@ void TStringAppendText(const char* text, TString* string) { TStringCopyGlyph(&text[i], glyphLength, string); i += glyphLength; - glyphLength = GetGlyphByteWidth(&text[i]); + glyphLength = GetGlyphByteWidth(text[i]); } } @@ -149,7 +168,7 @@ void TStringGetGlyph(int index, char buffer[8], const TString* string) { int glyphCount = 0; for(int i = 0; i < string->ByteCount;) { - int glyphWidth = GetGlyphByteWidth(&string->Characters[i]); + int glyphWidth = GetGlyphByteWidth(string->Characters[i]); if (glyphCount == index) { memcpy(buffer, &string->Characters[i], glyphWidth); @@ -169,19 +188,19 @@ void TStringFree(TString* string) { free(string); } -int GetGlyphByteWidth(const char* glyph) { +int GetGlyphByteWidth(char glyph) { if (!glyph) return 0; - if ((*glyph & 0x80) == 0) { + if ((glyph & 0x80) == 0) { return 1; } - else if ((*glyph & 0xE0) == 0xC0) { + else if ((glyph & 0xE0) == 0xC0) { return 2; } - else if ((*glyph & 0xF0) == 0xE0) { + else if ((glyph & 0xF0) == 0xE0) { return 3; } - else if ((*glyph & 0xF8) == 0xF0) { + else if ((glyph & 0xF8) == 0xF0) { return 4; } diff --git a/tstring.h b/tstring.h index 866ecad..153a15f 100644 --- a/tstring.h +++ b/tstring.h @@ -13,7 +13,7 @@ void TStringGetGlyph(int index, char buffer[8], const TString* string); void TStringFree(TString* string); void TStringPrintDetails(const TString* string); void TStringInsertText(const char* text, int index, TString* string); -//void TStringRemove(int index, TString* string); +void TStringPop(char buffer[8], TString* string); char* TStringGetString(const TString* string); int TStringLength(const TString* string);