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 librararies 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:
254 void Text::swap(Text &to)
256 items_.swap(to.items_);
257 chars_.swap(to.chars_);
260 TextIter::TextIter(const Text &text)
261 : ptr_(text.items_.begin()), text_(&text)
265 const Char *TextIter::chars(size_t &length) const
267 if (ptr_->type == TextItem::ignore) {
272 const StringC &chars = text_->chars_;
273 size_t charsIndex = ptr_->index;
274 if (ptr_ + 1 != text_->items_.begin() + text_->items_.size())
275 length = ptr_[1].index - charsIndex;
277 length = chars.size() - charsIndex;
278 return chars.data() + charsIndex;
282 Boolean TextIter::next(TextItem::Type &type, const Char *&str, size_t &length,
283 const Location *&loc)
285 const TextItem *end = text_->items_.begin() + text_->items_.size();
290 if (type == TextItem::ignore) {
295 const StringC &chars = text_->chars_;
296 size_t charsIndex = ptr_->index;
297 str = chars.data() + charsIndex;
299 length = ptr_[1].index - charsIndex;
301 length = chars.size() - charsIndex;
307 void Text::insertChars(const StringC &s, const Location &loc)
310 items_.resize(items_.size() + 1);
311 for (size_t i = items_.size() - 1; i > 0; i--) {
312 items_[i] = items_[i - 1];
313 items_[i].index += s.size();
316 items_[0].type = TextItem::data;
320 size_t Text::nDataEntities() const
323 for (size_t i = 0; i < items_.size(); i++)
324 switch (items_[i].type) {
325 case TextItem::sdata:
326 case TextItem::cdata:
335 // This is used to determine for a FIXED CDATA attribute
336 // whether a specified value if equal to the default value.
338 Boolean Text::fixedEqual(const Text &text) const
340 if (string() != text.string())
343 for (size_t i = 0; i < items_.size(); i++)
344 switch (items_[i].type) {
345 case TextItem::cdata:
346 case TextItem::sdata:
348 if (j >= text.items_.size())
350 if (text.items_[j].type == TextItem::cdata
351 || text.items_[j].type == TextItem::sdata)
355 if (text.items_[j].index != items_[i].index
356 || (text.items_[j].loc.origin()->asEntityOrigin()->entity()
357 != items_[i].loc.origin()->asEntityOrigin()->entity()))
363 for (; j < text.items_.size(); j++)
364 switch (text.items_[j].type) {
365 case TextItem::cdata:
366 case TextItem::sdata:
374 Location Text::charLocation(size_t ind) const
376 // Find the last item whose index <= ind.
378 // indexes < i implies index <= ind
379 // indexes >= lim implies index > ind
380 // The first item will always have index 0.
382 size_t lim = items_.size();
384 size_t mid = i + (lim - i)/2;
385 if (items_[mid].index > ind)
391 for (size_t i = 1; i < items_.size(); i++)
392 if (items_[i].index > ind)
397 // If items_.size() == 0, then i == lim.
400 loc += ind - items_[i].index;