rcode.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef __RCODE_H
  2. #define __RCODE_H
  3. #include <string>
  4. #include <cstdint>
  5. namespace rcode {
  6. std::string magick_encode(uint32_t x) {
  7. static const char* topchar = "om";
  8. static const char* hichars[32] = {
  9. "e", "ao", "yi", "wu", "si",
  10. "shi", "ti", "di", "ni", "tian",
  11. "dian", "nian", "yin", "da", "ta",
  12. "ting", "niao", "feng", "ming", "kong",
  13. "long", "hong", "shu", "sheng", "chi",
  14. "chu", "cheng", "lu", "liao", "hao",
  15. "ru", "ri"
  16. };
  17. /*
  18. static const char* lochars[16] = {
  19. "anp", "ard", "als", "amz",
  20. "emp", "est", "elz", "erp",
  21. "orz", "ond", "olp", "osf",
  22. "urd", "und", "ump", "upl"
  23. };
  24. */
  25. static const char* lochars1[16] = {
  26. "an", "as", "al", "ad",
  27. "es", "em", "et", "er",
  28. "os", "of", "ow", "os",
  29. "un", "um", "up", "uf"
  30. };
  31. static const char* lochars2[16] = {
  32. "arn", "ard", "atl", "alv",
  33. "ern", "erd", "est", "etz",
  34. "orn", "ord", "olv", "otl",
  35. "urn", "urd", "ums", "utz"
  36. };
  37. uint16_t lo2 = x & 0xFF;
  38. uint16_t lo1 = (x >> 8) & 0xFF;
  39. uint16_t hic = (x >> 16) & 0x1F;
  40. uint16_t hib = (x >> 21) & 0x1F;
  41. uint16_t hia = (x >> 26) & 0x1F;
  42. bool top = (x >> 31);
  43. std::string ret;
  44. if (top) {
  45. ret += topchar;
  46. ret += "-";
  47. }
  48. if (hia) {
  49. ret += hichars[hia];
  50. ret += " ";
  51. }
  52. if (hib) {
  53. ret += hichars[hib];
  54. }
  55. ret += hichars[hic];
  56. ret += "-";
  57. ret += lochars1[(lo1 >> 4) & 0xF];
  58. ret += lochars1[lo1 & 0xF];
  59. ret += lochars2[(lo2 >> 4) & 0xF];
  60. ret += lochars2[lo2 & 0xF];
  61. return ret;
  62. }
  63. template <typename T>
  64. std::string encode(T x) {
  65. static const std::string v =
  66. "0123456789ABCDEFGHJKMNPQRSTUWXYZ";
  67. std::string ret;
  68. while (x != 0) {
  69. ret += v[x&31];
  70. x = x >> 5;
  71. }
  72. return ret;
  73. }
  74. template <typename T>
  75. T decode(const std::string& x) {
  76. const T v[128] = {
  77. 0, 0, 0, 0, 0, 0, 0, 0, // 0x00-0x07
  78. 0, 0, 0, 0, 0, 0, 0, 0, // 0x08-0x0f
  79. 0, 0, 0, 0, 0, 0, 0, 0, // 0x10-0x17
  80. 0, 0, 0, 0, 0, 0, 0, 0, // 0x18-0x1f
  81. 0, 0, 0, 0, 0, 0, 0, 0, // 0x20-0x27 ' '-"'"
  82. 0, 0, 0, 0, 0, 0, 0, 0, // 0x28-0x2f '('-'/'
  83. 1, 2, 3, 4, 5, 6, 7, 8, // 0x30-0x37 '0'-'7'
  84. 9, 10, 0, 0, 0, 0, 0, 0, // 0x38-0x3f '8'-'?'
  85. 0, 11, 12, 13, 14, 15, 16, 17, // 0x40-0x47 '@'-'G'
  86. 18, 2, 19, 20, 2, 21, 22, 1, // 0x48-0x4f 'H'-'O'
  87. 23, 24, 25, 26, 27, 28, 28, 29, // 0x50-0x57 'P'-'W'
  88. 30, 31, 32, 0, 0, 0, 0, 0, // 0x58-0x5f 'X'-'_'
  89. 0, 11, 12, 13, 14, 15, 16, 17, // 0x40-0x47 '`'-'g'
  90. 18, 2, 19, 20, 2, 21, 22, 1, // 0x48-0x4f 'h'-'o'
  91. 23, 24, 25, 26, 27, 28, 28, 29, // 0x50-0x57 'p'-'w'
  92. 30, 31, 32, 0, 0, 0, 0, 0 // 0x58-0x5f 'x'-DEL
  93. };
  94. T ret = 0;
  95. for (int j = x.size() - 1; j >= 0; --j) {
  96. unsigned int i = x[j];
  97. if (i >= 128) continue;
  98. T n = v[i & 0x7F];
  99. if (n > 0) {
  100. ret = ret << 5;
  101. ret |= (n-1);
  102. }
  103. }
  104. return ret;
  105. }
  106. }
  107. #endif