aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unit/blob.unit63
-rw-r--r--src/unstable/blob.cpp161
-rw-r--r--src/unstable/blob.h39
3 files changed, 203 insertions, 60 deletions
diff --git a/src/unit/blob.unit b/src/unit/blob.unit
index 645052b..6f98cfc 100644
--- a/src/unit/blob.unit
+++ b/src/unit/blob.unit
@@ -26,6 +26,23 @@ suite Blob
26 unitTest( !strcmp( b.getData(), "New string") ); 26 unitTest( !strcmp( b.getData(), "New string") );
27 } 27 }
28 28
29 test empty
30 {
31 Bu::Blob n;
32 Bu::Blob e("");
33
34 unitTest( n.isEmpty() == false );
35 unitTest( n.isNull() == true );
36 unitTest( n.isNullOrEmpty() == true );
37
38 unitTest( e.isEmpty() == true );
39 unitTest( e.isNull() == false );
40 unitTest( e.isNullOrEmpty() == true );
41
42 unitTestCatch( n[0], Bu::ExceptionIndexOutOfBounds );
43 unitTestCatch( e[0], Bu::ExceptionIndexOutOfBounds );
44 }
45
29 test index 46 test index
30 { 47 {
31 Bu::Blob a("hello there"); 48 Bu::Blob a("hello there");
@@ -87,10 +104,56 @@ suite Blob
87 104
88 test iterator 105 test iterator
89 { 106 {
107 const char *sDat = "abcdefghijklmnopqrstuvwxyz";
108 Bu::Blob bDat( sDat );
109
110 Bu::Blob::iterator i2;
111 const char *sCmp = sDat;
112 for( Bu::Blob::iterator i = bDat.begin(); i; i++, sCmp++ )
113 {
114 unitTest( *i == *sCmp );
115 if( *i == 'k' )
116 i2 = i;
117 }
118
119 i2++;
120 unitTest( *i2 == 'l' );
121 unitTest( *(i2 + 3) == 'o' );
122 unitTest( Bu::Blob( i2 ) == "lmnopqrstuvwxyz" );
123 unitTest( Bu::Blob( i2, i2+5 ) == "lmnop" );
124
125 sCmp--;
126 for( Bu::Blob::iterator i = bDat.rbegin(); i; i++, sCmp-- )
127 {
128 unitTest( *i == *sCmp );
129 }
90 } 130 }
91 131
92 test const_iterator 132 test const_iterator
93 { 133 {
134 const char *sDat = "abcdefghijklmnopqrstuvwxyz";
135 Bu::Blob bDat( sDat );
136
137 Bu::Blob::const_iterator i2;
138 const char *sCmp = sDat;
139 for( Bu::Blob::const_iterator i = bDat.begin(); i; i++, sCmp++ )
140 {
141 unitTest( *i == *sCmp );
142 if( *i == 'k' )
143 i2 = i;
144 }
145
146 i2++;
147 unitTest( *i2 == 'l' );
148 unitTest( *(i2 + 3) == 'o' );
149 unitTest( Bu::Blob( i2 ) == "lmnopqrstuvwxyz" );
150 unitTest( Bu::Blob( i2, i2+5 ) == "lmnop" );
151
152 sCmp--;
153 for( Bu::Blob::const_iterator i = bDat.rbegin(); i; i++, sCmp-- )
154 {
155 unitTest( *i == *sCmp );
156 }
94 } 157 }
95 158
96 test sort 159 test sort
diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp
index f8cf1b0..3f7d9d9 100644
--- a/src/unstable/blob.cpp
+++ b/src/unstable/blob.cpp
@@ -34,14 +34,48 @@ Bu::Blob::Blob( const char *pSrc ) :
34 memcpy( pData, pSrc, iSize+1 ); 34 memcpy( pData, pSrc, iSize+1 );
35} 35}
36 36
37Bu::Blob::Blob( const void *pSrc, int32_t iSize ) 37Bu::Blob::Blob( const void *pSrc, int32_t iSize ) :
38 pData( 0 ),
39 iSize( iSize )
38{ 40{
39 this->iSize = iSize;
40 pData = new char[iSize+1]; 41 pData = new char[iSize+1];
41 memcpy( pData, pSrc, iSize ); 42 memcpy( pData, pSrc, iSize );
42 pData[iSize] = '\0'; 43 pData[iSize] = '\0';
43} 44}
44 45
46Bu::Blob::Blob( const Bu::Blob::const_iterator &iStart ) :
47 pData( 0 ),
48 iSize( -1 )
49{
50 if( !iStart )
51 return;
52
53 iSize = iStart.pBlob->iSize - iStart.iIndex;
54 pData = new char[iSize+1];
55 memcpy( pData, iStart.pBlob->pData+iStart.iIndex, iSize );
56}
57
58Bu::Blob::Blob( const Bu::Blob::const_iterator &iStart,
59 const Bu::Blob::const_iterator &iEnd ) :
60 pData( 0 ),
61 iSize( -1 )
62{
63 if( !iStart )
64 return;
65
66 if( iEnd )
67 {
68 iSize = iEnd.iIndex - iStart.iIndex;
69 }
70 else
71 {
72 iSize = iStart.pBlob->iSize - iStart.iIndex;
73 }
74
75 pData = new char[iSize+1];
76 memcpy( pData, iStart.pBlob->pData+iStart.iIndex, iSize );
77}
78
45Bu::Blob::~Blob() 79Bu::Blob::~Blob()
46{ 80{
47 delete[] pData; 81 delete[] pData;
@@ -316,6 +350,45 @@ bool Bu::Blob::operator>=( const char *pRhs ) const
316 return true; 350 return true;
317} 351}
318 352
353Bu::Blob::iterator Bu::Blob::begin()
354{
355 return iterator( this, true );
356}
357
358Bu::Blob::const_iterator Bu::Blob::begin() const
359{
360 return const_iterator( this, true );
361}
362
363Bu::Blob::iterator Bu::Blob::end()
364{
365 return iterator();
366}
367
368Bu::Blob::const_iterator Bu::Blob::end() const
369{
370 return const_iterator();
371}
372
373Bu::Blob::iterator Bu::Blob::rbegin()
374{
375 return iterator( this, false );
376}
377
378Bu::Blob::const_iterator Bu::Blob::rbegin() const
379{
380 return const_iterator( this, false );
381}
382
383Bu::Blob::iterator Bu::Blob::rend()
384{
385 return iterator();
386}
387
388Bu::Blob::const_iterator Bu::Blob::rend() const
389{
390 return const_iterator();
391}
319 392
320///// 393/////
321// Iterators 394// Iterators
@@ -328,6 +401,13 @@ Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) :
328{ 401{
329} 402}
330 403
404Bu::Blob::iterator::iterator( Bu::Blob *pBlob, int32_t iIndex, bool bForward ) :
405 pBlob( pBlob ),
406 iIndex( iIndex ),
407 bForward( bForward )
408{
409}
410
331Bu::Blob::iterator::iterator() : 411Bu::Blob::iterator::iterator() :
332 pBlob( NULL ), 412 pBlob( NULL ),
333 iIndex( -1 ), 413 iIndex( -1 ),
@@ -366,7 +446,7 @@ char &Bu::Blob::iterator::operator *()
366 return pBlob->pData[iIndex]; 446 return pBlob->pData[iIndex];
367} 447}
368 448
369Bu::Blob::iterator &Bu::Blob::iterator::operator++( int ) 449Bu::Blob::iterator &Bu::Blob::iterator::operator++( int32_t )
370{ 450{
371 if( bForward ) 451 if( bForward )
372 ++iIndex; 452 ++iIndex;
@@ -386,7 +466,7 @@ Bu::Blob::iterator &Bu::Blob::iterator::operator++()
386 return *this; 466 return *this;
387} 467}
388 468
389Bu::Blob::iterator &Bu::Blob::iterator::operator--( int ) 469Bu::Blob::iterator &Bu::Blob::iterator::operator--( int32_t )
390{ 470{
391 if( bForward ) 471 if( bForward )
392 --iIndex; 472 --iIndex;
@@ -406,6 +486,16 @@ Bu::Blob::iterator &Bu::Blob::iterator::operator--()
406 return *this; 486 return *this;
407} 487}
408 488
489Bu::Blob::iterator Bu::Blob::iterator::operator+( int32_t iDelta ) const
490{
491 return iterator( pBlob, iIndex + iDelta, bForward );
492}
493
494Bu::Blob::iterator Bu::Blob::iterator::operator-( int32_t iDelta ) const
495{
496 return iterator( pBlob, iIndex - iDelta, bForward );
497}
498
409bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs ) 499bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs )
410{ 500{
411 if( isValid() == false && rRhs.isValid() == false ) 501 if( isValid() == false && rRhs.isValid() == false )
@@ -454,6 +544,13 @@ Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, bool bForward ) :
454 bForward( bForward ) 544 bForward( bForward )
455{ 545{
456} 546}
547Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, int32_t iIndex,
548 bool bForward ) :
549 pBlob( pBlob ),
550 iIndex( iIndex ),
551 bForward( bForward )
552{
553}
457 554
458Bu::Blob::const_iterator::const_iterator() : 555Bu::Blob::const_iterator::const_iterator() :
459 pBlob( NULL ), 556 pBlob( NULL ),
@@ -541,6 +638,18 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--()
541 return *this; 638 return *this;
542} 639}
543 640
641Bu::Blob::const_iterator Bu::Blob::const_iterator::operator+( int32_t iDelta )
642 const
643{
644 return const_iterator( pBlob, iIndex + iDelta, bForward );
645}
646
647Bu::Blob::const_iterator Bu::Blob::const_iterator::operator-( int32_t iDelta )
648 const
649{
650 return const_iterator( pBlob, iIndex - iDelta, bForward );
651}
652
544bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs ) 653bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs )
545{ 654{
546 if( isValid() == false && rRhs.isValid() == false ) 655 if( isValid() == false && rRhs.isValid() == false )
@@ -595,50 +704,10 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=(
595 return *this; 704 return *this;
596} 705}
597 706
598 707/////
599Bu::Blob::iterator Bu::Blob::begin()
600{
601 return iterator( this, true );
602}
603
604Bu::Blob::const_iterator Bu::Blob::begin() const
605{
606 return const_iterator( this, true );
607}
608
609Bu::Blob::iterator Bu::Blob::end()
610{
611 return iterator();
612}
613
614Bu::Blob::const_iterator Bu::Blob::end() const
615{
616 return const_iterator();
617}
618
619Bu::Blob::iterator Bu::Blob::rbegin()
620{
621 return iterator( this, false );
622}
623
624Bu::Blob::const_iterator Bu::Blob::rbegin() const
625{
626 return const_iterator( this, false );
627}
628
629Bu::Blob::iterator Bu::Blob::rend()
630{
631 return iterator();
632}
633
634Bu::Blob::const_iterator Bu::Blob::rend() const
635{
636 return const_iterator();
637}
638
639////
640// Helper functions 708// Helper functions
641// 709//
710
642template<> uint32_t Bu::__calcHashCode<Bu::Blob>( const Bu::Blob &k ) 711template<> uint32_t Bu::__calcHashCode<Bu::Blob>( const Bu::Blob &k )
643{ 712{
644 int32_t sz = k.getSize(); 713 int32_t sz = k.getSize();
diff --git a/src/unstable/blob.h b/src/unstable/blob.h
index 6ce0c67..daee5f9 100644
--- a/src/unstable/blob.h
+++ b/src/unstable/blob.h
@@ -25,10 +25,16 @@ namespace Bu
25 class Blob 25 class Blob
26 { 26 {
27 public: 27 public:
28 class iterator;
29 class const_iterator;
30
31 public:
28 Blob(); 32 Blob();
29 Blob( const Blob &rSrc ); 33 Blob( const Blob &rSrc );
30 Blob( const char *pSrc ); 34 Blob( const char *pSrc );
31 Blob( const void *pSrc, int32_t iSize ); 35 Blob( const void *pSrc, int32_t iSize );
36 Blob( const const_iterator &iStart );
37 Blob( const const_iterator &iStart, const const_iterator &iEnd );
32 virtual ~Blob(); 38 virtual ~Blob();
33 39
34 int32_t getSize() const; 40 int32_t getSize() const;
@@ -58,13 +64,22 @@ namespace Bu
58 bool operator>=( const Blob &rRhs ) const; 64 bool operator>=( const Blob &rRhs ) const;
59 bool operator>=( const char *rRhs ) const; 65 bool operator>=( const char *rRhs ) const;
60 66
61 class const_iterator; 67 iterator begin();
68 const_iterator begin() const;
69 iterator end();
70 const_iterator end() const;
71 iterator rbegin();
72 const_iterator rbegin() const;
73 iterator rend();
74 const_iterator rend() const;
75
62 class iterator 76 class iterator
63 { 77 {
64 friend class Blob; 78 friend class Blob;
65 friend class const_iterator; 79 friend class const_iterator;
66 private: 80 private:
67 iterator( Blob *pBlob, bool bForward ); 81 iterator( Blob *pBlob, bool bForward );
82 iterator( Blob *pBlob, int32_t iIndex, bool bForward );
68 83
69 public: 84 public:
70 iterator(); 85 iterator();
@@ -74,10 +89,12 @@ namespace Bu
74 operator bool() const; 89 operator bool() const;
75 char &operator *(); 90 char &operator *();
76 91
77 iterator &operator++( int ); 92 iterator &operator++( int32_t );
78 iterator &operator++(); 93 iterator &operator++();
79 iterator &operator--( int ); 94 iterator &operator--( int32_t );
80 iterator &operator--(); 95 iterator &operator--();
96 iterator operator+( int32_t iDelta ) const;
97 iterator operator-( int32_t iDelta ) const;
81 bool operator==( const iterator &rRhs ); 98 bool operator==( const iterator &rRhs );
82 bool operator==( const const_iterator &rRhs ); 99 bool operator==( const const_iterator &rRhs );
83 bool operator!=( const iterator &rRhs ); 100 bool operator!=( const iterator &rRhs );
@@ -97,6 +114,7 @@ namespace Bu
97 friend class iterator; 114 friend class iterator;
98 private: 115 private:
99 const_iterator( const Blob *pBlob, bool bForward ); 116 const_iterator( const Blob *pBlob, bool bForward );
117 const_iterator( const Blob *pBlob, int32_t iIndex, bool bForward );
100 118
101 public: 119 public:
102 const_iterator(); 120 const_iterator();
@@ -107,10 +125,12 @@ namespace Bu
107 operator bool() const; 125 operator bool() const;
108 char operator *() const; 126 char operator *() const;
109 127
110 const_iterator &operator++( int ); 128 const_iterator &operator++( int32_t );
111 const_iterator &operator++(); 129 const_iterator &operator++();
112 const_iterator &operator--( int ); 130 const_iterator &operator--( int32_t );
113 const_iterator &operator--(); 131 const_iterator &operator--();
132 const_iterator operator+( int32_t iDelta ) const;
133 const_iterator operator-( int32_t iDelta ) const;
114 bool operator==( const iterator &rRhs ); 134 bool operator==( const iterator &rRhs );
115 bool operator==( const const_iterator &rRhs ); 135 bool operator==( const const_iterator &rRhs );
116 bool operator!=( const iterator &rRhs ); 136 bool operator!=( const iterator &rRhs );
@@ -125,15 +145,6 @@ namespace Bu
125 bool bForward; 145 bool bForward;
126 }; 146 };
127 147
128 iterator begin();
129 const_iterator begin() const;
130 iterator end();
131 const_iterator end() const;
132 iterator rbegin();
133 const_iterator rbegin() const;
134 iterator rend();
135 const_iterator rend() const;
136
137 private: 148 private:
138 char *pData; 149 char *pData;
139 int32_t iSize; 150 int32_t iSize;