Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. /* upisc (Unlimiter's Pair-based Integer String Compression)
  2. * A method to compress strings of digit pairs into strings of predefined symbols.
  3. * The symbol dictionary doesn't contain every possible combination of pairs, but pairs that can be reversed to produce other pairs.
  4. *
  5. * In the command line, the program takes 1 argument, the integer string you want compress. It must have an even length.
  6. * 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.
  7. *
  8. * Please improve it if you can.
  9. * The official decompression method is not available yet, but you can make your own one by learning from this compression method.
  10. *
  11. * Compile this file and run it.
  12. *
  13. * by Unlimiter
  14. */
  15.  
  16. /*
  17. * Dictionary of symbols (pairs get translated into symbols at compression):
  18. * Symbol Pair
  19. * 0 00
  20. * 1 01
  21. * 2 02
  22. * 3 03
  23. * 4 04
  24. * 5 05
  25. * 6 06
  26. * 7 07
  27. * 8 08
  28. * 9 09
  29. * a 11
  30. * b 12
  31. * c 13
  32. * d 14
  33. * e 15
  34. * f 16
  35. * g 17
  36. * h 18
  37. * i 19
  38. * j 22
  39. * k 23
  40. * l 24
  41. * m 25
  42. * n 26
  43. * o 27
  44. * p 28
  45. * q 29
  46. * r 33
  47. * s 34
  48. * t 35
  49. * u 36
  50. * v 37
  51. * w 38
  52. * x 39
  53. * y 44
  54. * z 45
  55. * A 46
  56. * B 47
  57. * C 48
  58. * D 49
  59. * E 55
  60. * F 56
  61. * G 57
  62. * H 58
  63. * I 59
  64. * J 66
  65. * K 67
  66. * L 68
  67. * M 69
  68. * N 77
  69. * O 78
  70. * P 79
  71. * Q 88
  72. * R 89
  73. * S 99
  74. */
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79.  
  80. char* pairs[55] = {
  81. "00",
  82. "01",
  83. "02",
  84. "03",
  85. "04",
  86. "05",
  87. "06",
  88. "07",
  89. "08",
  90. "09",
  91. "11",
  92. "12",
  93. "13",
  94. "14",
  95. "15",
  96. "16",
  97. "17",
  98. "18",
  99. "19",
  100. "22",
  101. "23",
  102. "24",
  103. "25",
  104. "26",
  105. "27",
  106. "28",
  107. "29",
  108. "33",
  109. "34",
  110. "35",
  111. "36",
  112. "37",
  113. "38",
  114. "39",
  115. "44",
  116. "45",
  117. "46",
  118. "47",
  119. "48",
  120. "49",
  121. "55",
  122. "56",
  123. "57",
  124. "58",
  125. "59",
  126. "66",
  127. "67",
  128. "68",
  129. "69",
  130. "77",
  131. "78",
  132. "79",
  133. "88",
  134. "89",
  135. "99"
  136. };
  137.  
  138. char symbols[55] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS";
  139.  
  140. char* swap(char* pair) {
  141. char first[2] = {pair[0], '\0'};
  142. char second[2] = {pair[1], '\0'};
  143. return strcat(second, first);
  144. }
  145.  
  146. int findIndex(char* pair) {
  147. int i = 0;
  148. while(i < 55) {
  149. if (!strcmp(pair, pairs[i])) {
  150. break;
  151. }
  152. else if (!strcmp(swap(pair), pairs[i])) {
  153. break;
  154. }
  155. i++;
  156. }
  157. return i;
  158. }
  159.  
  160. _Bool exists(char* pair) {
  161. for (int i = 0; i < 55; i++) {
  162. if (!strcmp(pair, pairs[i])) return 1;
  163. }
  164. return 0;
  165. }
  166.  
  167. char* encode(char* pair) {
  168. char* str = (char[3]) {(char) symbols[findIndex(pair)], '\0'};
  169. if (!exists(pair)) {
  170. strcpy(str, (char[4]) {(char) symbols[findIndex(pair)], '.', '\0'});
  171. }
  172. return str;
  173. }
  174.  
  175. char result[256];
  176. char* translate(char* target) {
  177. if (!strlen(target)) return "";
  178. else if ((strlen(target) % 2) != 0) {
  179. fprintf(
  180. stderr,
  181. "%s",
  182. "\033[1;31mError:\033[0m length of number must be even"
  183. );
  184. return "";
  185. }
  186.  
  187. for (int i = 0; target[i] != '\0'; i += 2) {
  188. char cur[3] = {target[i], target[i + 1], '\0'};
  189. if (strlen(cur) > 1) {
  190. strcat(result, encode(cur));
  191. memset(cur, 0, 3);
  192. }
  193. }
  194. return result;
  195. }
  196.  
  197. int main(int argc, char** argv) {
  198. printf(
  199. (argc > 1) ?
  200. "%s\n"
  201. : "",
  202. (argc > 1) ?
  203. translate(argv[1])
  204. : translate("")
  205. );
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement