TextIterator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // TextIterator.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/TextIterator.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Text
  8. // Module: TextIterator
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/TextIterator.h"
  16. #include "Poco/TextEncoding.h"
  17. #include <algorithm>
  18. namespace Poco {
  19. TextIterator::TextIterator():
  20. _pEncoding(0)
  21. {
  22. }
  23. TextIterator::TextIterator(const std::string& str, const TextEncoding& encoding):
  24. _pEncoding(&encoding),
  25. _it(str.begin()),
  26. _end(str.end())
  27. {
  28. }
  29. TextIterator::TextIterator(const std::string::const_iterator& begin, const std::string::const_iterator& end, const TextEncoding& encoding):
  30. _pEncoding(&encoding),
  31. _it(begin),
  32. _end(end)
  33. {
  34. }
  35. TextIterator::TextIterator(const std::string& str):
  36. _pEncoding(0),
  37. _it(str.end()),
  38. _end(str.end())
  39. {
  40. }
  41. TextIterator::TextIterator(const std::string::const_iterator& end):
  42. _pEncoding(0),
  43. _it(end),
  44. _end(end)
  45. {
  46. }
  47. TextIterator::~TextIterator()
  48. {
  49. }
  50. TextIterator::TextIterator(const TextIterator& it):
  51. _pEncoding(it._pEncoding),
  52. _it(it._it),
  53. _end(it._end)
  54. {
  55. }
  56. TextIterator& TextIterator::operator = (const TextIterator& it)
  57. {
  58. if (&it != this)
  59. {
  60. _pEncoding = it._pEncoding;
  61. _it = it._it;
  62. _end = it._end;
  63. }
  64. return *this;
  65. }
  66. void TextIterator::swap(TextIterator& it)
  67. {
  68. std::swap(_pEncoding, it._pEncoding);
  69. std::swap(_it, it._it);
  70. std::swap(_end, it._end);
  71. }
  72. int TextIterator::operator * () const
  73. {
  74. poco_check_ptr (_pEncoding);
  75. poco_assert (_it != _end);
  76. std::string::const_iterator it = _it;
  77. unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
  78. unsigned char* p = buffer;
  79. if (it != _end)
  80. *p++ = *it++;
  81. else
  82. *p++ = 0;
  83. int read = 1;
  84. int n = _pEncoding->queryConvert(buffer, 1);
  85. while (-1 > n && (_end - it) >= -n - read)
  86. {
  87. while (read < -n && it != _end)
  88. {
  89. *p++ = *it++;
  90. read++;
  91. }
  92. n = _pEncoding->queryConvert(buffer, read);
  93. }
  94. if (-1 > n)
  95. {
  96. return -1;
  97. }
  98. else
  99. {
  100. return n;
  101. }
  102. }
  103. TextIterator& TextIterator::operator ++ ()
  104. {
  105. poco_check_ptr (_pEncoding);
  106. poco_assert (_it != _end);
  107. unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
  108. unsigned char* p = buffer;
  109. if (_it != _end)
  110. *p++ = *_it++;
  111. else
  112. *p++ = 0;
  113. int read = 1;
  114. int n = _pEncoding->sequenceLength(buffer, 1);
  115. while (-1 > n && (_end - _it) >= -n - read)
  116. {
  117. while (read < -n && _it != _end)
  118. {
  119. *p++ = *_it++;
  120. read++;
  121. }
  122. n = _pEncoding->sequenceLength(buffer, read);
  123. }
  124. while (read < n && _it != _end)
  125. {
  126. _it++;
  127. read++;
  128. }
  129. return *this;
  130. }
  131. TextIterator TextIterator::operator ++ (int)
  132. {
  133. TextIterator prev(*this);
  134. operator ++ ();
  135. return prev;
  136. }
  137. } // namespace Poco