2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: Text.C /main/1 1996/07/29 17:06:09 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
28 #pragma implementation
37 namespace SP_NAMESPACE {
44 void Text::addChar(Char c, const Location &loc)
46 if (items_.size() == 0
47 || items_.back().type != TextItem::data
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().type = TextItem::data;
54 items_.back().index = chars_.size();
59 void Text::addChars(const Char *p, size_t length, const Location &loc)
61 if (items_.size() == 0
62 || items_.back().type != TextItem::data
63 || loc.origin().pointer() != items_.back().loc.origin().pointer()
64 || loc.index() != (items_.back().loc.index()
65 + (chars_.size() - items_.back().index))) {
66 items_.resize(items_.size() + 1);
67 items_.back().loc = loc;
68 items_.back().type = TextItem::data;
69 items_.back().index = chars_.size();
71 chars_.append(p, length);
74 void Text::addCdata(const InternalEntity *entity,
75 const ConstPtr<Origin> &origin)
77 addSimple(TextItem::cdata, Location(origin, 0));
78 chars_.append(entity->string().data(), entity->string().size());
81 void Text::addSdata(const InternalEntity *entity,
82 const ConstPtr<Origin> &origin)
84 addSimple(TextItem::sdata, Location(origin, 0));
85 chars_.append(entity->string().data(), entity->string().size());
88 void Text::addCharsTokenize(const Char *str, size_t n, const Location &loc,
92 // FIXME speed this up
93 for (size_t i = 0; i < n; loci += 1, i++) {
94 if (str[i] == space && (size() == 0 || lastChar() == space))
95 ignoreChar(str[i], loci);
97 addChar(str[i], loci);
101 void Text::tokenize(Char space, Text &text) const
103 TextIter iter(*this);
108 while (iter.next(type, p, n, loc)) {
111 text.addCharsTokenize(p, n, *loc, space);
113 case TextItem::sdata:
114 case TextItem::cdata:
116 text.addEntityStart(*loc);
117 text.addCharsTokenize(p, n, *loc, space);
120 text.addEntityEnd(tem);
123 case TextItem::ignore:
124 text.ignoreChar(*p, *loc);
127 text.addSimple(type, *loc);
131 if (text.size() > 0 && text.lastChar() == space)
132 text.ignoreLastChar();
135 void Text::addSimple(TextItem::Type type, const Location &loc)
137 items_.resize(items_.size() + 1);
138 items_.back().loc = loc;
139 items_.back().type = type;
140 items_.back().index = chars_.size();
143 void Text::ignoreChar(Char c, const Location &loc)
145 items_.resize(items_.size() + 1);
146 items_.back().loc = loc;
147 items_.back().type = TextItem::ignore;
149 items_.back().index = chars_.size();
152 void Text::ignoreLastChar()
154 size_t lastIndex = chars_.size() - 1;
156 for (i = items_.size() - 1; items_[i].index > lastIndex; i--)
158 // lastIndex >= items_[i].index
159 if (items_[i].index != lastIndex) {
160 items_.resize(items_.size() + 1);
162 for (size_t j = items_.size() - 1; j > i; j--)
163 items_[j] = items_[j - 1];
164 items_[i].index = lastIndex;
165 items_[i].loc = items_[i - 1].loc;
166 items_[i].loc += lastIndex - items_[i - 1].index;
169 items_[i].c = chars_[chars_.size() - 1];
170 items_[i].type = TextItem::ignore;
171 for (size_t j = i + 1; j < items_.size(); j++)
172 items_[j].index = lastIndex;
173 chars_.resize(chars_.size() - 1);
176 // All characters other than spaces are substed.
178 void Text::subst(const SubstTable<Char> &table, Char space)
180 for (size_t i = 0; i < items_.size(); i++)
181 if (items_[i].type == TextItem::data) {
182 size_t lim = (i + 1 < items_.size()
183 ? items_[i + 1].index
186 for (j = items_[i].index; j < lim; j++) {
188 if (c != space && c != table[c])
192 size_t start = items_[i].index;
193 StringC origChars(chars_.data() + start, lim - start);
195 if (chars_[j] != space)
196 table.subst(chars_[j]);
197 items_[i].loc = Location(new MultiReplacementOrigin(items_[i].loc,
210 Boolean Text::startDelimLocation(Location &loc) const
212 if (items_.size() == 0 || items_[0].type != TextItem::startDelim)
218 Boolean Text::endDelimLocation(Location &loc) const
220 if (items_.size() == 0)
222 switch (items_.back().type) {
223 case TextItem::endDelim:
224 case TextItem::endDelimA:
229 loc = items_.back().loc;
233 Boolean Text::delimType(Boolean &lita) const
235 if (items_.size() == 0)
237 switch (items_.back().type) {
238 case TextItem::endDelim:
241 case TextItem::endDelimA:
251 : type(data), c(0), index(0)
255 void Text::swap(Text &to)
257 items_.swap(to.items_);
258 chars_.swap(to.chars_);
261 TextIter::TextIter(const Text &text)
262 : ptr_(text.items_.begin()), text_(&text)
266 const Char *TextIter::chars(size_t &length) const
268 if (ptr_->type == TextItem::ignore) {
273 const StringC &chars = text_->chars_;
274 size_t charsIndex = ptr_->index;
275 if (ptr_ + 1 != text_->items_.begin() + text_->items_.size())
276 length = ptr_[1].index - charsIndex;
278 length = chars.size() - charsIndex;
279 return chars.data() + charsIndex;
283 Boolean TextIter::next(TextItem::Type &type, const Char *&str, size_t &length,
284 const Location *&loc)
286 const TextItem *end = text_->items_.begin() + text_->items_.size();
291 if (type == TextItem::ignore) {
296 const StringC &chars = text_->chars_;
297 size_t charsIndex = ptr_->index;
298 str = chars.data() + charsIndex;
300 length = ptr_[1].index - charsIndex;
302 length = chars.size() - charsIndex;
308 void Text::insertChars(const StringC &s, const Location &loc)
311 items_.resize(items_.size() + 1);
312 for (size_t i = items_.size() - 1; i > 0; i--) {
313 items_[i] = items_[i - 1];
314 items_[i].index += s.size();
317 items_[0].type = TextItem::data;
321 size_t Text::nDataEntities() const
324 for (size_t i = 0; i < items_.size(); i++)
325 switch (items_[i].type) {
326 case TextItem::sdata:
327 case TextItem::cdata:
336 // This is used to determine for a FIXED CDATA attribute
337 // whether a specified value if equal to the default value.
339 Boolean Text::fixedEqual(const Text &text) const
341 if (string() != text.string())
344 for (size_t i = 0; i < items_.size(); i++)
345 switch (items_[i].type) {
346 case TextItem::cdata:
347 case TextItem::sdata:
349 if (j >= text.items_.size())
351 if (text.items_[j].type == TextItem::cdata
352 || text.items_[j].type == TextItem::sdata)
356 if (text.items_[j].index != items_[i].index
357 || (text.items_[j].loc.origin()->asEntityOrigin()->entity()
358 != items_[i].loc.origin()->asEntityOrigin()->entity()))
364 for (; j < text.items_.size(); j++)
365 switch (text.items_[j].type) {
366 case TextItem::cdata:
367 case TextItem::sdata:
375 Location Text::charLocation(size_t ind) const
377 // Find the last item whose index <= ind.
379 // indexes < i implies index <= ind
380 // indexes >= lim implies index > ind
381 // The first item will always have index 0.
383 size_t lim = items_.size();
385 size_t mid = i + (lim - i)/2;
386 if (items_[mid].index > ind)
392 for (size_t i = 1; i < items_.size(); i++)
393 if (items_[i].index > ind)
398 // If items_.size() == 0, then i == lim.
401 loc += ind - items_[i].index;