Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: third_party/lzma/v4_65/files/CPP/Common/MyString.h

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Common/String.h
2
3 #ifndef __COMMON_STRING_H
4 #define __COMMON_STRING_H
5
6 #include <string.h>
7 // #include <wchar.h>
8
9 #include "MyVector.h"
10
11 #ifdef _WIN32
12 #include "MyWindows.h"
13 #endif
14
15 template <class T>
16 inline int MyStringLen(const T *s)
17 {
18 int i;
19 for (i = 0; s[i] != '\0'; i++);
20 return i;
21 }
22
23 template <class T>
24 inline T * MyStringCopy(T *dest, const T *src)
25 {
26 T *destStart = dest;
27 while ((*dest++ = *src++) != 0);
28 return destStart;
29 }
30
31 inline wchar_t* MyStringGetNextCharPointer(wchar_t *p)
32 { return (p + 1); }
33 inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p)
34 { return (p + 1); }
35 inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p)
36 { return (p - 1); }
37 inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p)
38 { return (p - 1); }
39
40 #ifdef _WIN32
41
42 inline char* MyStringGetNextCharPointer(char *p)
43 { return CharNextA(p); }
44 inline const char* MyStringGetNextCharPointer(const char *p)
45 { return CharNextA(p); }
46
47 inline char* MyStringGetPrevCharPointer(char *base, char *p)
48 { return CharPrevA(base, p); }
49 inline const char* MyStringGetPrevCharPointer(const char *base, const char *p)
50 { return CharPrevA(base, p); }
51
52 inline char MyCharUpper(char c)
53 { return (char)(unsigned int)(UINT_PTR)CharUpperA((LPSTR)(UINT_PTR)(unsigned i nt)(unsigned char)c); }
54 #ifdef _UNICODE
55 inline wchar_t MyCharUpper(wchar_t c)
56 { return (wchar_t)(unsigned int)(UINT_PTR)CharUpperW((LPWSTR)(UINT_PTR)(unsign ed int)c); }
57 #else
58 wchar_t MyCharUpper(wchar_t c);
59 #endif
60
61 inline char MyCharLower(char c)
62 { return (char)(unsigned int)(UINT_PTR)CharLowerA((LPSTR)(UINT_PTR)(unsigned i nt)(unsigned char)c); }
63 #ifdef _UNICODE
64 inline wchar_t MyCharLower(wchar_t c)
65 { return (wchar_t)(unsigned int)(UINT_PTR)CharLowerW((LPWSTR)(UINT_PTR)(unsign ed int)c); }
66 #else
67 wchar_t MyCharLower(wchar_t c);
68 #endif
69
70 inline char * MyStringUpper(char *s) { return CharUpperA(s); }
71 #ifdef _UNICODE
72 inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); }
73 #else
74 wchar_t * MyStringUpper(wchar_t *s);
75 #endif
76
77 inline char * MyStringLower(char *s) { return CharLowerA(s); }
78 #ifdef _UNICODE
79 inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); }
80 #else
81 wchar_t * MyStringLower(wchar_t *s);
82 #endif
83
84 #else // Standard-C
85 wchar_t MyCharUpper(wchar_t c);
86 #endif
87
88 //////////////////////////////////////
89 // Compare
90
91 /*
92 #ifndef _WIN32_WCE
93 int MyStringCollate(const char *s1, const char *s2);
94 int MyStringCollateNoCase(const char *s1, const char *s2);
95 #endif
96 int MyStringCollate(const wchar_t *s1, const wchar_t *s2);
97 int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2);
98 */
99
100 int MyStringCompare(const char *s1, const char *s2);
101 int MyStringCompare(const wchar_t *s1, const wchar_t *s2);
102
103 // int MyStringCompareNoCase(const char *s1, const char *s2);
104 int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2);
105
106 template <class T>
107 class CStringBase
108 {
109 void TrimLeftWithCharSet(const CStringBase &charSet)
110 {
111 const T *p = _chars;
112 while (charSet.Find(*p) >= 0 && (*p != 0))
113 p = GetNextCharPointer(p);
114 Delete(0, (int)(p - _chars));
115 }
116 void TrimRightWithCharSet(const CStringBase &charSet)
117 {
118 const T *p = _chars;
119 const T *pLast = NULL;
120 while (*p != 0)
121 {
122 if (charSet.Find(*p) >= 0)
123 {
124 if (pLast == NULL)
125 pLast = p;
126 }
127 else
128 pLast = NULL;
129 p = GetNextCharPointer(p);
130 }
131 if (pLast != NULL)
132 {
133 int i = (int)(pLast - _chars);
134 Delete(i, _length - i);
135 }
136
137 }
138 void MoveItems(int destIndex, int srcIndex)
139 {
140 memmove(_chars + destIndex, _chars + srcIndex,
141 sizeof(T) * (_length - srcIndex + 1));
142 }
143
144 void InsertSpace(int &index, int size)
145 {
146 CorrectIndex(index);
147 GrowLength(size);
148 MoveItems(index + size, index);
149 }
150
151 static T *GetNextCharPointer(T *p)
152 { return MyStringGetNextCharPointer(p); }
153 static const T *GetNextCharPointer(const T *p)
154 { return MyStringGetNextCharPointer(p); }
155 static T *GetPrevCharPointer(T *base, T *p)
156 { return MyStringGetPrevCharPointer(base, p); }
157 static const T *GetPrevCharPointer(const T *base, const T *p)
158 { return MyStringGetPrevCharPointer(base, p); }
159 protected:
160 T *_chars;
161 int _length;
162 int _capacity;
163
164 void SetCapacity(int newCapacity)
165 {
166 int realCapacity = newCapacity + 1;
167 if (realCapacity == _capacity)
168 return;
169 /*
170 const int kMaxStringSize = 0x20000000;
171 #ifndef _WIN32_WCE
172 if (newCapacity > kMaxStringSize || newCapacity < _length)
173 throw 1052337;
174 #endif
175 */
176 T *newBuffer = new T[realCapacity];
177 if (_capacity > 0)
178 {
179 for (int i = 0; i < _length; i++)
180 newBuffer[i] = _chars[i];
181 delete []_chars;
182 }
183 _chars = newBuffer;
184 _chars[_length] = 0;
185 _capacity = realCapacity;
186 }
187
188 void GrowLength(int n)
189 {
190 int freeSize = _capacity - _length - 1;
191 if (n <= freeSize)
192 return;
193 int delta;
194 if (_capacity > 64)
195 delta = _capacity / 2;
196 else if (_capacity > 8)
197 delta = 16;
198 else
199 delta = 4;
200 if (freeSize + delta < n)
201 delta = n - freeSize;
202 SetCapacity(_capacity + delta);
203 }
204
205 void CorrectIndex(int &index) const
206 {
207 if (index > _length)
208 index = _length;
209 }
210
211 public:
212 CStringBase(): _chars(0), _length(0), _capacity(0) { SetCapacity(3); }
213 CStringBase(T c): _chars(0), _length(0), _capacity(0)
214 {
215 SetCapacity(1);
216 _chars[0] = c;
217 _chars[1] = 0;
218 _length = 1;
219 }
220 CStringBase(const T *chars): _chars(0), _length(0), _capacity(0)
221 {
222 int length = MyStringLen(chars);
223 SetCapacity(length);
224 MyStringCopy(_chars, chars); // can be optimized by memove()
225 _length = length;
226 }
227 CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0)
228 {
229 SetCapacity(s._length);
230 MyStringCopy(_chars, s._chars);
231 _length = s._length;
232 }
233 ~CStringBase() { delete []_chars; }
234
235 operator const T*() const { return _chars;}
236
237 // The minimum size of the character buffer in characters.
238 // This value does not include space for a null terminator.
239 T* GetBuffer(int minBufLength)
240 {
241 if (minBufLength >= _capacity)
242 SetCapacity(minBufLength);
243 return _chars;
244 }
245 void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); }
246 void ReleaseBuffer(int newLength)
247 {
248 /*
249 #ifndef _WIN32_WCE
250 if (newLength >= _capacity)
251 throw 282217;
252 #endif
253 */
254 _chars[newLength] = 0;
255 _length = newLength;
256 }
257
258 CStringBase& operator=(T c)
259 {
260 Empty();
261 SetCapacity(1);
262 _chars[0] = c;
263 _chars[1] = 0;
264 _length = 1;
265 return *this;
266 }
267 CStringBase& operator=(const T *chars)
268 {
269 Empty();
270 int length = MyStringLen(chars);
271 SetCapacity(length);
272 MyStringCopy(_chars, chars);
273 _length = length;
274 return *this;
275 }
276 CStringBase& operator=(const CStringBase& s)
277 {
278 if (&s == this)
279 return *this;
280 Empty();
281 SetCapacity(s._length);
282 MyStringCopy(_chars, s._chars);
283 _length = s._length;
284 return *this;
285 }
286
287 CStringBase& operator+=(T c)
288 {
289 GrowLength(1);
290 _chars[_length] = c;
291 _chars[++_length] = 0;
292 return *this;
293 }
294 CStringBase& operator+=(const T *s)
295 {
296 int len = MyStringLen(s);
297 GrowLength(len);
298 MyStringCopy(_chars + _length, s);
299 _length += len;
300 return *this;
301 }
302 CStringBase& operator+=(const CStringBase &s)
303 {
304 GrowLength(s._length);
305 MyStringCopy(_chars + _length, s._chars);
306 _length += s._length;
307 return *this;
308 }
309 void Empty()
310 {
311 _length = 0;
312 _chars[0] = 0;
313 }
314 int Length() const { return _length; }
315 bool IsEmpty() const { return (_length == 0); }
316
317 CStringBase Mid(int startIndex) const
318 { return Mid(startIndex, _length - startIndex); }
319 CStringBase Mid(int startIndex, int count ) const
320 {
321 if (startIndex + count > _length)
322 count = _length - startIndex;
323
324 if (startIndex == 0 && startIndex + count == _length)
325 return *this;
326
327 CStringBase<T> result;
328 result.SetCapacity(count);
329 // MyStringNCopy(result._chars, _chars + startIndex, count);
330 for (int i = 0; i < count; i++)
331 result._chars[i] = _chars[startIndex + i];
332 result._chars[count] = 0;
333 result._length = count;
334 return result;
335 }
336 CStringBase Left(int count) const
337 { return Mid(0, count); }
338 CStringBase Right(int count) const
339 {
340 if (count > _length)
341 count = _length;
342 return Mid(_length - count, count);
343 }
344
345 void MakeUpper()
346 { MyStringUpper(_chars); }
347 void MakeLower()
348 { MyStringLower(_chars); }
349
350 int Compare(const CStringBase& s) const
351 { return MyStringCompare(_chars, s._chars); }
352
353 int Compare(const T *s) const
354 { return MyStringCompare(_chars, s); }
355
356 int CompareNoCase(const CStringBase& s) const
357 { return MyStringCompareNoCase(_chars, s._chars); }
358
359 int CompareNoCase(const T *s) const
360 { return MyStringCompareNoCase(_chars, s); }
361
362 /*
363 int Collate(const CStringBase& s) const
364 { return MyStringCollate(_chars, s._chars); }
365 int CollateNoCase(const CStringBase& s) const
366 { return MyStringCollateNoCase(_chars, s._chars); }
367 */
368
369 int Find(T c) const { return Find(c, 0); }
370 int Find(T c, int startIndex) const
371 {
372 T *p = _chars + startIndex;
373 for (;;)
374 {
375 if (*p == c)
376 return (int)(p - _chars);
377 if (*p == 0)
378 return -1;
379 p = GetNextCharPointer(p);
380 }
381 }
382 int Find(const CStringBase &s) const { return Find(s, 0); }
383 int Find(const CStringBase &s, int startIndex) const
384 {
385 if (s.IsEmpty())
386 return startIndex;
387 for (; startIndex < _length; startIndex++)
388 {
389 int j;
390 for (j = 0; j < s._length && startIndex + j < _length; j++)
391 if (_chars[startIndex+j] != s._chars[j])
392 break;
393 if (j == s._length)
394 return startIndex;
395 }
396 return -1;
397 }
398 int ReverseFind(T c) const
399 {
400 if (_length == 0)
401 return -1;
402 T *p = _chars + _length - 1;
403 for (;;)
404 {
405 if (*p == c)
406 return (int)(p - _chars);
407 if (p == _chars)
408 return -1;
409 p = GetPrevCharPointer(_chars, p);
410 }
411 }
412 int FindOneOf(const CStringBase &s) const
413 {
414 for (int i = 0; i < _length; i++)
415 if (s.Find(_chars[i]) >= 0)
416 return i;
417 return -1;
418 }
419
420 void TrimLeft(T c)
421 {
422 const T *p = _chars;
423 while (c == *p)
424 p = GetNextCharPointer(p);
425 Delete(0, p - _chars);
426 }
427 private:
428 CStringBase GetTrimDefaultCharSet()
429 {
430 CStringBase<T> charSet;
431 charSet += (T)' ';
432 charSet += (T)'\n';
433 charSet += (T)'\t';
434 return charSet;
435 }
436 public:
437
438 void TrimLeft()
439 {
440 TrimLeftWithCharSet(GetTrimDefaultCharSet());
441 }
442 void TrimRight()
443 {
444 TrimRightWithCharSet(GetTrimDefaultCharSet());
445 }
446 void TrimRight(T c)
447 {
448 const T *p = _chars;
449 const T *pLast = NULL;
450 while (*p != 0)
451 {
452 if (*p == c)
453 {
454 if (pLast == NULL)
455 pLast = p;
456 }
457 else
458 pLast = NULL;
459 p = GetNextCharPointer(p);
460 }
461 if (pLast != NULL)
462 {
463 int i = pLast - _chars;
464 Delete(i, _length - i);
465 }
466 }
467 void Trim()
468 {
469 TrimRight();
470 TrimLeft();
471 }
472
473 int Insert(int index, T c)
474 {
475 InsertSpace(index, 1);
476 _chars[index] = c;
477 _length++;
478 return _length;
479 }
480 int Insert(int index, const CStringBase &s)
481 {
482 CorrectIndex(index);
483 if (s.IsEmpty())
484 return _length;
485 int numInsertChars = s.Length();
486 InsertSpace(index, numInsertChars);
487 for (int i = 0; i < numInsertChars; i++)
488 _chars[index + i] = s[i];
489 _length += numInsertChars;
490 return _length;
491 }
492
493 // !!!!!!!!!!!!!!! test it if newChar = '\0'
494 int Replace(T oldChar, T newChar)
495 {
496 if (oldChar == newChar)
497 return 0;
498 int number = 0;
499 int pos = 0;
500 while (pos < Length())
501 {
502 pos = Find(oldChar, pos);
503 if (pos < 0)
504 break;
505 _chars[pos] = newChar;
506 pos++;
507 number++;
508 }
509 return number;
510 }
511 int Replace(const CStringBase &oldString, const CStringBase &newString)
512 {
513 if (oldString.IsEmpty())
514 return 0;
515 if (oldString == newString)
516 return 0;
517 int oldStringLength = oldString.Length();
518 int newStringLength = newString.Length();
519 int number = 0;
520 int pos = 0;
521 while (pos < _length)
522 {
523 pos = Find(oldString, pos);
524 if (pos < 0)
525 break;
526 Delete(pos, oldStringLength);
527 Insert(pos, newString);
528 pos += newStringLength;
529 number++;
530 }
531 return number;
532 }
533 int Delete(int index, int count = 1 )
534 {
535 if (index + count > _length)
536 count = _length - index;
537 if (count > 0)
538 {
539 MoveItems(index, index + count);
540 _length -= count;
541 }
542 return _length;
543 }
544 };
545
546 template <class T>
547 CStringBase<T> operator+(const CStringBase<T>& s1, const CStringBase<T>& s2)
548 {
549 CStringBase<T> result(s1);
550 result += s2;
551 return result;
552 }
553
554 template <class T>
555 CStringBase<T> operator+(const CStringBase<T>& s, T c)
556 {
557 CStringBase<T> result(s);
558 result += c;
559 return result;
560 }
561
562 template <class T>
563 CStringBase<T> operator+(T c, const CStringBase<T>& s)
564 {
565 CStringBase<T> result(c);
566 result += s;
567 return result;
568 }
569
570 template <class T>
571 CStringBase<T> operator+(const CStringBase<T>& s, const T * chars)
572 {
573 CStringBase<T> result(s);
574 result += chars;
575 return result;
576 }
577
578 template <class T>
579 CStringBase<T> operator+(const T * chars, const CStringBase<T>& s)
580 {
581 CStringBase<T> result(chars);
582 result += s;
583 return result;
584 }
585
586 template <class T>
587 bool operator==(const CStringBase<T>& s1, const CStringBase<T>& s2)
588 { return (s1.Compare(s2) == 0); }
589
590 template <class T>
591 bool operator<(const CStringBase<T>& s1, const CStringBase<T>& s2)
592 { return (s1.Compare(s2) < 0); }
593
594 template <class T>
595 bool operator==(const T *s1, const CStringBase<T>& s2)
596 { return (s2.Compare(s1) == 0); }
597
598 template <class T>
599 bool operator==(const CStringBase<T>& s1, const T *s2)
600 { return (s1.Compare(s2) == 0); }
601
602 template <class T>
603 bool operator!=(const CStringBase<T>& s1, const CStringBase<T>& s2)
604 { return (s1.Compare(s2) != 0); }
605
606 template <class T>
607 bool operator!=(const T *s1, const CStringBase<T>& s2)
608 { return (s2.Compare(s1) != 0); }
609
610 template <class T>
611 bool operator!=(const CStringBase<T>& s1, const T *s2)
612 { return (s1.Compare(s2) != 0); }
613
614 typedef CStringBase<char> AString;
615 typedef CStringBase<wchar_t> UString;
616
617 typedef CObjectVector<AString> AStringVector;
618 typedef CObjectVector<UString> UStringVector;
619
620 #ifdef _UNICODE
621 typedef UString CSysString;
622 #else
623 typedef AString CSysString;
624 #endif
625
626 typedef CObjectVector<CSysString> CSysStringVector;
627
628 #endif
OLDNEW
« no previous file with comments | « third_party/lzma/v4_65/files/CPP/Common/MyInitGuid.h ('k') | third_party/lzma/v4_65/files/CPP/Common/MyString.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698