Some code clean up, started work on functions to insert text, but they're incomplete.
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
#include "tstring.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int GetGlyphByteWidth(const char* glyph);
|
||||
void TStringCopyGlyph(const char* glyph, int length, TString* string);
|
||||
int ResizeTStringCapacity(int newTextLength, TString* string);
|
||||
|
||||
struct __tstring {
|
||||
char* Characters;
|
||||
@@ -38,11 +42,68 @@ TString* TStringCreate(void) {
|
||||
}
|
||||
|
||||
int TStringLength(const TString* string) {
|
||||
if (!string) return - 1;
|
||||
if (!string) return -1;
|
||||
|
||||
return string->Length;
|
||||
}
|
||||
|
||||
int ResizeTStringCapacity(int newTextLength, TString* string) {
|
||||
if (!string) return 0;
|
||||
if (string->ByteCount + newTextLength < string->Capacity) return 1;
|
||||
|
||||
//TODO: This is a bad assumption considering the additional length could overflow without a check.
|
||||
char* buffer = realloc(string->Characters, string->Capacity * 2);
|
||||
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "Failed to realloc space for text insertion. %s.\n", strerror(errno));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
string->Characters = buffer;
|
||||
string->Capacity *= 2;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void TStringRemove(int index, TString* string) {
|
||||
|
||||
}
|
||||
|
||||
void TStringInsertText(const char* text, int index, TString* string) {
|
||||
if (!text || !string) return;
|
||||
|
||||
int textLength = strlen(text);
|
||||
int glyphCount = 0;
|
||||
int startByte = 0;
|
||||
|
||||
if (!ResizeTStringCapacity(textLength, string)) return;
|
||||
|
||||
while(startByte < string->ByteCount) {
|
||||
if (glyphCount == index) break;
|
||||
|
||||
startByte += GetGlyphByteWidth(&string->Characters[startByte]);
|
||||
|
||||
glyphCount++;
|
||||
}
|
||||
}
|
||||
|
||||
void TStringMemSwap(const char* text, int startIndex, int length, TString* string) {
|
||||
if (!text || !string) return;
|
||||
|
||||
char buffer[256] = { 0 };
|
||||
char intermediate[sizeof buffer] = { 0 };
|
||||
const char* inputTextStart = text;
|
||||
const char* characterListStart = &string->Characters[startIndex];
|
||||
|
||||
int blockCount = length / sizeof buffer;
|
||||
int remainder = length % sizeof buffer;
|
||||
|
||||
for(; blockCount > 0; blockCount--) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void TStringAppendText(const char* text, TString* string) {
|
||||
if (!text || !string) return;
|
||||
|
||||
@@ -133,18 +194,7 @@ void TStringCopyGlyph(const char* glyph, int length, TString* string) {
|
||||
if (!glyph || !string) return;
|
||||
if (length < 1) return;
|
||||
|
||||
if (string->ByteCount + length > string->Capacity) {
|
||||
char* buffer = realloc(string->Characters, string->Capacity * 2);
|
||||
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "Failed to realloc memory TString. %s.\n", strerror(errno));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
string->Characters = buffer;
|
||||
string->Capacity *= 2;
|
||||
}
|
||||
if (!ResizeTStringCapacity(length, string)) return;
|
||||
|
||||
memcpy(&string->Characters[string->ByteCount], glyph, length);
|
||||
|
||||
@@ -156,6 +206,10 @@ void TStringPrintDetails(const TString* string) {
|
||||
if (!string) return;
|
||||
|
||||
printf("Length: %d Size: %d byte(s) Capacity: %d bytes\n", string->Length, string->ByteCount, string->Capacity);
|
||||
printf("%s", TStringGetString(string));
|
||||
printf("\n");
|
||||
|
||||
if (string->Characters) {
|
||||
char* text = TStringGetString(string);
|
||||
printf("%s\n", text);
|
||||
free(text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user