000754a227638e0c467846f73059ea1590c2a940
[oweals/cde.git] / cde / programs / nsgmls / SdText.C
1 /* $XConsortium: SdText.C /main/1 1996/07/29 17:03:53 cde-hp $ */
2 // Copyright (c) 1995 James Clark
3 // See the file COPYING for copying permission.
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8 #include "splib.h"
9 #include "SdText.h"
10
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14
15 SdText::SdText()
16 {
17 }
18
19 SdText::SdText(const Location &loc, Boolean lita)
20 : lita_(lita)
21 {
22   items_.resize(items_.size() + 1);
23   items_.back().loc = loc;
24   items_.back().index = 0;
25 }
26
27 void SdText::addChar(SyntaxChar c, const Location &loc)
28 {
29   if (items_.size() == 0
30       || loc.origin().pointer() != items_.back().loc.origin().pointer()
31       || loc.index() != (items_.back().loc.index()
32                          + (chars_.size() - items_.back().index))) {
33     items_.resize(items_.size() + 1);
34     items_.back().loc = loc;
35     items_.back().index = chars_.size();
36   }
37   chars_ += c;
38 }
39
40 void SdText::swap(SdText &to)
41 {
42   items_.swap(to.items_);
43   chars_.swap(to.chars_);
44   {
45     Boolean tem = to.lita_;
46     to.lita_ = lita_;
47     lita_ = tem;
48   }
49 }
50
51 Location SdText::endDelimLocation() const
52 {
53   Location loc(items_.back().loc);
54   loc += chars_.size() - items_.back().index;
55   return loc;
56 }
57
58 SdTextItem::SdTextItem()
59 {
60 }
61
62 SdTextIter::SdTextIter(const SdText &text)
63 : ptr_(&text),
64   itemIndex_(0)
65 {
66 }
67
68 Boolean SdTextIter::next(const SyntaxChar *&ptr, size_t &length, Location &loc)
69 {
70   const Vector<SdTextItem> &items = ptr_->items_;
71   if (itemIndex_ >= items.size())
72     return 0;
73   loc = items[itemIndex_].loc;
74   const String<SyntaxChar> &chars = ptr_->chars_;
75   size_t charsIndex = items[itemIndex_].index;
76   ptr = chars.data() + charsIndex;
77   if (itemIndex_ + 1 < items.size())
78     length = items[itemIndex_ + 1].index - charsIndex;
79   else
80     length = chars.size() - charsIndex;
81   itemIndex_++;
82   return 1;
83 }
84
85 #ifdef SP_NAMESPACE
86 }
87 #endif