TextBufferIterator.cpp 2.9 KB

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