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.

72 lines
2.1 KiB

5 months ago
  1. #pragma once
  2. #include <algorithm>
  3. #include <regex>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. namespace iflytop {
  8. namespace core {
  9. using namespace std;
  10. class StringUtils {
  11. char byteToChar(uint8_t byte);
  12. bool isLegalHex(char c);
  13. public:
  14. string upper(const string& value);
  15. string lower(const string& value);
  16. string bytesToString(const uint8_t* data, size_t size);
  17. string bytesToString(const char* data, size_t size) { return bytesToString((const uint8_t*)data, size); }
  18. string bytesToString(const vector<uint8_t>& byteTable) { return bytesToString(byteTable.data(), byteTable.size()); }
  19. int32_t split(const string& str, const string& delim, vector<string>& ret);
  20. /**
  21. * @brief new_value="",
  22. *
  23. * @param str
  24. * @param old_value
  25. * @param new_value
  26. * @return string&
  27. */
  28. string& replaceAllDistinct(string& str, const string& old_value, const string& new_value);
  29. /**
  30. * @brief HEX字符串成Bytes数组例如
  31. *
  32. * // string("12,0a,2a,ab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  33. * vector<uint8_t> tobytes;
  34. * hexStringToBytes("12,0a,2a,ab",",",tobytes);
  35. *
  36. * // string("120a2aab") ===> char buf[] = {0x12,0x0a,0x2a,0xab}
  37. * vector<uint8_t> tobytes;
  38. * hexStringToBytes("120a2aab","",tobytes);
  39. *
  40. * @param in
  41. * @param delims
  42. * @param byteTable byte数组
  43. * @return true
  44. * @return false
  45. */
  46. bool hexStringToBytes(string in, string delims, vector<uint8_t>& byteTable);
  47. /**
  48. * @brief
  49. *
  50. * @param value 0x55
  51. * @param bitCount 8
  52. * @param remoteZero true/false
  53. * @return string 01010101
  54. *
  55. * Demo:
  56. * StringUtils::bytet2Binary(0x55,8,true) ===> 1010101
  57. * StringUtils::bytet2Binary(0x55,8,false) ===> 01010101
  58. *
  59. */
  60. string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true);
  61. string bytet2BinaryHumanReadable(uint32_t value, int bitCount);
  62. string bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero = true);
  63. string escapeSequence(const string& raw_str);
  64. };
  65. } // namespace core
  66. } // namespace iflytop