1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Novell code.
16 : *
17 : * The Initial Developer of the Original Code is Novell.
18 : * Portions created by the Initial Developer are Copyright (C) 2006
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Robert O'Callahan <robert@ocallahan.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "gfxSkipChars.h"
39 :
40 : #include <stdlib.h>
41 :
42 : #define SHORTCUT_FREQUENCY 256
43 :
44 : // Even numbered list entries are "keep" entries
45 : static bool
46 0 : IsKeepEntry(PRUint32 aEntry)
47 : {
48 0 : return !(aEntry & 1);
49 : }
50 :
51 : void
52 0 : gfxSkipChars::BuildShortcuts()
53 : {
54 0 : if (!mList || mCharCount < SHORTCUT_FREQUENCY)
55 0 : return;
56 :
57 0 : mShortcuts = new Shortcut[mCharCount/SHORTCUT_FREQUENCY];
58 0 : if (!mShortcuts)
59 0 : return;
60 :
61 : PRUint32 i;
62 0 : PRUint32 nextShortcutIndex = 0;
63 0 : PRUint32 originalCharOffset = 0;
64 0 : PRUint32 skippedCharOffset = 0;
65 0 : for (i = 0; i < mListLength; ++i) {
66 0 : PRUint8 len = mList[i];
67 :
68 : // We use >= here to ensure that when mCharCount is a multiple of
69 : // SHORTCUT_FREQUENCY, we fill in the final shortcut with a reference
70 : // to the last element of mList. This means that in general when a list
71 : // element ends on an offset that's a multiple of SHORTCUT_FREQUENCY,
72 : // that list element is the shortcut for that offset, which is
73 : // slightly suboptimal (the *next* element is the one we really want),
74 : // but it's all correct and simpler this way.
75 0 : while (originalCharOffset + len >= (nextShortcutIndex + 1)*SHORTCUT_FREQUENCY) {
76 0 : mShortcuts[nextShortcutIndex] =
77 0 : Shortcut(i, originalCharOffset, skippedCharOffset);
78 0 : ++nextShortcutIndex;
79 : }
80 :
81 0 : originalCharOffset += len;
82 0 : if (IsKeepEntry(i)) {
83 0 : skippedCharOffset += len;
84 : }
85 : }
86 : }
87 :
88 : void
89 0 : gfxSkipCharsIterator::SetOffsets(PRUint32 aOffset, bool aInOriginalString)
90 : {
91 0 : NS_ASSERTION(aOffset <= mSkipChars->mCharCount,
92 : "Invalid offset");
93 :
94 0 : if (mSkipChars->mListLength == 0) {
95 0 : mOriginalStringOffset = mSkippedStringOffset = aOffset;
96 0 : return;
97 : }
98 :
99 0 : if (aOffset == 0) {
100 : // Start from the beginning of the string.
101 0 : mSkippedStringOffset = 0;
102 0 : mOriginalStringOffset = 0;
103 0 : mListPrefixLength = 0;
104 0 : mListPrefixKeepCharCount = 0;
105 0 : mListPrefixCharCount = 0;
106 0 : if (aInOriginalString) {
107 : // Nothing more to do!
108 0 : return;
109 : }
110 : }
111 :
112 0 : if (aInOriginalString && mSkipChars->mShortcuts &&
113 0 : abs(PRInt32(aOffset) - PRInt32(mListPrefixCharCount)) > SHORTCUT_FREQUENCY) {
114 : // Take a shortcut. This makes SetOffsets(..., true) O(1) by bounding
115 : // the iterations in the loop below to at most SHORTCUT_FREQUENCY iterations
116 0 : PRUint32 shortcutIndex = aOffset/SHORTCUT_FREQUENCY;
117 0 : if (shortcutIndex == 0) {
118 0 : mListPrefixLength = 0;
119 0 : mListPrefixKeepCharCount = 0;
120 0 : mListPrefixCharCount = 0;
121 : } else {
122 0 : const gfxSkipChars::Shortcut& shortcut = mSkipChars->mShortcuts[shortcutIndex - 1];
123 0 : mListPrefixLength = shortcut.mListPrefixLength;
124 0 : mListPrefixKeepCharCount = shortcut.mListPrefixKeepCharCount;
125 0 : mListPrefixCharCount = shortcut.mListPrefixCharCount;
126 : }
127 : }
128 :
129 0 : PRInt32 currentRunLength = mSkipChars->mList[mListPrefixLength];
130 0 : for (;;) {
131 : // See if aOffset is in the string segment described by
132 : // mSkipChars->mList[mListPrefixLength]
133 0 : PRUint32 segmentOffset = aInOriginalString ? mListPrefixCharCount : mListPrefixKeepCharCount;
134 0 : if ((aInOriginalString || IsKeepEntry(mListPrefixLength)) &&
135 : aOffset >= segmentOffset && aOffset < segmentOffset + currentRunLength) {
136 0 : PRInt32 offsetInSegment = aOffset - segmentOffset;
137 0 : mOriginalStringOffset = mListPrefixCharCount + offsetInSegment;
138 0 : mSkippedStringOffset = mListPrefixKeepCharCount;
139 0 : if (IsKeepEntry(mListPrefixLength)) {
140 0 : mSkippedStringOffset += offsetInSegment;
141 : }
142 0 : return;
143 : }
144 :
145 0 : if (aOffset < segmentOffset) {
146 : // We need to move backwards
147 0 : if (mListPrefixLength <= 0) {
148 : // nowhere to go backwards
149 0 : mOriginalStringOffset = mSkippedStringOffset = 0;
150 0 : return;
151 : }
152 : // Go backwards one segment and restore invariants
153 0 : --mListPrefixLength;
154 0 : currentRunLength = mSkipChars->mList[mListPrefixLength];
155 0 : mListPrefixCharCount -= currentRunLength;
156 0 : if (IsKeepEntry(mListPrefixLength)) {
157 0 : mListPrefixKeepCharCount -= currentRunLength;
158 : }
159 : } else {
160 : // We need to move forwards
161 0 : if (mListPrefixLength >= mSkipChars->mListLength - 1) {
162 : // nowhere to go forwards
163 0 : mOriginalStringOffset = mListPrefixCharCount + currentRunLength;
164 0 : mSkippedStringOffset = mListPrefixKeepCharCount;
165 0 : if (IsKeepEntry(mListPrefixLength)) {
166 0 : mSkippedStringOffset += currentRunLength;
167 : }
168 0 : return;
169 : }
170 : // Go forwards one segment and restore invariants
171 0 : mListPrefixCharCount += currentRunLength;
172 0 : if (IsKeepEntry(mListPrefixLength)) {
173 0 : mListPrefixKeepCharCount += currentRunLength;
174 : }
175 0 : ++mListPrefixLength;
176 0 : currentRunLength = mSkipChars->mList[mListPrefixLength];
177 : }
178 : }
179 : }
180 :
181 : bool
182 0 : gfxSkipCharsIterator::IsOriginalCharSkipped(PRInt32* aRunLength) const
183 : {
184 0 : if (mSkipChars->mListLength == 0) {
185 0 : if (aRunLength) {
186 0 : *aRunLength = mSkipChars->mCharCount - mOriginalStringOffset;
187 : }
188 0 : return mSkipChars->mCharCount == PRUint32(mOriginalStringOffset);
189 : }
190 :
191 0 : PRUint32 listPrefixLength = mListPrefixLength;
192 : // figure out which segment we're in
193 0 : PRUint32 currentRunLength = mSkipChars->mList[listPrefixLength];
194 : // Zero-length list entries are possible. Advance until mListPrefixLength
195 : // is pointing to a run with real characters (or we're at the end of the
196 : // string).
197 0 : while (currentRunLength == 0 && listPrefixLength < mSkipChars->mListLength - 1) {
198 0 : ++listPrefixLength;
199 : // This does not break the iterator's invariant because no skipped
200 : // or kept characters are being added
201 0 : currentRunLength = mSkipChars->mList[listPrefixLength];
202 : }
203 0 : NS_ASSERTION(PRUint32(mOriginalStringOffset) >= mListPrefixCharCount,
204 : "Invariant violation");
205 : PRUint32 offsetIntoCurrentRun =
206 0 : PRUint32(mOriginalStringOffset) - mListPrefixCharCount;
207 0 : if (listPrefixLength >= mSkipChars->mListLength - 1 &&
208 : offsetIntoCurrentRun >= currentRunLength) {
209 0 : NS_ASSERTION(listPrefixLength == mSkipChars->mListLength - 1 &&
210 : offsetIntoCurrentRun == currentRunLength,
211 : "Overran end of string");
212 : // We're at the end of the string
213 0 : if (aRunLength) {
214 0 : *aRunLength = 0;
215 : }
216 0 : return true;
217 : }
218 :
219 0 : bool isSkipped = !IsKeepEntry(listPrefixLength);
220 0 : if (aRunLength) {
221 : // Long runs of all-skipped or all-kept characters will be encoded as
222 : // sequences of 255, 0, 255, 0 etc. Compute the maximum run length by skipping
223 : // over zero entries.
224 0 : PRUint32 runLength = currentRunLength - offsetIntoCurrentRun;
225 0 : for (PRUint32 i = listPrefixLength + 2; i < mSkipChars->mListLength; i += 2) {
226 0 : if (mSkipChars->mList[i - 1] != 0)
227 0 : break;
228 0 : runLength += mSkipChars->mList[i];
229 : }
230 0 : *aRunLength = runLength;
231 : }
232 0 : return isSkipped;
233 : }
234 :
235 : void
236 0 : gfxSkipCharsBuilder::FlushRun()
237 : {
238 0 : NS_ASSERTION((mBuffer.Length() & 1) == mRunSkipped,
239 : "out of sync?");
240 : // Fill in buffer entries starting at mBufferLength, as many as necessary
241 0 : PRUint32 charCount = mRunCharCount;
242 0 : for (;;) {
243 0 : PRUint32 chars = NS_MIN<PRUint32>(255, charCount);
244 0 : if (!mBuffer.AppendElement(chars)) {
245 0 : mInErrorState = true;
246 0 : return;
247 : }
248 0 : charCount -= chars;
249 0 : if (charCount == 0)
250 : break;
251 0 : if (!mBuffer.AppendElement(0)) {
252 0 : mInErrorState = true;
253 0 : return;
254 : }
255 : }
256 :
257 0 : NS_ASSERTION(mCharCount + mRunCharCount >= mCharCount,
258 : "String length overflow");
259 0 : mCharCount += mRunCharCount;
260 0 : mRunCharCount = 0;
261 0 : mRunSkipped = !mRunSkipped;
262 : }
|