Added a way to delete a range of characters at any index and count.
This commit is contained in:
@@ -58,6 +58,21 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (input[0] == 'r') {
|
if (input[0] == 'r') {
|
||||||
|
if (input[1] != '\0') {
|
||||||
|
printf("Select index: ");
|
||||||
|
fgets(input, sizeof input, stdin);
|
||||||
|
|
||||||
|
int num = strtol(input, NULL, 10);
|
||||||
|
printf("Enter count: ");
|
||||||
|
|
||||||
|
fgets(input, sizeof input, stdin);
|
||||||
|
|
||||||
|
int count = strtol(input, NULL, 10);
|
||||||
|
|
||||||
|
TStringRemoveRange(num, count, string);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
TStringPop(glyph, string);
|
TStringPop(glyph, string);
|
||||||
printf("Popped '%s'\n", glyph);
|
printf("Popped '%s'\n", glyph);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
void TStringCopyGlyph(const char* glyph, int length, TString* string);
|
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 ResizeTStringCapacity(int newTextLength, TString* string);
|
||||||
int GetGlyphByteWidth(char glyph);
|
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) {
|
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;
|
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;
|
string->ByteCount += insertionTextLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TStringCompactString(unsigned int startByte, unsigned int endByte, TString* string) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void TStringAppendText(const char* text, TString* string) {
|
void TStringAppendText(const char* text, TString* string) {
|
||||||
if (!text || !string) return;
|
if (!text || !string) return;
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ void TStringFree(TString* string);
|
|||||||
void TStringPrintDetails(const TString* string, int statsOnly);
|
void TStringPrintDetails(const TString* string, int statsOnly);
|
||||||
void TStringInsertText(const char* text, int index, TString* string);
|
void TStringInsertText(const char* text, int index, TString* string);
|
||||||
void TStringPop(char buffer[8], TString* string);
|
void TStringPop(char buffer[8], TString* string);
|
||||||
|
void TStringRemoveRange(int startIndex, int count, TString* string);
|
||||||
char* TStringGetString(const TString* string);
|
char* TStringGetString(const TString* string);
|
||||||
int TStringLength(const TString* string);
|
int TStringLength(const TString* string);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user