Skip to content

Commit

Permalink
Hack to fix DgIntegerToString for negatives
Browse files Browse the repository at this point in the history
  • Loading branch information
knot126 committed Jul 6, 2024
1 parent fc494dd commit 73c8d30
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions source/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ char *DgStringConcatinate(const char * const string1, const char * const string2
return result;
}

void DgStringConcatinate_Test(void) {
const char *tests[] = {
"first", "second",
"another ... ", "test!",
NULL
};

for (size_t i = 0; tests[i * 2] != NULL; i++) {
char *result = DgStringConcatinate(tests[i * 2], tests[i * 2 + 1]);
DgLog(DG_LOG_VERBOSE, "'%s' + '%s' = '%s'", tests[i * 2], tests[i * 2 + 1], result);
DgMemoryFree(result);
}

DgLog(DG_LOG_VERBOSE, "%s", DgStringConcatinate("test", NULL));
DgLog(DG_LOG_VERBOSE, "%s", DgStringConcatinate(NULL, "test"));
DgLog(DG_LOG_VERBOSE, "%s", DgStringConcatinate(NULL, NULL));
}

char *DgStringConcatinateL(const char * const string1, const char * const string2) {
/**
* Concatinate string1 and string2 and then free string1
Expand Down Expand Up @@ -677,11 +695,11 @@ char *DgIntegerToString(uint8_t base, int64_t data) {

// Find if it's negative
bool negative = data < 0;

// Find the amount of memory needed for our string.
size_t string_length = 0;

if (negative) { string_length++; }
if (negative) { string_length++; data = -data; }

int64_t temp = data;

Expand Down Expand Up @@ -713,6 +731,37 @@ char *DgIntegerToString(uint8_t base, int64_t data) {
return out;
}

void DgIntegerToString_Test(void) {
int64_t tests[] = {
10, 100,
12, 100,
16, 255,
36, 23456789,
22, 1049,
10, 10,
6, 10,
3, 156,
7, 100,
8, 100,
9, 100,
11, 100,
10, 0,
12, 0,
8, 0,
10, -100,
10, -123456,
16, -130,
24, -11111,
0
};

for (size_t i = 0; tests[2 * i]; i++) {
char *output = DgIntegerToString(tests[2 * i], tests[2 * i + 1]);
DgLog(DG_LOG_INFO, "%lld as a string in base %d is '%s'", tests[2 * i], tests[2 * i + 1], output);
DgMemoryFree(output);
}
}

const char gBase64EncodeTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
Expand Down

0 comments on commit 73c8d30

Please sign in to comment.