change the code and input range
This commit is contained in:
parent
cd7f6daaf9
commit
ec31d0b799
@ -3,48 +3,31 @@
|
||||
using namespace deepstate;
|
||||
|
||||
char* encode(const char* input) {
|
||||
unsigned int l = strlen(input);
|
||||
char* encoded = (char*)malloc((l*2)+1);
|
||||
if (l == 0) {
|
||||
encoded[0] = '\0';
|
||||
return encoded;
|
||||
}
|
||||
unsigned char last = input[0];
|
||||
unsigned char current = input[0];
|
||||
int count = 1;
|
||||
char* encoded = (char*)malloc((strlen(input)*2)+1);
|
||||
int pos = 0;
|
||||
for (int i = 1; i < l; i++) {
|
||||
current = input[i];
|
||||
if ((current == last) && (count < 26)) {
|
||||
count++;
|
||||
} else {
|
||||
encoded[pos++] = last;
|
||||
encoded[pos++] = 64 + count;
|
||||
last = current;
|
||||
count = 1;
|
||||
if (strlen(input) > 0) {
|
||||
unsigned char last = input[0]; int count = 1;
|
||||
for (int i = 1; i < strlen(input); 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++] = 65;
|
||||
}
|
||||
encoded[pos++] = last;
|
||||
encoded[pos++] = 65;
|
||||
encoded[pos] = '\0';
|
||||
return encoded;
|
||||
}
|
||||
|
||||
char* decode(const char* output) {
|
||||
unsigned int l = strlen(output);
|
||||
char* decoded = (char*)malloc(((l/2)*26)+1);
|
||||
if (l == 0) {
|
||||
decoded[0] = '\0';
|
||||
return decoded;
|
||||
}
|
||||
char* decoded = (char*)malloc(((strlen(output))/2)*26);
|
||||
int pos = 0;
|
||||
unsigned char current;
|
||||
for (int i = 0; i < l; i++) {
|
||||
current = output[i++];
|
||||
unsigned int count = output[i] - 64;
|
||||
for (int j = 0; j < count; j++) {
|
||||
decoded[pos++] = current;
|
||||
}
|
||||
if (strlen(output) > 0) {
|
||||
for (int i = 0; i < strlen(output); i += 2)
|
||||
for (int j = 0; j < (output[i+1] - 64); j++)
|
||||
decoded[pos++] = output[i];
|
||||
}
|
||||
decoded[pos] = '\0';
|
||||
return decoded;
|
||||
@ -53,7 +36,7 @@ char* decode(const char* output) {
|
||||
#define MAX_STR_LEN 10
|
||||
|
||||
TEST(Runlength, EncodeDecode) {
|
||||
char* original = DeepState_CStrUpToLen(MAX_STR_LEN, "abcde");
|
||||
char* original = DeepState_CStrUpToLen(MAX_STR_LEN, "ab");
|
||||
LOG(TRACE) << "original = `" << original << "`";
|
||||
char* encoded = encode(original);
|
||||
char* roundtrip = decode(encoded);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user