other.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "other.h"
  2. #include <util/string/util.h>
  3. #include <util/system/yassert.h>
  4. /********************************************************/
  5. /********************************************************/
  6. static const Tr InvertTr(".:/?#", "\005\004\003\002\001");
  7. static const Tr RevertTr("\005\004\003\002\001", ".:/?#");
  8. void TrspChars(char* s) {
  9. InvertTr.Do(s);
  10. }
  11. void UnTrspChars(char* s) {
  12. RevertTr.Do(s);
  13. }
  14. void TrspChars(char* s, size_t l) {
  15. InvertTr.Do(s, l);
  16. }
  17. void UnTrspChars(char* s, size_t l) {
  18. RevertTr.Do(s, l);
  19. }
  20. void TrspChars(const char* s, char* d) {
  21. InvertTr.Do(s, d);
  22. }
  23. void UnTrspChars(const char* s, char* d) {
  24. RevertTr.Do(s, d);
  25. }
  26. void InvertDomain(char* begin, char* end) {
  27. // skip schema if it is present
  28. const auto dotPos = TStringBuf{begin, end}.find('.');
  29. if (dotPos == TStringBuf::npos)
  30. return; // no need to invert anything
  31. const auto schemaendPos = TStringBuf{begin, end}.find("://", 3);
  32. if (schemaendPos < dotPos)
  33. begin += schemaendPos + 3;
  34. char* sl = (char*)memchr(begin, '/', end - begin);
  35. char* cl = (char*)memchr(begin, ':', sl ? sl - begin : end - begin);
  36. end = cl ? cl : (sl ? sl : end);
  37. // invert string
  38. for (size_t i = 0, n = end - begin; i < n / 2; ++i)
  39. DoSwap(begin[i], begin[n - i - 1]);
  40. // invert back each host name segment
  41. char* b = begin;
  42. while (true) {
  43. char* e = (char*)memchr(b, '.', end - b);
  44. if (!e)
  45. e = end;
  46. for (size_t i = 0, n = e - b; i < n / 2; ++i)
  47. DoSwap(b[i], b[n - i - 1]);
  48. if (e == end)
  49. break;
  50. b = e + 1;
  51. }
  52. }
  53. void InvertUrl(char* begin, char* end) {
  54. char* slash = strchr(begin, '/');
  55. if (slash) {
  56. *slash = 0;
  57. }
  58. strlwr(begin);
  59. if (slash) {
  60. *slash = '/';
  61. }
  62. InvertDomain(begin, end);
  63. TrspChars(begin);
  64. }
  65. void RevertUrl(char* begin, char* end) {
  66. UnTrspChars(begin);
  67. InvertDomain(begin, end);
  68. }