Updated the interface for the TStringGetStringRange function. It will now fill a buffer, leaving a null byte at the end, with as many characters as it can depending on the supplied buffer's size.

This commit is contained in:
2022-07-10 15:17:18 -05:00
parent eff939cc8f
commit c201da9736
3 changed files with 23 additions and 31 deletions
+7 -9
View File
@@ -7,6 +7,7 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
char input[1024] = { 0 }; char input[1024] = { 0 };
char glyph[8] = { 0 }; char glyph[8] = { 0 };
char buffer[11] = { 0 };
char* file; char* file;
size_t bytesRead; size_t bytesRead;
@@ -90,11 +91,12 @@ int main(int argc, char** argv) {
int count = strtol(input, NULL, 10); int count = strtol(input, NULL, 10);
char* text = TStringGetStringRange(num, count, string); //char* text = TStringGetStringRange(num, count, string);
TStringGetStringRange(string, num, count, sizeof buffer, buffer);
printf("Returned String:\n'%s'\n", text); printf("Returned String:\n'%s'\n", buffer);
for (int i = 0; i < sizeof buffer; i++) printf("%02X ", buffer[i] & 0xFF);
free(text); printf("\n");
continue; continue;
} }
@@ -102,10 +104,6 @@ int main(int argc, char** argv) {
TStringAppendText(input, string); TStringAppendText(input, string);
} }
//fgets(glyph, 10, stdin);
//glyph[strlen(glyph) - 1] = 0;
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
TStringFree(string); TStringFree(string);
} }
+15 -21
View File
@@ -202,35 +202,29 @@ char* TStringGetString(const TString* string) {
return text; return text;
} }
char* TStringGetStringRange(int startIndex, int count, const TString* string) { void TStringGetStringRange(const TString* string, unsigned int startIndex, unsigned int count, unsigned int bufferSize, char buffer[]) {
if (!string) return NULL; if (!string || !buffer) return;
if (startIndex < 0 || startIndex >= string->Length) return NULL; if (count > string->Length) return;
if (count > string->Length) return NULL;
char* text = calloc(string->ByteCount + 1, sizeof(char)); unsigned int glyphCount = 0, startByte = 0;
if (!text) { while (glyphCount != startIndex) {
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]); startByte += GetGlyphByteWidth(string->Characters[startByte]);
glyphCount++; glyphCount++;
} }
unsigned int endByte = startByte; unsigned int endByte = startByte;
for (glyphCount = 0; glyphCount < count && endByte <= string->ByteCount; glyphCount++) endByte += GetGlyphByteWidth(string->Characters[endByte]); for(glyphCount = 0; glyphCount < count && endByte <= string->ByteCount && endByte - startByte < bufferSize; glyphCount++) {
int width = GetGlyphByteWidth(string->Characters[endByte]);
memcpy(text, &string->Characters[startByte], endByte - startByte);
return text; if (width + endByte - startByte < bufferSize) endByte += width;
else break;
}
memset(buffer, 0, bufferSize);
memcpy(buffer, &string->Characters[startByte], endByte - startByte);
} }
void TStringGetGlyph(int index, char buffer[8], const TString* string) { void TStringGetGlyph(int index, char buffer[8], const TString* string) {
+1 -1
View File
@@ -15,8 +15,8 @@ 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); void TStringRemoveRange(int startIndex, int count, TString* string);
void TStringGetStringRange(const TString* string, unsigned int startIndex, unsigned int count, unsigned int bufferSize, char buffer[]);
char* TStringGetString(const TString* string); char* TStringGetString(const TString* string);
char* TStringGetStringRange(int startIndex, int count, const TString* string);
int TStringLength(const TString* string); int TStringLength(const TString* string);