Added a string truncate function to clear the string and reset it to a default state.

This commit is contained in:
2022-07-18 16:27:44 -05:00
parent c201da9736
commit 58de344698
3 changed files with 28 additions and 0 deletions
+9
View File
@@ -102,6 +102,15 @@ int main(int argc, char** argv) {
} }
} }
if (input[0] == 't') {
TStringPrintDetails(string, 0);
TStringTruncate(string);
printf("TString truncated\n");
continue;
}
TStringAppendText(input, string); TStringAppendText(input, string);
} }
+18
View File
@@ -41,6 +41,24 @@ TString* TStringCreate(void) {
return string; return string;
} }
void TStringTruncate(TString* string) {
if (!string) return;
char* newBuffer = calloc(DEFAULT_TSTRING_BYTE_BUFFER, sizeof(char));
if (!newBuffer) {
fprintf(stderr, "Failed to calloc space during TString truncation. %s\n", strerror(errno));
return;
}
free(string->Characters);
string->Characters = newBuffer;
string->Capacity = DEFAULT_TSTRING_BYTE_BUFFER;
string->ByteCount = 0;
string->Length = 0;
}
int TStringLength(const TString* string) { int TStringLength(const TString* string) {
if (!string) return -1; if (!string) return -1;
+1
View File
@@ -8,6 +8,7 @@
typedef struct __tstring TString; typedef struct __tstring TString;
TString* TStringCreate(void); TString* TStringCreate(void);
void TStringTruncate(TString* string);
void TStringAppendText(const char* text, TString* string); void TStringAppendText(const char* text, TString* string);
void TStringGetGlyph(int index, char buffer[8], const TString* string); void TStringGetGlyph(int index, char buffer[8], const TString* string);
void TStringFree(TString* string); void TStringFree(TString* string);