escape.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <util/generic/strbuf.h>
  4. template <class TChar>
  5. TBasicString<TChar>& EscapeCImpl(const TChar* str, size_t len, TBasicString<TChar>&);
  6. template <class TChar>
  7. TBasicString<TChar>& UnescapeCImpl(const TChar* str, size_t len, TBasicString<TChar>&);
  8. template <class TChar>
  9. TChar* UnescapeC(const TChar* str, size_t len, TChar* buf);
  10. template <typename TChar>
  11. static inline TBasicString<TChar>& EscapeC(const TChar* str, size_t len, TBasicString<TChar>& s) {
  12. return EscapeCImpl(str, len, s);
  13. }
  14. template <typename TChar>
  15. static inline TBasicString<TChar> EscapeC(const TChar* str, size_t len) {
  16. TBasicString<TChar> s;
  17. return EscapeC(str, len, s);
  18. }
  19. template <typename TChar>
  20. static inline TBasicString<TChar> EscapeC(const TBasicStringBuf<TChar>& str) {
  21. return EscapeC(str.data(), str.size());
  22. }
  23. template <typename TChar>
  24. static inline TBasicString<TChar>& UnescapeC(const TChar* str, size_t len, TBasicString<TChar>& s) {
  25. return UnescapeCImpl(str, len, s);
  26. }
  27. template <typename TChar>
  28. static inline TBasicString<TChar> UnescapeC(const TChar* str, size_t len) {
  29. TBasicString<TChar> s;
  30. return UnescapeCImpl(str, len, s);
  31. }
  32. template <typename TChar>
  33. static inline TBasicString<TChar> EscapeC(TChar ch) {
  34. return EscapeC(&ch, 1);
  35. }
  36. template <typename TChar>
  37. static inline TBasicString<TChar> EscapeC(const TChar* str) {
  38. return EscapeC(str, std::char_traits<TChar>::length(str));
  39. }
  40. TString& EscapeC(const TStringBuf str, TString& res);
  41. TUtf16String& EscapeC(const TWtringBuf str, TUtf16String& res);
  42. // these two need to be methods, because of TBasicString::Quote implementation
  43. TString EscapeC(const TString& str);
  44. TUtf16String EscapeC(const TUtf16String& str);
  45. TString& UnescapeC(const TStringBuf str, TString& res);
  46. TUtf16String& UnescapeC(const TWtringBuf str, TUtf16String& res);
  47. TString UnescapeC(const TStringBuf str);
  48. TUtf16String UnescapeC(const TWtringBuf wtr);
  49. /// Returns number of chars in escape sequence.
  50. /// - 0, if begin >= end
  51. /// - 1, if [begin, end) starts with an unescaped char
  52. /// - at least 2 (including '\'), if [begin, end) starts with an escaped symbol
  53. template <class TChar>
  54. size_t UnescapeCCharLen(const TChar* begin, const TChar* end);