Added a function to get a range of characters from the TString object.

This commit is contained in:
2022-07-05 23:33:44 -05:00
parent ce4878e103
commit b1d79d7b7e
3 changed files with 54 additions and 5 deletions
+31 -5
View File
@@ -5,7 +5,6 @@
#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);
@@ -171,10 +170,6 @@ 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;
@@ -214,6 +209,37 @@ char* TStringGetString(const TString* string) {
return text;
}
char* TStringGetStringRange(int startIndex, int count, const TString* string) {
if (!string) return NULL;
if (startIndex < 0 || startIndex >= string->Length) return NULL;
if (count > string->Length) return NULL;
char* text = calloc(string->ByteCount + 1, sizeof(char));
if (!text) {
fprintf(stderr, "Failed to calloc memory for TString text. %s.\n", strerror(errno));
return NULL;
}
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 && endByte <= string->ByteCount; glyphCount++) endByte += GetGlyphByteWidth(string->Characters[endByte]);
memcpy(text, &string->Characters[startByte], endByte - startByte);
return text;
}
void TStringGetGlyph(int index, char buffer[8], const TString* string) {
if (!buffer || !string) return;