Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:set ts=4 sw=4 sts=4 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* We would use std::max but MS makes it painful |
michael@0 | 8 | // windef.h defines min and max macros that we don't want |
michael@0 | 9 | // http://support.microsoft.com/kb/143208 |
michael@0 | 10 | #ifdef _WIN32 |
michael@0 | 11 | #define NOMINMAX |
michael@0 | 12 | #endif |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | #include <stdlib.h> |
michael@0 | 16 | #include <string.h> |
michael@0 | 17 | #include "nsAlgorithm.h" |
michael@0 | 18 | |
michael@0 | 19 | /* This is a standard string builder like ones in Java |
michael@0 | 20 | or C#. It uses a doubling allocation strategy |
michael@0 | 21 | to grow when out of capacity. |
michael@0 | 22 | |
michael@0 | 23 | This does not use nsTArray because nsTArray starts |
michael@0 | 24 | growing by multiples of page size after it is the |
michael@0 | 25 | size of one page. We want to keep doubling in size |
michael@0 | 26 | so that we can continue to append at high speed even |
michael@0 | 27 | for large strings. |
michael@0 | 28 | |
michael@0 | 29 | Eventually, this should be templated for wide characters. |
michael@0 | 30 | |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | namespace mozilla { |
michael@0 | 34 | |
michael@0 | 35 | class StringBuilder |
michael@0 | 36 | { |
michael@0 | 37 | public: |
michael@0 | 38 | StringBuilder() { |
michael@0 | 39 | mCapacity = 16; |
michael@0 | 40 | mLength = 0; |
michael@0 | 41 | mBuffer = static_cast<char*>(malloc(sizeof(char)*mCapacity)); |
michael@0 | 42 | mBuffer[0] = '\0'; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | void Append(const char *s) { |
michael@0 | 46 | size_t newLength = strlen(s); |
michael@0 | 47 | |
michael@0 | 48 | EnsureCapacity(mLength + newLength+1); |
michael@0 | 49 | |
michael@0 | 50 | // copy the entire string including the null terminator |
michael@0 | 51 | memcpy(&mBuffer[mLength], s, newLength+1); |
michael@0 | 52 | mLength += newLength; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | char *Buffer() { |
michael@0 | 56 | return mBuffer; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | size_t Length() { |
michael@0 | 60 | return mLength; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | size_t EnsureCapacity(size_t capacity) { |
michael@0 | 64 | if (capacity > mCapacity) { |
michael@0 | 65 | // make sure we at least double in size |
michael@0 | 66 | mCapacity = XPCOM_MAX(capacity, mCapacity*2); |
michael@0 | 67 | mBuffer = static_cast<char*>(realloc(mBuffer, mCapacity)); |
michael@0 | 68 | mCapacity = moz_malloc_usable_size(mBuffer); |
michael@0 | 69 | } |
michael@0 | 70 | return mCapacity; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | ~StringBuilder() |
michael@0 | 74 | { |
michael@0 | 75 | free(mBuffer); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | private: |
michael@0 | 79 | char *mBuffer; |
michael@0 | 80 | size_t mLength; // the length of the contained string not including the null terminator |
michael@0 | 81 | size_t mCapacity; // the total size of mBuffer |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | } |