Some code clean up, started work on functions to insert text, but they're incomplete.
This commit is contained in:
@@ -32,7 +32,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
printf("From string index %d: '%s' (string length is %d)\n", index, characters, TStringLength(string));
|
||||
printf("And one English character: '%s'\n", glyph);
|
||||
printf("Full string: '%s'\n", TStringGetString(string));
|
||||
//printf("Full string: '%s'\n", TStringGetString(string));
|
||||
TStringPrintDetails(string);
|
||||
|
||||
//fgets(glyph, 10, stdin);
|
||||
@@ -40,4 +40,5 @@ int main(int argc, char** argv) {
|
||||
//glyph[strlen(glyph) - 1] = 0;
|
||||
|
||||
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
|
||||
TStringFree(string);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,6 @@
|
||||
#ifndef TSTRING_H
|
||||
#define TSTRING_H
|
||||
|
||||
#include "stdlib.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef DEFAULT_TSTRING_BYTE_BUFFER
|
||||
#define DEFAULT_TSTRING_BYTE_BUFFER 32
|
||||
#endif
|
||||
@@ -13,11 +8,12 @@
|
||||
typedef struct __tstring TString;
|
||||
|
||||
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 TStringInsertText(const char* text, int index, TString* string);
|
||||
//void TStringRemove(int index, TString* string);
|
||||
char* TStringGetString(const TString* string);
|
||||
int TStringLength(const TString* string);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user