SdText.C 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: SdText.C /main/1 1996/07/29 17:03:53 cde-hp $ */
  24. // Copyright (c) 1995 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifdef __GNUG__
  27. #pragma implementation
  28. #endif
  29. #include "splib.h"
  30. #include "SdText.h"
  31. #ifdef SP_NAMESPACE
  32. namespace SP_NAMESPACE {
  33. #endif
  34. SdText::SdText()
  35. : lita_(false)
  36. {
  37. }
  38. SdText::SdText(const Location &loc, Boolean lita)
  39. : lita_(lita)
  40. {
  41. items_.resize(items_.size() + 1);
  42. items_.back().loc = loc;
  43. items_.back().index = 0;
  44. }
  45. void SdText::addChar(SyntaxChar c, const Location &loc)
  46. {
  47. if (items_.size() == 0
  48. || loc.origin().pointer() != items_.back().loc.origin().pointer()
  49. || loc.index() != (items_.back().loc.index()
  50. + (chars_.size() - items_.back().index))) {
  51. items_.resize(items_.size() + 1);
  52. items_.back().loc = loc;
  53. items_.back().index = chars_.size();
  54. }
  55. chars_ += c;
  56. }
  57. void SdText::swap(SdText &to)
  58. {
  59. items_.swap(to.items_);
  60. chars_.swap(to.chars_);
  61. {
  62. Boolean tem = to.lita_;
  63. to.lita_ = lita_;
  64. lita_ = tem;
  65. }
  66. }
  67. Location SdText::endDelimLocation() const
  68. {
  69. Location loc(items_.back().loc);
  70. loc += chars_.size() - items_.back().index;
  71. return loc;
  72. }
  73. SdTextItem::SdTextItem()
  74. : index(0)
  75. {
  76. }
  77. SdTextIter::SdTextIter(const SdText &text)
  78. : ptr_(&text),
  79. itemIndex_(0)
  80. {
  81. }
  82. Boolean SdTextIter::next(const SyntaxChar *&ptr, size_t &length, Location &loc)
  83. {
  84. const Vector<SdTextItem> &items = ptr_->items_;
  85. if (itemIndex_ >= items.size())
  86. return 0;
  87. loc = items[itemIndex_].loc;
  88. const String<SyntaxChar> &chars = ptr_->chars_;
  89. size_t charsIndex = items[itemIndex_].index;
  90. ptr = chars.data() + charsIndex;
  91. if (itemIndex_ + 1 < items.size())
  92. length = items[itemIndex_ + 1].index - charsIndex;
  93. else
  94. length = chars.size() - charsIndex;
  95. itemIndex_++;
  96. return 1;
  97. }
  98. #ifdef SP_NAMESPACE
  99. }
  100. #endif