TextBufferIterator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // TextBufferIterator.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/TextBufferIterator.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Text
  8. // Module: TextBufferIterator
  9. //
  10. // Definition of the TextBufferIterator class.
  11. //
  12. // Copyright (c) 2010, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_TextBufferIterator_INCLUDED
  18. #define Foundation_TextBufferIterator_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include <cstdlib>
  21. namespace Poco {
  22. class TextEncoding;
  23. class Foundation_API TextBufferIterator
  24. /// An unidirectional iterator for iterating over characters in a buffer.
  25. /// The TextBufferIterator uses a TextEncoding object to
  26. /// work with multi-byte character encodings like UTF-8.
  27. /// Characters are reported in Unicode.
  28. ///
  29. /// Example: Count the number of UTF-8 characters in a buffer.
  30. ///
  31. /// UTF8Encoding utf8Encoding;
  32. /// char buffer[] = "...";
  33. /// TextBufferIterator it(buffer, utf8Encoding);
  34. /// TextBufferIterator end(it.end());
  35. /// int n = 0;
  36. /// while (it != end) { ++n; ++it; }
  37. ///
  38. /// NOTE: When an UTF-16 encoding is used, surrogate pairs will be
  39. /// reported as two separate characters, due to restrictions of
  40. /// the TextEncoding class.
  41. ///
  42. /// For iterating over the characters in a std::string, see the
  43. /// TextIterator class.
  44. {
  45. public:
  46. TextBufferIterator();
  47. /// Creates an uninitialized TextBufferIterator.
  48. TextBufferIterator(const char* begin, const TextEncoding& encoding);
  49. /// Creates a TextBufferIterator for the given buffer, which must be 0-terminated.
  50. /// The encoding object must not be deleted as long as the iterator
  51. /// is in use.
  52. TextBufferIterator(const char* begin, std::size_t size, const TextEncoding& encoding);
  53. /// Creates a TextBufferIterator for the given buffer with the given size.
  54. /// The encoding object must not be deleted as long as the iterator
  55. /// is in use.
  56. TextBufferIterator(const char* begin, const char* end, const TextEncoding& encoding);
  57. /// Creates a TextBufferIterator for the given range.
  58. /// The encoding object must not be deleted as long as the iterator
  59. /// is in use.
  60. TextBufferIterator(const char* end);
  61. /// Creates an end TextBufferIterator for the given buffer.
  62. ~TextBufferIterator();
  63. /// Destroys the TextBufferIterator.
  64. TextBufferIterator(const TextBufferIterator& it);
  65. /// Copy constructor.
  66. TextBufferIterator& operator = (const TextBufferIterator& it);
  67. /// Assignment operator.
  68. void swap(TextBufferIterator& it);
  69. /// Swaps the iterator with another one.
  70. int operator * () const;
  71. /// Returns the Unicode value of the current character.
  72. /// If there is no valid character at the current position,
  73. /// -1 is returned.
  74. TextBufferIterator& operator ++ ();
  75. /// Prefix increment operator.
  76. TextBufferIterator operator ++ (int);
  77. /// Postfix increment operator.
  78. bool operator == (const TextBufferIterator& it) const;
  79. /// Compares two iterators for equality.
  80. bool operator != (const TextBufferIterator& it) const;
  81. /// Compares two iterators for inequality.
  82. TextBufferIterator end() const;
  83. /// Returns the end iterator for the range handled
  84. /// by the iterator.
  85. private:
  86. const TextEncoding* _pEncoding;
  87. const char* _it;
  88. const char* _end;
  89. };
  90. //
  91. // inlines
  92. //
  93. inline bool TextBufferIterator::operator == (const TextBufferIterator& it) const
  94. {
  95. return _it == it._it;
  96. }
  97. inline bool TextBufferIterator::operator != (const TextBufferIterator& it) const
  98. {
  99. return _it != it._it;
  100. }
  101. inline void swap(TextBufferIterator& it1, TextBufferIterator& it2)
  102. {
  103. it1.swap(it2);
  104. }
  105. inline TextBufferIterator TextBufferIterator::end() const
  106. {
  107. return TextBufferIterator(_end);
  108. }
  109. } // namespace Poco
  110. #endif // Foundation_TextBufferIterator_INCLUDED