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:
+7
-9
@@ -7,6 +7,7 @@
|
||||
int main(int argc, char** argv) {
|
||||
char input[1024] = { 0 };
|
||||
char glyph[8] = { 0 };
|
||||
char buffer[11] = { 0 };
|
||||
char* file;
|
||||
size_t bytesRead;
|
||||
|
||||
@@ -90,11 +91,12 @@ int main(int argc, char** argv) {
|
||||
|
||||
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);
|
||||
|
||||
free(text);
|
||||
printf("Returned String:\n'%s'\n", buffer);
|
||||
for (int i = 0; i < sizeof buffer; i++) printf("%02X ", buffer[i] & 0xFF);
|
||||
printf("\n");
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -102,10 +104,6 @@ int main(int argc, char** argv) {
|
||||
|
||||
TStringAppendText(input, string);
|
||||
}
|
||||
//fgets(glyph, 10, stdin);
|
||||
|
||||
//glyph[strlen(glyph) - 1] = 0;
|
||||
|
||||
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
|
||||
|
||||
TStringFree(string);
|
||||
}
|
||||
+15
-21
@@ -202,35 +202,29 @@ 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;
|
||||
void TStringGetStringRange(const TString* string, unsigned int startIndex, unsigned int count, unsigned int bufferSize, char buffer[]) {
|
||||
if (!string || !buffer) return;
|
||||
if (count > string->Length) return;
|
||||
|
||||
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) {
|
||||
unsigned int glyphCount = 0, startByte = 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);
|
||||
for(glyphCount = 0; glyphCount < count && endByte <= string->ByteCount && endByte - startByte < bufferSize; glyphCount++) {
|
||||
int width = GetGlyphByteWidth(string->Characters[endByte]);
|
||||
|
||||
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) {
|
||||
|
||||
+1
-1
@@ -15,8 +15,8 @@ void TStringPrintDetails(const TString* string, int statsOnly);
|
||||
void TStringInsertText(const char* text, int index, TString* string);
|
||||
void TStringPop(char buffer[8], 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* TStringGetStringRange(int startIndex, int count, const TString* string);
|
||||
int TStringLength(const TString* string);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user