Helper.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include <sys/types.h>
  3. #include "NavicatCrypto.hpp"
  4. namespace Helper {
  5. extern Navicat11Crypto NavicatCipher;
  6. template<typename _Type, bool _Ascending = true>
  7. void QuickSort(_Type* pArray, off_t begin, off_t end) {
  8. if (end - begin <= 1)
  9. return;
  10. off_t i = begin;
  11. off_t j = end - 1;
  12. _Type seperator = static_cast<_Type&&>(pArray[begin]);
  13. while (i < j) {
  14. if (_Ascending) {
  15. while (i < j && seperator <= pArray[j])
  16. --j;
  17. if (i < j)
  18. pArray[i++] = static_cast<_Type&&>(pArray[j]);
  19. while (i < j && pArray[i] <= seperator)
  20. ++i;
  21. if (i < j)
  22. pArray[j--] = static_cast<_Type&&>(pArray[i]);
  23. } else {
  24. while (i < j && seperator >= pArray[j])
  25. --j;
  26. if (i < j)
  27. pArray[i++] = static_cast<_Type&&>(pArray[j]);
  28. while (i < j && pArray[i] >= seperator)
  29. ++i;
  30. if (i < j)
  31. pArray[j--] = static_cast<_Type&&>(pArray[i]);
  32. }
  33. }
  34. pArray[i] = static_cast<_Type&&>(seperator);
  35. QuickSort<_Type, _Ascending>(pArray, begin, i);
  36. QuickSort<_Type, _Ascending>(pArray, i + 1, end);
  37. }
  38. //
  39. // Print memory data in [from, to) at least
  40. // If `base` is not nullptr, print address as offset. Otherwise, as absolute address.
  41. // NOTICE:
  42. // `base` must <= `from`
  43. //
  44. void PrintMemory(const void* from, const void* to, const void* base = nullptr);
  45. void PrintBytes(const void* p, size_t s);
  46. bool IsPrintable(const uint8_t* p, size_t s);
  47. template<typename __string_t>
  48. void StringReplace(__string_t& Str, const __string_t& OldSubStr, const __string_t& NewSubStr) {
  49. typename __string_t::size_type pos = 0;
  50. auto srclen = OldSubStr.size();
  51. auto dstlen = NewSubStr.size();
  52. while ((pos = Str.find(OldSubStr, pos)) != __string_t::npos) {
  53. Str.replace(pos, srclen, NewSubStr);
  54. pos += dstlen;
  55. }
  56. }
  57. template<typename __string_t>
  58. void StringRemove(__string_t& Str, const __string_t& SubStr) {
  59. typename __string_t::size_type pos = 0;
  60. while ((pos = Str.find(SubStr, pos)) != __string_t::npos) {
  61. Str.erase(pos, SubStr.length());
  62. }
  63. }
  64. }