Added a 'pop' function to remove the last glyph from the string and store it into a buffer.

This commit is contained in:
2022-07-02 23:30:13 -05:00
parent 7c20eef8d1
commit 338835b998
3 changed files with 41 additions and 19 deletions
+31 -12
View File
@@ -4,7 +4,7 @@
#include <string.h>
#include <errno.h>
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;
}