encodings.hxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /** Internal string encodings support for libpqxx
  2. *
  3. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  4. *
  5. * See COPYING for copyright license. If you did not receive a file called
  6. * COPYING with this source code, please notify the distributor of this mistake,
  7. * or contact the author.
  8. */
  9. #ifndef PQXX_H_ENCODINGS
  10. #define PQXX_H_ENCODINGS
  11. #include "pqxx/compiler-public.hxx"
  12. #include "pqxx/compiler-internal-pre.hxx"
  13. #include "pqxx/internal/encoding_group.hxx"
  14. #include <string>
  15. namespace pqxx
  16. {
  17. namespace internal
  18. {
  19. const char *name_encoding(int encoding_id);
  20. /// Convert libpq encoding enum or encoding name to its libpqxx group.
  21. encoding_group enc_group(int /* libpq encoding ID */);
  22. encoding_group enc_group(const std::string&);
  23. /// Function type: "find the end of the current glyph."
  24. /** This type of function takes a text buffer, and a location in that buffer,
  25. * and returns the location one byte past the end of the current glyph.
  26. *
  27. * The start offset marks the beginning of the current glyph. It must fall
  28. * within the buffer.
  29. *
  30. * There are multiple different glyph scnaner implementations, for different
  31. * kinds of encodings.
  32. */
  33. using glyph_scanner_func =
  34. std::string::size_type(
  35. const char buffer[],
  36. std::string::size_type buffer_len,
  37. std::string::size_type start);
  38. /// Look up the glyph scanner function for a given encoding group.
  39. /** To identify the glyph boundaries in a buffer, call this to obtain the
  40. * scanner function appropriate for the buffer's encoding. Then, repeatedly
  41. * call the scanner function to find the glyphs.
  42. */
  43. PQXX_LIBEXPORT glyph_scanner_func *get_glyph_scanner(encoding_group);
  44. /// Find a single-byte "needle" character in a "haystack" text buffer.
  45. std::string::size_type find_with_encoding(
  46. encoding_group enc,
  47. const std::string& haystack,
  48. char needle,
  49. std::string::size_type start = 0
  50. );
  51. PQXX_LIBEXPORT std::string::size_type find_with_encoding(
  52. encoding_group enc,
  53. const std::string& haystack,
  54. const std::string& needle,
  55. std::string::size_type start = 0
  56. );
  57. /// Iterate over the glyphs in a buffer.
  58. /** Scans the glyphs in the buffer, and for each, passes its begin and its
  59. * one-past-end pointers to @c callback.
  60. */
  61. template<typename CALLABLE> PQXX_LIBEXPORT inline void for_glyphs(
  62. encoding_group enc,
  63. CALLABLE callback,
  64. const char buffer[],
  65. std::string::size_type buffer_len,
  66. std::string::size_type start = 0
  67. )
  68. {
  69. const auto scan = get_glyph_scanner(enc);
  70. for (
  71. std::string::size_type here = start, next;
  72. here < buffer_len;
  73. here = next
  74. )
  75. {
  76. next = scan(buffer, buffer_len, here);
  77. callback(buffer + here, buffer + next);
  78. }
  79. }
  80. } // namespace pqxx::internal
  81. } // namespace pqxx
  82. #include "pqxx/compiler-internal-post.hxx"
  83. #endif