string_transform.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h>
  3. #include <util/generic/ptr.h>
  4. #include <util/generic/refcount.h>
  5. namespace NProtobufJson {
  6. class IStringTransform: public TSimpleRefCount<IStringTransform> {
  7. public:
  8. virtual ~IStringTransform() {
  9. }
  10. /// Some transforms have special meaning.
  11. /// For example, escape transforms cause generic JSON escaping to be turned off.
  12. enum Type {
  13. EscapeTransform = 0x1,
  14. };
  15. virtual int GetType() const = 0;
  16. /// This method is called for each string field in proto
  17. virtual void Transform(TString& str) const = 0;
  18. /// This method is called for each bytes field in proto
  19. virtual void TransformBytes(TString& str) const {
  20. // Default behaviour is to apply string transform
  21. return Transform(str);
  22. }
  23. };
  24. using TStringTransformPtr = TIntrusivePtr<IStringTransform>;
  25. template <bool quote, bool tounicode>
  26. class TEscapeJTransform: public IStringTransform {
  27. public:
  28. int GetType() const override {
  29. return EscapeTransform;
  30. }
  31. void Transform(TString& str) const override {
  32. TString newStr;
  33. NEscJ::EscapeJ<quote, tounicode>(str, newStr);
  34. str = newStr;
  35. }
  36. };
  37. class TCEscapeTransform: public IStringTransform {
  38. public:
  39. int GetType() const override {
  40. return EscapeTransform;
  41. }
  42. void Transform(TString& str) const override;
  43. };
  44. class TSafeUtf8CEscapeTransform: public IStringTransform {
  45. public:
  46. int GetType() const override {
  47. return EscapeTransform;
  48. }
  49. void Transform(TString& str) const override;
  50. };
  51. class TDoubleEscapeTransform: public IStringTransform {
  52. public:
  53. int GetType() const override {
  54. return EscapeTransform;
  55. }
  56. void Transform(TString& str) const override;
  57. };
  58. class TDoubleUnescapeTransform: public NProtobufJson::IStringTransform {
  59. public:
  60. int GetType() const override {
  61. return NProtobufJson::IStringTransform::EscapeTransform;
  62. }
  63. void Transform(TString& str) const override;
  64. private:
  65. TString Unescape(const TString& str) const;
  66. };
  67. class TBase64EncodeBytesTransform: public NProtobufJson::IStringTransform {
  68. public:
  69. int GetType() const override {
  70. return 0;
  71. }
  72. void Transform(TString&) const override {
  73. // Do not transform strings
  74. }
  75. void TransformBytes(TString &str) const override;
  76. };
  77. class TBase64DecodeBytesTransform: public NProtobufJson::IStringTransform {
  78. public:
  79. int GetType() const override {
  80. return 0;
  81. }
  82. void Transform(TString&) const override {
  83. // Do not transform strings
  84. }
  85. void TransformBytes(TString &str) const override;
  86. };
  87. }