aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/blob.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/blob.cpp')
-rw-r--r--src/unstable/blob.cpp271
1 files changed, 271 insertions, 0 deletions
diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp
index 51a3b93..f8cf1b0 100644
--- a/src/unstable/blob.cpp
+++ b/src/unstable/blob.cpp
@@ -7,6 +7,7 @@
7 7
8#include "bu/blob.h" 8#include "bu/blob.h"
9#include "bu/exceptioninvaliditerator.h" 9#include "bu/exceptioninvaliditerator.h"
10#include "bu/exceptionindexoutofbounds.h"
10 11
11#include <string.h> 12#include <string.h>
12 13
@@ -63,6 +64,37 @@ char *Bu::Blob::c_str() const
63 return pData; 64 return pData;
64} 65}
65 66
67char &Bu::Blob::operator[]( int32_t iIndex )
68{
69 if( iIndex < 0 || iIndex >= iSize || pData == NULL )
70 throw Bu::ExceptionIndexOutOfBounds();
71
72 return pData[iIndex];
73}
74
75char Bu::Blob::operator[]( int32_t iIndex ) const
76{
77 if( iIndex < 0 || iIndex >= iSize || pData == NULL )
78 throw Bu::ExceptionIndexOutOfBounds();
79
80 return pData[iIndex];
81}
82
83bool Bu::Blob::isEmpty() const
84{
85 return pData != NULL && iSize == 0;
86}
87
88bool Bu::Blob::isNull() const
89{
90 return pData == NULL;
91}
92
93bool Bu::Blob::isNullOrEmpty() const
94{
95 return pData == NULL || iSize == 0;
96}
97
66Bu::Blob &Bu::Blob::operator=( const Bu::Blob &rRhs ) 98Bu::Blob &Bu::Blob::operator=( const Bu::Blob &rRhs )
67{ 99{
68 delete[] pData; 100 delete[] pData;
@@ -85,6 +117,210 @@ Bu::Blob &Bu::Blob::operator=( const char *pRhs )
85 return *this; 117 return *this;
86} 118}
87 119
120bool Bu::Blob::operator==( const Bu::Blob &rRhs ) const
121{
122 if( pData == rRhs.pData )
123 return true;
124
125 if( iSize != rRhs.iSize )
126 return false;
127
128 for( int32_t j = 0; j < iSize; j++ )
129 {
130 if( pData[j] != rRhs.pData[j] )
131 return false;
132 }
133
134 return true;
135}
136
137bool Bu::Blob::operator==( const char *pRhs ) const
138{
139 if( pData == pRhs )
140 return true;
141
142 for( int32_t j = 0; j < iSize && pRhs[j]; j++ )
143 {
144 if( pData[j] != pRhs[j] )
145 return false;
146 }
147
148 if( pRhs[iSize] == '\0' )
149 return true;
150
151 return false;
152}
153
154bool Bu::Blob::operator!=( const Bu::Blob &rRhs ) const
155{
156 if( pData == rRhs.pData )
157 return false;
158
159 if( iSize != rRhs.iSize )
160 return true;
161
162 for( int32_t j = 0; j < iSize; j++ )
163 {
164 if( pData[j] != rRhs.pData[j] )
165 return true;
166 }
167
168 return false;
169}
170
171bool Bu::Blob::operator!=( const char *pRhs ) const
172{
173 if( pData == pRhs )
174 return false;
175
176 for( int32_t j = 0; j < iSize && pRhs[j]; j++ )
177 {
178 if( pData[j] != pRhs[j] )
179 return true;
180 }
181
182 if( pRhs[iSize] == '\0' )
183 return false;
184
185 return true;
186}
187
188bool Bu::Blob::operator<( const Bu::Blob &rRhs ) const
189{
190 if( pData == rRhs.pData )
191 return false;
192
193 for( int32_t j = 0; j < iSize && j < rRhs.iSize; j++ )
194 {
195 if( pData[j] != rRhs.pData[j] )
196 return pData[j] < rRhs.pData[j];
197 }
198
199 if( iSize < rRhs.iSize )
200 return true;
201
202 return false;
203}
204
205bool Bu::Blob::operator<( const char *pRhs ) const
206{
207 if( pData == pRhs )
208 return false;
209
210 for( int32_t j = 0; j < iSize && pRhs[j]; j++ )
211 {
212 if( pData[j] != pRhs[j] )
213 return pData[j] < pRhs[j];
214 }
215
216 if( pRhs[iSize] == '\0' )
217 return false;
218
219 return true;
220}
221
222bool Bu::Blob::operator<=( const Bu::Blob &rRhs ) const
223{
224 if( pData == rRhs.pData )
225 return true;
226
227 for( int32_t j = 0; j < iSize && j < rRhs.iSize; j++ )
228 {
229 if( pData[j] != rRhs.pData[j] )
230 return pData[j] < rRhs.pData[j];
231 }
232
233 return iSize <= rRhs.iSize;
234}
235
236bool Bu::Blob::operator<=( const char *pRhs ) const
237{
238 if( pData == pRhs )
239 return true;
240
241 for( int32_t j = 0; j < iSize && pRhs[j]; j++ )
242 {
243 if( pData[j] != pRhs[j] )
244 return pData[j] < pRhs[j];
245 }
246
247 if( pRhs[iSize] == '\0' )
248 return true;
249
250 return true;
251}
252
253bool Bu::Blob::operator>( const Bu::Blob &rRhs ) const
254{
255 if( pData == rRhs.pData )
256 return false;
257
258 for( int32_t j = 0; j < iSize && j < rRhs.iSize; j++ )
259 {
260 if( pData[j] != rRhs.pData[j] )
261 return pData[j] > rRhs.pData[j];
262 }
263
264 if( iSize > rRhs.iSize )
265 return true;
266
267 return false;
268}
269
270bool Bu::Blob::operator>( const char *pRhs ) const
271{
272 if( pData == pRhs )
273 return false;
274
275 int32_t j;
276 for( j = 0; j < iSize && pRhs[j]; j++ )
277 {
278 if( pData[j] != pRhs[j] )
279 return pData[j] > pRhs[j];
280 }
281
282 if( pRhs[iSize] == '\0' && iSize-1 > j )
283 return true;
284
285 return false;
286}
287
288bool Bu::Blob::operator>=( const Bu::Blob &rRhs ) const
289{
290 if( pData == rRhs.pData )
291 return true;
292
293 for( int32_t j = 0; j < iSize && j < rRhs.iSize; j++ )
294 {
295 if( pData[j] != rRhs.pData[j] )
296 return pData[j] > rRhs.pData[j];
297 }
298
299 return iSize >= rRhs.iSize;
300}
301
302bool Bu::Blob::operator>=( const char *pRhs ) const
303{
304 if( pData == pRhs )
305 return true;
306
307 for( int32_t j = 0; j < iSize && pRhs[j]; j++ )
308 {
309 if( pData[j] != pRhs[j] )
310 return pData[j] > pRhs[j];
311 }
312
313 if( pRhs[iSize] == '\0' )
314 return true;
315
316 return true;
317}
318
319
320/////
321// Iterators
322//
323
88Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) : 324Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) :
89 pBlob( pBlob ), 325 pBlob( pBlob ),
90 iIndex( bForward?0:pBlob->iSize-1 ), 326 iIndex( bForward?0:pBlob->iSize-1 ),
@@ -400,4 +636,39 @@ Bu::Blob::const_iterator Bu::Blob::rend() const
400 return const_iterator(); 636 return const_iterator();
401} 637}
402 638
639////
640// Helper functions
641//
642template<> uint32_t Bu::__calcHashCode<Bu::Blob>( const Bu::Blob &k )
643{
644 int32_t sz = k.getSize();
645 const char *s = k.getData();
646
647 uint32_t iPos = 0;
648 for( int32_t j = 0; j < sz; j++, s++ )
649 {
650 iPos = *s + (iPos << 6) + (iPos << 16) - iPos;
651 }
652
653 return iPos;
654}
655
656template<> bool Bu::__cmpHashKeys<Bu::Blob>(
657 const Bu::Blob &a, const Bu::Blob &b )
658{
659 return a == b;
660}
661
662#include <stdio.h>
663template<> void Bu::__tracer_format<Bu::Blob>( const Bu::Blob &v )
664{
665 printf("(%d)\"%s\"", v.getSize(), v.getData() );
666}
667
668#include "bu/formatter.h"
669Bu::Formatter &Bu::operator<<( Bu::Formatter &rOut, const Bu::Blob &b )
670{
671 rOut.write( b.getData(), b.getSize() );
672 return rOut;
673}
403 674