traits.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <contrib/libs/pcre/pcre.h>
  3. #include <util/generic/ptr.h> // THolder
  4. #include <util/system/types.h> // wchar16, wchar32
  5. namespace NPcre {
  6. template <class TCharType>
  7. struct TPcreTraits;
  8. template <>
  9. struct TPcreTraits<char> {
  10. using TCharType = char;
  11. using TStringType = const char*;
  12. using TCodeType = pcre;
  13. using TExtraType = pcre_extra;
  14. static constexpr TCodeType* (*Compile)(TStringType pattern, int options, int* errcodeptr, const char** errptr, int* erroffset, const unsigned char* tableptr) = pcre_compile2;
  15. static constexpr TExtraType* (*Study)(const TCodeType* pattern, int options, const char** errptr) = pcre_study;
  16. static constexpr int (*Exec)(const TCodeType* code, const TExtraType* extra, TStringType str, int length, int startoffset, int options, int* ovector, int ovecsize) = pcre_exec;
  17. };
  18. template <>
  19. struct TPcreTraits<wchar16> {
  20. using TCharType = wchar16;
  21. using TStringType = PCRE_SPTR16;
  22. using TCodeType = pcre16;
  23. using TExtraType = pcre16_extra;
  24. static constexpr TCodeType* (*Compile)(TStringType pattern, int options, int* errcodeptr, const char** errptr, int* erroffset, const unsigned char* tableptr) = pcre16_compile2;
  25. static constexpr TExtraType* (*Study)(const TCodeType* pattern, int options, const char** errptr) = pcre16_study;
  26. static constexpr int (*Exec)(const TCodeType* code, const TExtraType* extra, TStringType str, int length, int startoffset, int options, int* ovector, int ovecsize) = pcre16_exec;
  27. };
  28. template <>
  29. struct TPcreTraits<wchar32> {
  30. using TCharType = wchar32;
  31. using TStringType = PCRE_SPTR32;
  32. using TCodeType = pcre32;
  33. using TExtraType = pcre32_extra;
  34. static constexpr TCodeType* (*Compile)(TStringType pattern, int options, int* errcodeptr, const char** errptr, int* erroffset, const unsigned char* tableptr) = pcre32_compile2;
  35. static constexpr TExtraType* (*Study)(const TCodeType* pattern, int options, const char** errptr) = pcre32_study;
  36. static constexpr int (*Exec)(const TCodeType* code, const TExtraType* extra, TStringType str, int length, int startoffset, int options, int* ovector, int ovecsize) = pcre32_exec;
  37. };
  38. template <class TCharType>
  39. struct TFreePcre;
  40. template <>
  41. struct TFreePcre<char> {
  42. static inline void Destroy(void* ptr) noexcept {
  43. pcre_free(ptr);
  44. }
  45. };
  46. template <>
  47. struct TFreePcre<wchar16> {
  48. static inline void Destroy(void* ptr) noexcept {
  49. pcre16_free(ptr);
  50. }
  51. };
  52. template <>
  53. struct TFreePcre<wchar32> {
  54. static inline void Destroy(void* ptr) noexcept {
  55. pcre32_free(ptr);
  56. }
  57. };
  58. template <class TCharType>
  59. struct TFreePcreExtra;
  60. template <>
  61. struct TFreePcreExtra<char> {
  62. static inline void Destroy(pcre_extra* ptr) noexcept {
  63. pcre_free_study(ptr);
  64. }
  65. };
  66. template <>
  67. struct TFreePcreExtra<wchar16> {
  68. static inline void Destroy(pcre16_extra* ptr) noexcept {
  69. pcre16_free_study(ptr);
  70. }
  71. };
  72. template <>
  73. struct TFreePcreExtra<wchar32> {
  74. static inline void Destroy(pcre32_extra* ptr) noexcept {
  75. pcre32_free_study(ptr);
  76. }
  77. };
  78. template <typename TCharType>
  79. using TPcreCode = THolder<typename TPcreTraits<TCharType>::TCodeType, TFreePcre<TCharType>>;
  80. template <typename TCharType>
  81. using TPcreExtra = THolder<typename TPcreTraits<TCharType>::TExtraType, TFreePcreExtra<TCharType>>;
  82. }