Minor refactoring. Added a flag in the TString details function to only print some basic stats instead of always showing the stored string and the byte representation.
This commit is contained in:
@@ -21,7 +21,8 @@ int main(int argc, char** argv) {
|
||||
if (input[0] == 'q') break;
|
||||
|
||||
if (input[0] == 'p') {
|
||||
TStringPrintDetails(string);
|
||||
if (input[1] == '\0') TStringPrintDetails(string, 0);
|
||||
else TStringPrintDetails(string, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,13 +47,13 @@ int TStringLength(const TString* string) {
|
||||
return string->Length;
|
||||
}
|
||||
|
||||
int ResizeTStringCapacity(int newTextLength, TString* string) {
|
||||
int ResizeTStringCapacity(int additionalLength, TString* string) {
|
||||
if (!string) return 0;
|
||||
if (string->ByteCount + newTextLength < string->Capacity) return 1;
|
||||
if (string->ByteCount + additionalLength < string->Capacity) return 1;
|
||||
|
||||
unsigned int newCapacity = string->Capacity;
|
||||
|
||||
while (newTextLength > newCapacity) newCapacity = string->Capacity * 2;
|
||||
while (additionalLength > newCapacity) newCapacity = string->Capacity * 2;
|
||||
|
||||
char* buffer = realloc(string->Characters, string->Capacity * 2);
|
||||
|
||||
@@ -192,8 +192,6 @@ void TStringFree(TString* string) {
|
||||
}
|
||||
|
||||
int GetGlyphByteWidth(char glyph) {
|
||||
if (!glyph) return 0;
|
||||
|
||||
//ANDing 0x80 (1000 0000) should result in 0 if this code point is 1 byte long.
|
||||
if ((glyph & 0x80) == 0) return 1;
|
||||
|
||||
@@ -223,10 +221,14 @@ void TStringCopyGlyph(const char* glyph, int length, TString* string) {
|
||||
string->Length++;
|
||||
}
|
||||
|
||||
void TStringPrintDetails(const TString* string) {
|
||||
void TStringPrintDetails(const TString* string, int statsOnly) {
|
||||
if (!string) return;
|
||||
|
||||
printf("Stats: Length %d; Size %d byte(s); Capacity: %d bytes; Stored String:\n\n", string->Length, string->ByteCount, string->Capacity);
|
||||
printf("Stats: Length %d; Size %d byte(s); Capacity: %d bytes\n", string->Length, string->ByteCount, string->Capacity);
|
||||
|
||||
if (statsOnly) return;
|
||||
|
||||
printf("Stored String:\n");
|
||||
|
||||
if (string->Characters) {
|
||||
char* text = TStringGetString(string);
|
||||
|
||||
@@ -11,7 +11,7 @@ TString* TStringCreate(void);
|
||||
void TStringAppendText(const char* text, TString* string);
|
||||
void TStringGetGlyph(int index, char buffer[8], const TString* string);
|
||||
void TStringFree(TString* string);
|
||||
void TStringPrintDetails(const TString* string);
|
||||
void TStringPrintDetails(const TString* string, int statsOnly);
|
||||
void TStringInsertText(const char* text, int index, TString* string);
|
||||
void TStringPop(char buffer[8], TString* string);
|
||||
char* TStringGetString(const TString* string);
|
||||
|
||||
Reference in New Issue
Block a user