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
+27 -25
View File
@@ -73,35 +73,33 @@ void TStringRemove(int index, TString* string) {
void TStringInsertText(const char* text, int index, TString* string) {
if (!text || !string) return;
int textLength = strlen(text);
int glyphCount = 0;
int startByte = 0;
unsigned int insertionTextLength = strlen(text);
unsigned int startByte = 0;
unsigned int endByte = string->ByteCount + insertionTextLength;
if (!ResizeTStringCapacity(textLength, 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;
if (!ResizeTStringCapacity(insertionTextLength, string)) return;
char buffer[256] = { 0 };
char intermediate[sizeof buffer] = { 0 };
const char* inputTextStart = text;
const char* characterListStart = &string->Characters[startIndex];
int blockCount = length / sizeof buffer;
int remainder = length % sizeof buffer;
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(; 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) {
@@ -172,7 +170,7 @@ void TStringFree(TString* string) {
}
int GetGlyphByteWidth(const char* glyph) {
if (!glyph) return -1;
if (!glyph) return 0;
if ((*glyph & 0x80) == 0) {
return 1;
@@ -187,7 +185,7 @@ int GetGlyphByteWidth(const char* glyph) {
return 4;
}
return -1;
return 0;
}
void TStringCopyGlyph(const char* glyph, int length, TString* string) {
@@ -212,4 +210,8 @@ void TStringPrintDetails(const TString* string) {
printf("%s\n", text);
free(text);
}
printf("Bytes:");
for(int i = 0; i < string->Capacity; i++) printf(" %02X", string->Characters[i] & 0xFF);
printf("\n");
}