1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:set ts=4 sw=4 sts=4 et cindent: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla code.
17 : *
18 : * The Initial Developer of the Original Code is the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jeff Muizelaar <jmuizelaar@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* We would use std::max but MS makes it painful
40 : // windef.h defines min and max macros that we don't want
41 : // http://support.microsoft.com/kb/143208
42 : #ifdef _WIN32
43 : #define NOMINMAX
44 : #endif
45 : */
46 :
47 : #include <stdlib.h>
48 : #include <string.h>
49 : #include "nsAlgorithm.h"
50 :
51 : /* This is a standard string builder like ones in Java
52 : or C#. It uses a doubling allocation strategy
53 : to grow when out of capacity.
54 :
55 : This does not use nsTArray because nsTArray starts
56 : growing by multiples of page size after it is the
57 : size of one page. We want to keep doubling in size
58 : so that we can continue to append at high speed even
59 : for large strings.
60 :
61 : Eventually, this should be templated for wide characters.
62 :
63 : */
64 :
65 : namespace mozilla {
66 :
67 : class StringBuilder
68 : {
69 : public:
70 0 : StringBuilder() {
71 0 : mCapacity = 16;
72 0 : mLength = 0;
73 0 : mBuffer = static_cast<char*>(malloc(sizeof(char)*mCapacity));
74 0 : mBuffer[0] = '\0';
75 0 : }
76 :
77 0 : void Append(const char *s) {
78 0 : size_t newLength = strlen(s);
79 :
80 0 : EnsureCapacity(mLength + newLength+1);
81 :
82 : // copy the entire string including the null terminator
83 0 : memcpy(&mBuffer[mLength], s, newLength+1);
84 0 : mLength += newLength;
85 0 : }
86 :
87 0 : char *Buffer() {
88 0 : return mBuffer;
89 : }
90 :
91 0 : size_t Length() {
92 0 : return mLength;
93 : }
94 :
95 0 : size_t EnsureCapacity(size_t capacity) {
96 0 : if (capacity > mCapacity) {
97 : // make sure we at least double in size
98 0 : mCapacity = NS_MAX(capacity, mCapacity*2);
99 0 : mBuffer = static_cast<char*>(realloc(mBuffer, mCapacity));
100 0 : mCapacity = moz_malloc_usable_size(mBuffer);
101 : }
102 0 : return mCapacity;
103 : }
104 :
105 0 : ~StringBuilder()
106 : {
107 0 : free(mBuffer);
108 0 : }
109 :
110 : private:
111 : char *mBuffer;
112 : size_t mLength; // the length of the contained string not including the null terminator
113 : size_t mCapacity; // the total size of mBuffer
114 : };
115 :
116 : }
|