Finished the string insertion function.

This commit is contained in:
2022-07-02 21:02:11 -05:00
parent 4fa7438a97
commit 7c20eef8d1
3 changed files with 34 additions and 31 deletions
+6 -5
View File
@@ -19,19 +19,20 @@ int main(int argc, char** argv) {
char* test2 = u8"\xE3\x81\x84"; char* test2 = u8"\xE3\x81\x84";
char* test4 = u8"\xF0\xA0\xBA\x8C"; char* test4 = u8"\xF0\xA0\xBA\x8C";
char* test5 = "B"; char* test5 = "B";
char* test3 = " Just some extra texts"; //char* test3 = " Just some extra texts";
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);
printf("From string index %d: '%s' (string length is %d)\n", index, characters, TStringLength(string)); TStringInsertText("**INS**", 2, string);
printf("And one English character: '%s'\n", glyph); //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)); //printf("Full string: '%s'\n", TStringGetString(string));
TStringPrintDetails(string); TStringPrintDetails(string);
+26 -24
View File
@@ -73,35 +73,33 @@ void TStringRemove(int index, TString* string) {
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;
int textLength = strlen(text); unsigned int insertionTextLength = strlen(text);
int glyphCount = 0; unsigned int startByte = 0;
int startByte = 0; unsigned int endByte = string->ByteCount + insertionTextLength;
if (!ResizeTStringCapacity(textLength, string)) return; if (!ResizeTStringCapacity(insertionTextLength, string)) return;
while(startByte < string->ByteCount) {
if (glyphCount == index) break;
startByte += GetGlyphByteWidth(&string->Characters[startByte]);
glyphCount++;
}
}
void TStringMemSwap(const char* text, int startIndex, int length, TString* string) {
if (!text || !string) return;
char buffer[256] = { 0 }; char buffer[256] = { 0 };
char intermediate[sizeof buffer] = { 0 };
const char* inputTextStart = text;
const char* characterListStart = &string->Characters[startIndex];
int blockCount = length / sizeof buffer; for (int glyphCount = 0; glyphCount != index; glyphCount++) startByte += GetGlyphByteWidth(&string->Characters[startByte]);
int remainder = length % sizeof buffer; for (unsigned int offset = 0; offset < insertionTextLength; string->Length++) offset += GetGlyphByteWidth(&text[offset]);
for(; blockCount > 0; blockCount--) { unsigned int fullBuffers = (string->ByteCount - startByte) / sizeof buffer;
unsigned int remainder = (string->ByteCount - startByte) - fullBuffers * sizeof buffer;
for (int i = fullBuffers; i > 0; i--) {
memcpy(buffer, &string->Characters[string->ByteCount - i * sizeof buffer], sizeof buffer);
memcpy(&string->Characters[endByte - sizeof buffer * i], buffer, sizeof buffer);
} }
if (remainder > 0) {
memcpy(buffer, &string->Characters[startByte], remainder);
memcpy(&string->Characters[insertionTextLength + startByte], buffer, remainder);
}
memcpy(&string->Characters[startByte], text, insertionTextLength);
string->ByteCount += insertionTextLength;
} }
void TStringAppendText(const char* text, TString* string) { void TStringAppendText(const char* text, TString* string) {
@@ -172,7 +170,7 @@ void TStringFree(TString* string) {
} }
int GetGlyphByteWidth(const char* glyph) { int GetGlyphByteWidth(const char* glyph) {
if (!glyph) return -1; if (!glyph) return 0;
if ((*glyph & 0x80) == 0) { if ((*glyph & 0x80) == 0) {
return 1; return 1;
@@ -187,7 +185,7 @@ int GetGlyphByteWidth(const char* glyph) {
return 4; return 4;
} }
return -1; return 0;
} }
void TStringCopyGlyph(const char* glyph, int length, TString* string) { void TStringCopyGlyph(const char* glyph, int length, TString* string) {
@@ -212,4 +210,8 @@ void TStringPrintDetails(const TString* string) {
printf("%s\n", text); printf("%s\n", text);
free(text); free(text);
} }
printf("Bytes:");
for(int i = 0; i < string->Capacity; i++) printf(" %02X", string->Characters[i] & 0xFF);
printf("\n");
} }
+1 -1
View File
@@ -12,7 +12,7 @@ void TStringAppendText(const char* text, TString* string);
void TStringGetGlyph(int index, char buffer[8], const TString* string); 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 TStringRemove(int index, TString* string);
char* TStringGetString(const TString* string); char* TStringGetString(const TString* string);
int TStringLength(const TString* string); int TStringLength(const TString* string);