]> www.fi.muni.cz Git - evince.git/blob - GString.cc
e0890912d5b495a1ecb6a7c84d1f0023ddde094b
[evince.git] / GString.cc
1 //========================================================================
2 //
3 // GString.cc
4 //
5 // Simple variable-length string type.
6 //
7 // Copyright 1996 Derek B. Noonburg
8 //
9 //========================================================================
10
11 #ifdef __GNUC__
12 #pragma implementation
13 #endif
14
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "GString.h"
20
21 static inline int size(int len) {
22   int delta;
23
24   delta = len < 256 ? 7 : 255;
25   return ((len + 1) + delta) & ~delta;
26 }
27
28 inline void GString::resize(int length1) {
29   char *s1;
30
31   if (!s) {
32     s = new char[size(length1)];
33   } else if (size(length1) != size(length)) {
34     s1 = new char[size(length1)];
35     memcpy(s1, s, length + 1);
36     delete[] s;
37     s = s1;
38   }
39 }
40
41 GString::GString() {
42   s = NULL;
43   resize(length = 0);
44   s[0] = '\0';
45 }
46
47 GString::GString(char *s1) {
48   int n = strlen(s1);
49
50   s = NULL;
51   resize(length = n);
52   memcpy(s, s1, n + 1);
53 }
54
55 GString::GString(char *s1, int length1) {
56   s = NULL;
57   resize(length = length1);
58   memcpy(s, s1, length * sizeof(char));
59   s[length] = '\0';
60 }
61
62 GString::GString(GString *str) {
63   s = NULL;
64   resize(length = str->getLength());
65   memcpy(s, str->getCString(), length + 1);
66 }
67
68 GString::GString(GString *str1, GString *str2) {
69   int n1 = str1->getLength();
70   int n2 = str2->getLength();
71
72   s = NULL;
73   resize(length = n1 + n2);
74   memcpy(s, str1->getCString(), n1);
75   memcpy(s + n1, str2->getCString(), n2 + 1);
76 }
77
78 GString::~GString() {
79   delete[] s;
80 }
81
82 GString *GString::clear() {
83   s[length = 0] = '\0';
84   resize(0);
85   return this;
86 }
87
88 GString *GString::append(char c) {
89   resize(length + 1);
90   s[length++] = c;
91   s[length] = '\0';
92   return this;
93 }
94
95 GString *GString::append(GString *str) {
96   int n = str->getLength();
97
98   resize(length + n);
99   memcpy(s + length, str->getCString(), n + 1);
100   length += n;
101   return this;
102 }
103
104 GString *GString::append(char *str) {
105   int n = strlen(str);
106
107   resize(length + n);
108   memcpy(s + length, str, n + 1);
109   length += n;
110   return this;
111 }
112
113 GString *GString::append(char *str, int length1) {
114   resize(length + length1);
115   memcpy(s + length, str, length1);
116   length += length1;
117   s[length] = '\0';
118   return this;
119 }
120
121 GString *GString::insert(int i, char c) {
122   int j;
123
124   resize(length + 1);
125   for (j = length + 1; j > i; --j)
126     s[j] = s[j-1];
127   s[i] = c;
128   ++length;
129   return this;
130 }
131
132 GString *GString::insert(int i, GString *str) {
133   int n = str->getLength();
134   int j;
135
136   resize(length + n);
137   for (j = length; j >= i; --j)
138     s[j+n] = s[j];
139   memcpy(s+i, str->getCString(), n);
140   length += n;
141   return this;
142 }
143
144 GString *GString::insert(int i, char *str) {
145   int n = strlen(str);
146   int j;
147
148   resize(length + n);
149   for (j = length; j >= i; --j)
150     s[j+n] = s[j];
151   memcpy(s+i, str, n);
152   length += n;
153   return this;
154 }
155
156 GString *GString::insert(int i, char *str, int length1) {
157   int j;
158
159   resize(length + length1);
160   for (j = length; j >= i; --j)
161     s[j+length1] = s[j];
162   memcpy(s+i, str, length1);
163   length += length1;
164   return this;
165 }
166
167 GString *GString::del(int i, int n) {
168   int j;
169
170   if (n > 0) {
171     for (j = i; j <= length - n; ++j)
172       s[j] = s[j + n];
173     resize(length -= n);
174   }
175   return this;
176 }
177
178 GString *GString::upperCase() {
179   int i;
180
181   for (i = 0; i < length; ++i) {
182     if (islower(s[i]))
183       s[i] = toupper(s[i]);
184   }
185   return this;
186 }
187
188 GString *GString::lowerCase() {
189   int i;
190
191   for (i = 0; i < length; ++i) {
192     if (isupper(s[i]))
193       s[i] = tolower(s[i]);
194   }
195   return this;
196 }