You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.4 KiB

  1. #include "stringutils.hpp"
  2. using namespace iflytop;
  3. using namespace core;
  4. using namespace std;
  5. /***********************************************************************************************************************
  6. * ======================================================private====================================================== *
  7. ***********************************************************************************************************************/
  8. char StringUtils::byteToChar(uint8_t byte) {
  9. if (byte < 10) {
  10. return '0' + byte;
  11. } else {
  12. return 'A' + (byte - 10);
  13. }
  14. throw std::out_of_range("byteToChar out of range");
  15. return 'x';
  16. }
  17. bool StringUtils::isLegalHex(char c) {
  18. if (c >= '0' && c <= '9') {
  19. return true;
  20. } else if (c >= 'A' && c <= 'F') {
  21. return true;
  22. } else if (c >= 'a' && c <= 'f') {
  23. return true;
  24. }
  25. return false;
  26. }
  27. /***********************************************************************************************************************
  28. * ======================================================public======================================================= *
  29. ***********************************************************************************************************************/
  30. string StringUtils::upper(const string& value) {
  31. string cpy = value;
  32. transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper);
  33. return cpy;
  34. }
  35. string StringUtils::lower(const string& value) {
  36. string cpy = value;
  37. transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower);
  38. return cpy;
  39. }
  40. string StringUtils::bytesToString(const uint8_t* data, size_t size) {
  41. string ret;
  42. for (unsigned i = 0; i < size; ++i) {
  43. uint8_t hight4 = data[i] >> 4 & 0x0f;
  44. uint8_t low4 = data[i] >> 0 & 0x0f;
  45. ret += byteToChar(hight4);
  46. ret += byteToChar(low4);
  47. }
  48. return ret;
  49. }
  50. string& StringUtils::replaceAllDistinct(string& str, const string& old_value, const string& new_value) {
  51. for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
  52. if ((pos = str.find(old_value, pos)) != string::npos) {
  53. str.replace(pos, old_value.length(), new_value);
  54. } else {
  55. break;
  56. }
  57. }
  58. return str;
  59. }
  60. bool StringUtils::hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable) {
  61. string hexTable;
  62. byteTable.clear();
  63. if (!delims.empty()) {
  64. hexTable = replaceAllDistinct(in, delims, "");
  65. /* code */
  66. } else {
  67. hexTable = in;
  68. }
  69. if (hexTable.length() % 2 != 0) {
  70. // printf("ss\n");
  71. return false;
  72. }
  73. try {
  74. for (unsigned i = 0; i < hexTable.length(); i += 2) {
  75. string hex = hexTable.substr(i, 2);
  76. // printf("ss1 %s\n", hex.c_str());
  77. if (!isLegalHex(hex.c_str()[0])) {
  78. return false;
  79. }
  80. if (!isLegalHex(hex.c_str()[1])) {
  81. return false;
  82. }
  83. int value = std::stoi(hex, 0, 16);
  84. byteTable.push_back((uint8_t)value);
  85. }
  86. } catch (const std::exception& e) {
  87. // printf("ss1\n");
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * @brief
  94. *
  95. * @param value
  96. * @return string
  97. */
  98. string StringUtils::bytet2Binary(uint32_t value, int bitCount, bool remoteZero) {
  99. string ret;
  100. for (int i = 0; i < bitCount; ++i) {
  101. uint32_t bit = value & 0x01;
  102. value = value >> 1;
  103. if (bit == 0) {
  104. ret = "0" + ret;
  105. } else {
  106. ret = "1" + ret;
  107. }
  108. }
  109. if (remoteZero) {
  110. while (ret.length() > 0 && ret[0] == '0') {
  111. ret = ret.substr(1, ret.length() - 1);
  112. }
  113. }
  114. return ret;
  115. }
  116. string StringUtils::bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero) {
  117. string ret;
  118. for (int i = 0; i < bitCount; ++i) {
  119. uint32_t bit = value & 0x01;
  120. value = value >> 1;
  121. if (bit == 0) {
  122. ret = ret + "0";
  123. } else {
  124. ret = ret + "1";
  125. }
  126. }
  127. if (remoteZero) {
  128. while (ret.length() > 0 && ret[ret.length() - 1] == '0') {
  129. ret = ret.substr(0, ret.length() - 1);
  130. }
  131. }
  132. return ret;
  133. }
  134. string StringUtils::escapeSequence(const string& raw_str) {
  135. string escaped_str;
  136. for (const char& ch : raw_str) {
  137. switch (ch) {
  138. case '\n':
  139. escaped_str.append("\\n");
  140. break;
  141. case '\r':
  142. escaped_str.append("\\r");
  143. break;
  144. case '\t':
  145. escaped_str.append("\\t");
  146. break;
  147. case '\"':
  148. escaped_str.append("\\\"");
  149. break;
  150. case '\\':
  151. escaped_str.append("\\\\");
  152. break;
  153. default:
  154. escaped_str.push_back(ch);
  155. break;
  156. }
  157. }
  158. return escaped_str;
  159. }