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
+9 -6
View File
@@ -24,17 +24,20 @@ int main(int argc, char** argv) {
TStringAppendText(test, string); TStringAppendText(test, string);
TStringAppendText(test2, string); TStringAppendText(test2, string);
TStringAppendText(test4, string); TStringAppendText(test4, string);
TStringAppendText(test5, string); //TStringAppendText(test5, string);
//TStringAppendText(test3, string); //TStringAppendText(test3, string);
TStringGetGlyph(index, characters, string); //TStringGetGlyph(index, characters, string);
TStringGetGlyph(3, glyph, string); TStringGetGlyph(3, glyph, string);
//TStringPrintDetails(string);
TStringInsertText("END", 2, string);
TStringPrintDetails(string); TStringPrintDetails(string);
TStringInsertText("**INS**", 2, string); TStringPop(glyph, string);
//printf("From string index %d: '%s' (string length is %d)\n", index, characters, TStringLength(string)); printf("%s\n", glyph);
//printf("And one English character: '%s'\n", glyph);
//printf("Full string: '%s'\n", TStringGetString(string));
TStringPrintDetails(string); TStringPrintDetails(string);
TStringPop(glyph, string);
TStringPrintDetails(string);
printf("%s\n", glyph);
//fgets(glyph, 10, stdin); //fgets(glyph, 10, stdin);
+31 -12
View File
@@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
int GetGlyphByteWidth(const char* glyph); int GetGlyphByteWidth(char glyph);
void TStringCopyGlyph(const char* glyph, int length, TString* string); void TStringCopyGlyph(const char* glyph, int length, TString* string);
int ResizeTStringCapacity(int newTextLength, TString* string); int ResizeTStringCapacity(int newTextLength, TString* string);
@@ -66,12 +66,31 @@ int ResizeTStringCapacity(int newTextLength, TString* string) {
return 1; 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) { void TStringInsertText(const char* text, int index, TString* string) {
if (!text || !string) return; if (!text || !string) return;
if (index < 0 || index > string->Length) index = string->Length;
unsigned int insertionTextLength = strlen(text); unsigned int insertionTextLength = strlen(text);
unsigned int startByte = 0; unsigned int startByte = 0;
@@ -81,8 +100,8 @@ void TStringInsertText(const char* text, int index, TString* string) {
char buffer[256] = { 0 }; char buffer[256] = { 0 };
for (int glyphCount = 0; glyphCount != index; glyphCount++) startByte += GetGlyphByteWidth(&string->Characters[startByte]); 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 (unsigned int offset = 0; offset < insertionTextLength; string->Length++) offset += GetGlyphByteWidth(text[offset]);
unsigned int fullBuffers = (string->ByteCount - startByte) / sizeof buffer; unsigned int fullBuffers = (string->ByteCount - startByte) / sizeof buffer;
unsigned int remainder = (string->ByteCount - startByte) - fullBuffers * 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; if (!text || !string) return;
int totalLength = strlen(text); 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. //If the two lengths are the same then that means we have a single character.
//Simply append it to the list and return. //Simply append it to the list and return.
@@ -121,7 +140,7 @@ void TStringAppendText(const char* text, TString* string) {
TStringCopyGlyph(&text[i], glyphLength, string); TStringCopyGlyph(&text[i], glyphLength, string);
i += glyphLength; 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; int glyphCount = 0;
for(int i = 0; i < string->ByteCount;) { for(int i = 0; i < string->ByteCount;) {
int glyphWidth = GetGlyphByteWidth(&string->Characters[i]); int glyphWidth = GetGlyphByteWidth(string->Characters[i]);
if (glyphCount == index) { if (glyphCount == index) {
memcpy(buffer, &string->Characters[i], glyphWidth); memcpy(buffer, &string->Characters[i], glyphWidth);
@@ -169,19 +188,19 @@ void TStringFree(TString* string) {
free(string); free(string);
} }
int GetGlyphByteWidth(const char* glyph) { int GetGlyphByteWidth(char glyph) {
if (!glyph) return 0; if (!glyph) return 0;
if ((*glyph & 0x80) == 0) { if ((glyph & 0x80) == 0) {
return 1; return 1;
} }
else if ((*glyph & 0xE0) == 0xC0) { else if ((glyph & 0xE0) == 0xC0) {
return 2; return 2;
} }
else if ((*glyph & 0xF0) == 0xE0) { else if ((glyph & 0xF0) == 0xE0) {
return 3; return 3;
} }
else if ((*glyph & 0xF8) == 0xF0) { else if ((glyph & 0xF8) == 0xF0) {
return 4; return 4;
} }
+1 -1
View File
@@ -13,7 +13,7 @@ void TStringGetGlyph(int index, char buffer[8], const TString* string);
void TStringFree(TString* string); void TStringFree(TString* string);
void TStringPrintDetails(const TString* string); void TStringPrintDetails(const TString* string);
void TStringInsertText(const char* text, int index, 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); char* TStringGetString(const TString* string);
int TStringLength(const TString* string); int TStringLength(const TString* string);