1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsTArray.h"
8 #include "nsASCIIMask.h"
9 #include "mozilla/CheckedInt.h"
10 
11 /**
12  * nsTString::Find
13  *
14  * aOffset specifies starting index
15  * aCount specifies number of string compares (iterations)
16  */
17 template <typename T>
Find(const nsTString<char> & aString,bool aIgnoreCase,int32_t aOffset,int32_t aCount) const18 int32_t nsTString<T>::Find(const nsTString<char>& aString, bool aIgnoreCase,
19                            int32_t aOffset, int32_t aCount) const {
20   // this method changes the meaning of aOffset and aCount:
21   Find_ComputeSearchRange(this->mLength, aString.Length(), aOffset, aCount);
22 
23   int32_t result = FindSubstring(this->mData + aOffset, aCount, aString.get(),
24                                  aString.Length(), aIgnoreCase);
25   if (result != kNotFound) result += aOffset;
26   return result;
27 }
28 
29 template <typename T>
Find(const char * aString,bool aIgnoreCase,int32_t aOffset,int32_t aCount) const30 int32_t nsTString<T>::Find(const char* aString, bool aIgnoreCase,
31                            int32_t aOffset, int32_t aCount) const {
32   return Find(nsTDependentString<char>(aString), aIgnoreCase, aOffset, aCount);
33 }
34 
35 /**
36  * nsTString::RFind
37  *
38  * aOffset specifies starting index
39  * aCount specifies number of string compares (iterations)
40  */
41 template <typename T>
RFind(const nsTString<char> & aString,bool aIgnoreCase,int32_t aOffset,int32_t aCount) const42 int32_t nsTString<T>::RFind(const nsTString<char>& aString, bool aIgnoreCase,
43                             int32_t aOffset, int32_t aCount) const {
44   // this method changes the meaning of aOffset and aCount:
45   RFind_ComputeSearchRange(this->mLength, aString.Length(), aOffset, aCount);
46 
47   int32_t result = RFindSubstring(this->mData + aOffset, aCount, aString.get(),
48                                   aString.Length(), aIgnoreCase);
49   if (result != kNotFound) result += aOffset;
50   return result;
51 }
52 
53 template <typename T>
RFind(const char * aString,bool aIgnoreCase,int32_t aOffset,int32_t aCount) const54 int32_t nsTString<T>::RFind(const char* aString, bool aIgnoreCase,
55                             int32_t aOffset, int32_t aCount) const {
56   return RFind(nsTDependentString<char>(aString), aIgnoreCase, aOffset, aCount);
57 }
58 
59 /**
60  * nsTString::RFindChar
61  */
62 template <typename T>
RFindChar(char16_t aChar,int32_t aOffset,int32_t aCount) const63 int32_t nsTString<T>::RFindChar(char16_t aChar, int32_t aOffset,
64                                 int32_t aCount) const {
65   return nsBufferRoutines<T>::rfind_char(this->mData, this->mLength, aOffset,
66                                          aChar, aCount);
67 }
68 
69 /**
70  * nsTString::FindCharInSet
71  */
72 
73 template <typename T>
FindCharInSet(const char_type * aSet,int32_t aOffset) const74 int32_t nsTString<T>::FindCharInSet(const char_type* aSet,
75                                     int32_t aOffset) const {
76   if (aOffset < 0)
77     aOffset = 0;
78   else if (aOffset >= int32_t(this->mLength))
79     return kNotFound;
80 
81   int32_t result =
82       ::FindCharInSet(this->mData + aOffset, this->mLength - aOffset, aSet);
83   if (result != kNotFound) result += aOffset;
84   return result;
85 }
86 
87 /**
88  * nsTString::RFindCharInSet
89  */
90 
91 template <typename T>
RFindCharInSet(const char_type * aSet,int32_t aOffset) const92 int32_t nsTString<T>::RFindCharInSet(const char_type* aSet,
93                                      int32_t aOffset) const {
94   // We want to pass a "data length" to ::RFindCharInSet
95   if (aOffset < 0 || aOffset > int32_t(this->mLength))
96     aOffset = this->mLength;
97   else
98     ++aOffset;
99 
100   return ::RFindCharInSet(this->mData, aOffset, aSet);
101 }
102 
103 /**
104  * nsTString::Mid
105  */
106 
107 template <typename T>
Mid(self_type & aResult,index_type aStartPos,size_type aLengthToCopy) const108 typename nsTString<T>::size_type nsTString<T>::Mid(
109     self_type& aResult, index_type aStartPos, size_type aLengthToCopy) const {
110   if (aStartPos == 0 && aLengthToCopy >= this->mLength)
111     aResult = *this;
112   else
113     aResult = Substring(*this, aStartPos, aLengthToCopy);
114 
115   return aResult.mLength;
116 }
117 
118 /**
119  * nsTString::SetCharAt
120  */
121 
122 template <typename T>
SetCharAt(char16_t aChar,index_type aIndex)123 bool nsTString<T>::SetCharAt(char16_t aChar, index_type aIndex) {
124   if (aIndex >= this->mLength) return false;
125 
126   if (!this->EnsureMutable()) this->AllocFailed(this->mLength);
127 
128   this->mData[aIndex] = char_type(aChar);
129   return true;
130 }
131 
132 /**
133  * nsTString::StripChars,StripChar,StripWhitespace
134  */
135 
136 template <typename T>
137 template <typename Q, typename EnableIfChar16>
StripChars(const incompatible_char_type * aSet)138 void nsTString<T>::StripChars(const incompatible_char_type* aSet) {
139   if (!StripChars(aSet, mozilla::fallible)) {
140     this->AllocFailed(this->mLength);
141   }
142 }
143 
144 template void nsTString<char16_t>::StripChars(const incompatible_char_type*);
145 
146 template <typename T>
147 template <typename Q, typename EnableIfChar16>
StripChars(const incompatible_char_type * aSet,const fallible_t &)148 bool nsTString<T>::StripChars(const incompatible_char_type* aSet,
149                               const fallible_t&) {
150   if (!this->EnsureMutable()) {
151     return false;
152   }
153 
154   this->mLength =
155       nsBufferRoutines<T>::strip_chars(this->mData, this->mLength, aSet);
156   return true;
157 }
158 
159 template bool nsTString<char16_t>::StripChars(const incompatible_char_type*,
160                                               const fallible_t&);
161 
162 template <typename T>
StripChars(const char_type * aSet)163 void nsTString<T>::StripChars(const char_type* aSet) {
164   nsTSubstring<T>::StripChars(aSet);
165 }
166 
167 template <typename T>
StripWhitespace()168 void nsTString<T>::StripWhitespace() {
169   if (!StripWhitespace(mozilla::fallible)) {
170     this->AllocFailed(this->mLength);
171   }
172 }
173 
174 template <typename T>
StripWhitespace(const fallible_t &)175 bool nsTString<T>::StripWhitespace(const fallible_t&) {
176   if (!this->EnsureMutable()) {
177     return false;
178   }
179 
180   this->StripTaggedASCII(mozilla::ASCIIMask::MaskWhitespace());
181   return true;
182 }
183 
184 /**
185  * nsTString::ReplaceChar,ReplaceSubstring
186  */
187 
188 template <typename T>
ReplaceChar(char_type aOldChar,char_type aNewChar)189 void nsTString<T>::ReplaceChar(char_type aOldChar, char_type aNewChar) {
190   if (!this->EnsureMutable())  // XXX do this lazily?
191     this->AllocFailed(this->mLength);
192 
193   for (uint32_t i = 0; i < this->mLength; ++i) {
194     if (this->mData[i] == aOldChar) this->mData[i] = aNewChar;
195   }
196 }
197 
198 template <typename T>
ReplaceChar(const char_type * aSet,char_type aNewChar)199 void nsTString<T>::ReplaceChar(const char_type* aSet, char_type aNewChar) {
200   if (!this->EnsureMutable())  // XXX do this lazily?
201     this->AllocFailed(this->mLength);
202 
203   char_type* data = this->mData;
204   uint32_t lenRemaining = this->mLength;
205 
206   while (lenRemaining) {
207     int32_t i = ::FindCharInSet(data, lenRemaining, aSet);
208     if (i == kNotFound) break;
209 
210     data[i++] = aNewChar;
211     data += i;
212     lenRemaining -= i;
213   }
214 }
215 
216 template void nsTString<char16_t>::ReplaceChar(const char*, char16_t);
217 
218 void ReleaseData(void* aData, nsAString::DataFlags aFlags);
219 
220 template <typename T>
ReplaceSubstring(const char_type * aTarget,const char_type * aNewValue)221 void nsTString<T>::ReplaceSubstring(const char_type* aTarget,
222                                     const char_type* aNewValue) {
223   ReplaceSubstring(nsTDependentString<T>(aTarget),
224                    nsTDependentString<T>(aNewValue));
225 }
226 
227 template <typename T>
ReplaceSubstring(const char_type * aTarget,const char_type * aNewValue,const fallible_t & aFallible)228 bool nsTString<T>::ReplaceSubstring(const char_type* aTarget,
229                                     const char_type* aNewValue,
230                                     const fallible_t& aFallible) {
231   return ReplaceSubstring(nsTDependentString<T>(aTarget),
232                           nsTDependentString<T>(aNewValue), aFallible);
233 }
234 
235 template <typename T>
ReplaceSubstring(const self_type & aTarget,const self_type & aNewValue)236 void nsTString<T>::ReplaceSubstring(const self_type& aTarget,
237                                     const self_type& aNewValue) {
238   if (!ReplaceSubstring(aTarget, aNewValue, mozilla::fallible)) {
239     // Note that this may wildly underestimate the allocation that failed, as
240     // we could have been replacing multiple copies of aTarget.
241     this->AllocFailed(this->mLength + (aNewValue.Length() - aTarget.Length()));
242   }
243 }
244 
245 template <typename T>
ReplaceSubstring(const self_type & aTarget,const self_type & aNewValue,const fallible_t &)246 bool nsTString<T>::ReplaceSubstring(const self_type& aTarget,
247                                     const self_type& aNewValue,
248                                     const fallible_t&) {
249   if (aTarget.Length() == 0) return true;
250 
251   // Remember all of the non-matching parts.
252   AutoTArray<Segment, 16> nonMatching;
253   uint32_t i = 0;
254   mozilla::CheckedUint32 newLength;
255   while (true) {
256     int32_t r = FindSubstring(this->mData + i, this->mLength - i,
257                               static_cast<const char_type*>(aTarget.Data()),
258                               aTarget.Length(), false);
259     int32_t until = (r == kNotFound) ? this->mLength - i : r;
260     nonMatching.AppendElement(Segment(i, until));
261     newLength += until;
262     if (r == kNotFound) {
263       break;
264     }
265 
266     newLength += aNewValue.Length();
267     i += r + aTarget.Length();
268     if (i >= this->mLength) {
269       // Add an auxiliary entry at the end of the list to help as an edge case
270       // for the algorithms below.
271       nonMatching.AppendElement(Segment(this->mLength, 0));
272       break;
273     }
274   }
275 
276   if (!newLength.isValid()) {
277     return false;
278   }
279 
280   // If there's only one non-matching segment, then the target string was not
281   // found, and there's nothing to do.
282   if (nonMatching.Length() == 1) {
283     MOZ_ASSERT(
284         nonMatching[0].mBegin == 0 && nonMatching[0].mLength == this->mLength,
285         "We should have the correct non-matching segment.");
286     return true;
287   }
288 
289   // Make sure that we can mutate our buffer.
290   // Note that we always allocate at least an this->mLength sized buffer,
291   // because the rest of the algorithm relies on having access to all of the
292   // original string.  In other words, we over-allocate in the shrinking case.
293   uint32_t oldLen = this->mLength;
294   auto r =
295       this->StartBulkWriteImpl(XPCOM_MAX(oldLen, newLength.value()), oldLen);
296   if (r.isErr()) {
297     return false;
298   }
299 
300   if (aTarget.Length() >= aNewValue.Length()) {
301     // In the shrinking case, start filling the buffer from the beginning.
302     const uint32_t delta = (aTarget.Length() - aNewValue.Length());
303     for (i = 1; i < nonMatching.Length(); ++i) {
304       // When we move the i'th non-matching segment into position, we need to
305       // account for the characters deleted by the previous |i| replacements by
306       // subtracting |i * delta|.
307       const char_type* sourceSegmentPtr = this->mData + nonMatching[i].mBegin;
308       char_type* destinationSegmentPtr =
309           this->mData + nonMatching[i].mBegin - i * delta;
310       // Write the i'th replacement immediately before the new i'th non-matching
311       // segment.
312       char_traits::copy(destinationSegmentPtr - aNewValue.Length(),
313                         aNewValue.Data(), aNewValue.Length());
314       char_traits::move(destinationSegmentPtr, sourceSegmentPtr,
315                         nonMatching[i].mLength);
316     }
317   } else {
318     // In the growing case, start filling the buffer from the end.
319     const uint32_t delta = (aNewValue.Length() - aTarget.Length());
320     for (i = nonMatching.Length() - 1; i > 0; --i) {
321       // When we move the i'th non-matching segment into position, we need to
322       // account for the characters added by the previous |i| replacements by
323       // adding |i * delta|.
324       const char_type* sourceSegmentPtr = this->mData + nonMatching[i].mBegin;
325       char_type* destinationSegmentPtr =
326           this->mData + nonMatching[i].mBegin + i * delta;
327       char_traits::move(destinationSegmentPtr, sourceSegmentPtr,
328                         nonMatching[i].mLength);
329       // Write the i'th replacement immediately before the new i'th non-matching
330       // segment.
331       char_traits::copy(destinationSegmentPtr - aNewValue.Length(),
332                         aNewValue.Data(), aNewValue.Length());
333     }
334   }
335 
336   // Adjust the length and make sure the string is null terminated.
337   this->FinishBulkWriteImpl(newLength.value());
338 
339   return true;
340 }
341 
342 /**
343  * nsTString::Trim
344  */
345 
346 template <typename T>
Trim(const char * aSet,bool aTrimLeading,bool aTrimTrailing,bool aIgnoreQuotes)347 void nsTString<T>::Trim(const char* aSet, bool aTrimLeading, bool aTrimTrailing,
348                         bool aIgnoreQuotes) {
349   // the old implementation worried about aSet being null :-/
350   if (!aSet) return;
351 
352   char_type* start = this->mData;
353   char_type* end = this->mData + this->mLength;
354 
355   // skip over quotes if requested
356   if (aIgnoreQuotes && this->mLength > 2 &&
357       this->mData[0] == this->mData[this->mLength - 1] &&
358       (this->mData[0] == '\'' || this->mData[0] == '"')) {
359     ++start;
360     --end;
361   }
362 
363   uint32_t setLen = nsCharTraits<char>::length(aSet);
364 
365   if (aTrimLeading) {
366     uint32_t cutStart = start - this->mData;
367     uint32_t cutLength = 0;
368 
369     // walk forward from start to end
370     for (; start != end; ++start, ++cutLength) {
371       int32_t pos = FindChar1(aSet, setLen, 0, *start, setLen);
372       if (pos == kNotFound) break;
373     }
374 
375     if (cutLength) {
376       this->Cut(cutStart, cutLength);
377 
378       // reset iterators
379       start = this->mData + cutStart;
380       end = this->mData + this->mLength - cutStart;
381     }
382   }
383 
384   if (aTrimTrailing) {
385     uint32_t cutEnd = end - this->mData;
386     uint32_t cutLength = 0;
387 
388     // walk backward from end to start
389     --end;
390     for (; end >= start; --end, ++cutLength) {
391       int32_t pos = FindChar1(aSet, setLen, 0, *end, setLen);
392       if (pos == kNotFound) break;
393     }
394 
395     if (cutLength) this->Cut(cutEnd - cutLength, cutLength);
396   }
397 }
398 
399 /**
400  * nsTString::CompressWhitespace.
401  */
402 
403 template <typename T>
CompressWhitespace(bool aTrimLeading,bool aTrimTrailing)404 void nsTString<T>::CompressWhitespace(bool aTrimLeading, bool aTrimTrailing) {
405   // Quick exit
406   if (this->mLength == 0) {
407     return;
408   }
409 
410   if (!this->EnsureMutable()) this->AllocFailed(this->mLength);
411 
412   const ASCIIMaskArray& mask = mozilla::ASCIIMask::MaskWhitespace();
413 
414   char_type* to = this->mData;
415   char_type* from = this->mData;
416   char_type* end = this->mData + this->mLength;
417 
418   // Compresses runs of whitespace down to a normal space ' ' and convert
419   // any whitespace to a normal space.  This assumes that whitespace is
420   // all standard 7-bit ASCII.
421   bool skipWS = aTrimLeading;
422   while (from < end) {
423     uint32_t theChar = *from++;
424     if (mozilla::ASCIIMask::IsMasked(mask, theChar)) {
425       if (!skipWS) {
426         *to++ = ' ';
427         skipWS = true;
428       }
429     } else {
430       *to++ = theChar;
431       skipWS = false;
432     }
433   }
434 
435   // If we need to trim the trailing whitespace, back up one character.
436   if (aTrimTrailing && skipWS && to > this->mData) {
437     to--;
438   }
439 
440   *to = char_type(0);  // add the null
441   this->mLength = to - this->mData;
442 }
443