1 /******************************************************* 2 3 CoolReader Engine 4 5 lvstring16collection.cpp: collection of strings 6 7 (c) Vadim Lopatin, 2000-2006 8 This source code is distributed under the terms of 9 GNU General Public License 10 See LICENSE file for details 11 12 *******************************************************/ 13 14 #include "../include/lvstring8collection.h" 15 split(const lString8 & str,const lString8 & delimiter)16void lString8Collection::split( const lString8 & str, const lString8 & delimiter ) 17 { 18 if (str.empty()) 19 return; 20 for (int startpos = 0; startpos < str.length(); ) { 21 int pos = str.pos(delimiter, startpos); 22 if (pos < 0) 23 pos = str.length(); 24 add(str.substr(startpos, pos - startpos)); 25 startpos = pos + delimiter.length(); 26 } 27 } 28 erase(int offset,int cnt)29void lString8Collection::erase(int offset, int cnt) 30 { 31 if (count <= 0) 32 return; 33 if (offset < 0 || offset + cnt > count) 34 return; 35 int i; 36 for (i = offset; i < offset + cnt; i++) 37 { 38 ((lString8 *)chunks)[i].release(); 39 } 40 for (i = offset + cnt; i < count; i++) 41 { 42 chunks[i-cnt] = chunks[i]; 43 } 44 count -= cnt; 45 if (!count) 46 clear(); 47 } 48 operator ==(const lString8Collection & other) const49bool lString8Collection::operator==(const lString8Collection& other) const 50 { 51 bool equal = false; 52 // Compare <this> with <other> consider items order 53 if (count == other.length()) { 54 equal = true; 55 for (int i = 0; i < count; i++) { 56 if (((lString8 *)chunks)[i] != other[i]) { 57 equal = false; 58 break; 59 } 60 } 61 } 62 return equal; 63 } 64 operator !=(const lString8Collection & other) const65bool lString8Collection::operator!=(const lString8Collection &other) const 66 { 67 return !operator ==(other); 68 } 69 reserve(int space)70void lString8Collection::reserve(int space) 71 { 72 if ( count + space > size ) 73 { 74 int tmpSize = count + space + 64; 75 void* tmp = realloc( chunks, sizeof(lstring8_chunk_t *) * tmpSize ); 76 if (tmp) { 77 size = tmpSize; 78 chunks = (lstring8_chunk_t * *)tmp; 79 } 80 else { 81 // TODO: throw exception or change function prototype & return code 82 } 83 } 84 } 85 add(const lString8 & str)86int lString8Collection::add( const lString8 & str ) 87 { 88 reserve( 1 ); 89 chunks[count] = str.pchunk; 90 str.addref(); 91 return count++; 92 } 93 getHash() const94lUInt32 lString8Collection::getHash() const 95 { 96 lUInt32 hash = 0; 97 for (int i = 0; i < count; i++) 98 hash = 31*hash + ((lString8 *)chunks)[i].getHash(); 99 return hash; 100 } 101 clear()102void lString8Collection::clear() 103 { 104 for (int i=0; i<count; i++) 105 { 106 ((lString8 *)chunks)[i].release(); 107 } 108 if (chunks) 109 free(chunks); 110 chunks = NULL; 111 count = 0; 112 size = 0; 113 } 114