clean up formatting

This commit is contained in:
Alex Groce 2019-01-04 12:00:57 -07:00
parent 59a3b51f9f
commit 276ce6cb3f

View File

@ -9,17 +9,21 @@ char* encode(const char* input) {
unsigned int len = strlen(input);
char* encoded = (char*)malloc((len*2)+1);
int pos = 0;
if (strlen(input) > 0) {
unsigned char last = input[0]; int count = 1;
if (len > 0) {
unsigned char last = input[0];
int count = 1;
for (int i = 1; i < len; i++) {
if (((unsigned char)input[i] == last) && (count < 26))
count++;
else {
encoded[pos++] = last; encoded[pos++] = 64 + count;
last = (unsigned char)input[i]; count = 1;
encoded[pos++] = last;
encoded[pos++] = 64 + count;
last = (unsigned char)input[i];
count = 1;
}
}
encoded[pos++] = last; encoded[pos++] = 65; // Should be 64 + count
encoded[pos++] = last;
encoded[pos++] = 65; // Should be 64 + count
}
encoded[pos] = '\0';
return encoded;
@ -29,10 +33,10 @@ char* decode(const char* output) {
unsigned int len = strlen(output);
char* decoded = (char*)malloc((len/2)*26);
int pos = 0;
if (strlen(output) > 0) {
for (int i = 0; i < len; i += 2)
for (int j = 0; j < (output[i+1] - 64); j++)
decoded[pos++] = output[i];
for (int i = 0; i < len; i += 2) {
for (int j = 0; j < (output[i+1] - 64); j++) {
decoded[pos++] = output[i];
}
}
decoded[pos] = '\0';
return decoded;