Skip to content

Instantly share code, notes, and snippets.

@Unlimiter Unlimiter/upisc.c
Last active May 24, 2019

Embed
What would you like to do?
A method to compress strings of digit pairs to strings of predefined symbols.
/* upisc (Unlimiter's Pair-based Integer String Compression)
* A method to compress strings of digit pairs into strings of predefined symbols.
* The symbol dictionary doesn't contain every possible combination of pairs, but pairs that can be reversed to produce other pairs.
*
* In the command line, the program takes 1 argument, the integer string you want compress. It must have an even length.
* If one of the pairs in the argument is not in the dictionary, the program reverses it then translates it, after that, it puts a dot after it to denote it has been reversed.
*
* — Unlimiter
*/
/*
* Dictionary of symbols (pairs get translated into symbols at compression):
* Symbol Pair
* 0 00
* 1 01
* 2 02
* 3 03
* 4 04
* 5 05
* 6 06
* 7 07
* 8 08
* 9 09
* a 11
* b 12
* c 13
* d 14
* e 15
* f 16
* g 17
* h 18
* i 19
* j 22
* k 23
* l 24
* m 25
* n 26
* o 27
* p 28
* q 29
* r 33
* s 34
* t 35
* u 36
* v 37
* w 38
* x 39
* y 44
* z 45
* A 46
* B 47
* C 48
* D 49
* E 55
* F 56
* G 57
* H 58
* I 59
* J 66
* K 67
* L 68
* M 69
* N 77
* O 78
* P 79
* Q 88
* R 89
* S 99
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* pairs[55] = {
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"44",
"45",
"46",
"47",
"48",
"49",
"55",
"56",
"57",
"58",
"59",
"66",
"67",
"68",
"69",
"77",
"78",
"79",
"88",
"89",
"99"
};
char symbols[55] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS";
char* swap(char* pair) {
char first[2] = {pair[0], '\0'};
char second[2] = {pair[1], '\0'};
return strcat(second, first);
}
int findIndex(char* pair) {
int i = 0;
while(i < 55) {
if (!strcmp(pair, pairs[i])) {
break;
}
else if (!strcmp(swap(pair), pairs[i])) {
break;
}
i++;
}
return i;
}
_Bool exists(char* pair) {
for (int i = 0; i < 55; i++) {
if (!strcmp(pair, pairs[i])) return 1;
}
return 0;
}
char* encode(char* pair) {
char* str = (char[3]) {(char) symbols[findIndex(pair)], '\0'};
if (!exists(pair)) {
strcpy(str, (char[4]) {(char) symbols[findIndex(pair)], '.', '\0'});
}
return str;
}
char result[256];
char* translate(char* target) {
if (!strlen(target)) return "";
else if ((strlen(target) % 2) != 0) {
fprintf(
stderr,
"%s",
"\033[1;31mError:\033[0m length of number must be even"
);
return "";
}
for (int i = 0; target[i] != '\0'; i += 2) {
char cur[3] = {target[i], target[i + 1], '\0'};
if (strlen(cur) > 1) {
strcat(result, encode(cur));
memset(cur, 0, 3);
}
}
return result;
}
int main(int argc, char** argv) {
printf(
(argc > 1) ?
"%s\n"
: "",
(argc > 1) ?
translate(argv[1])
: translate("")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.