Added a function to get a range of characters from the TString object.
This commit is contained in:
@@ -78,6 +78,28 @@ int main(int argc, char** argv) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input[0] == 'g') {
|
||||
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);
|
||||
|
||||
char* text = TStringGetStringRange(num, count, string);
|
||||
|
||||
printf("Returned String:\n'%s'\n", text);
|
||||
|
||||
free(text);
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
TStringAppendText(input, string);
|
||||
}
|
||||
//fgets(glyph, 10, stdin);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ void TStringInsertText(const char* text, int index, TString* string);
|
||||
void TStringPop(char buffer[8], TString* string);
|
||||
void TStringRemoveRange(int startIndex, int count, TString* string);
|
||||
char* TStringGetString(const TString* string);
|
||||
char* TStringGetStringRange(int startIndex, int count, const TString* string);
|
||||
int TStringLength(const TString* string);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user