Added a way to delete a range of characters at any index and count.

This commit is contained in:
2022-07-05 19:32:58 -05:00
parent 5b5036b996
commit 0a61197cb6
3 changed files with 65 additions and 0 deletions
+49
View File
@@ -5,6 +5,7 @@
#include <errno.h>
void TStringCopyGlyph(const char* glyph, int length, TString* string);
void TStringCompactString(unsigned int startByte, unsigned int endByte, TString* string);
int ResizeTStringCapacity(int newTextLength, TString* string);
int GetGlyphByteWidth(char glyph);
@@ -91,6 +92,50 @@ void TStringPop(char buffer[8], TString* string) {
}
}
void TStringRemoveRange(int startIndex, int count, TString* string) {
if (!string) return;
if (startIndex < 0 || startIndex > string->Length) return;
if (count <= 0 || count > string->Length - startIndex) return;
char buffer[256] = { 0 };
unsigned int startByte = 0;
unsigned int glyphCount = 0;
while(glyphCount != startIndex) {
startByte += GetGlyphByteWidth(string->Characters[startByte]);
glyphCount++;
}
unsigned int endByte = startByte;
for (glyphCount = 0; glyphCount < count; glyphCount++) endByte += GetGlyphByteWidth(string->Characters[endByte]);
memset(&string->Characters[startByte], '\0', endByte - startByte);
if (endByte == string->ByteCount) {
string->Length -= glyphCount;
string->ByteCount -= endByte - startByte;
return;
}
unsigned int fullBuffers = (string->ByteCount - endByte) / sizeof buffer;
unsigned int remainder = (string->ByteCount - endByte) - 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[endByte], remainder);
memcpy(&string->Characters[startByte], buffer, remainder);
}
string->Length -= glyphCount;
string->ByteCount -= endByte - startByte;
}
void TStringInsertText(const char* text, int index, TString* string) {
if (!text || !string) return;
if (index < 0 || index > string->Length) index = string->Length;
@@ -124,6 +169,10 @@ void TStringInsertText(const char* text, int index, TString* string) {
string->ByteCount += insertionTextLength;
}
void TStringCompactString(unsigned int startByte, unsigned int endByte, TString* string) {
}
void TStringAppendText(const char* text, TString* string) {
if (!text || !string) return;