TextIterator.h 3.4 KB

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