bbstring.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #ifndef BB_STRING_H
  2. #define BB_STRING_H
  3. #include "bbtypes.h"
  4. #include "bbassert.h"
  5. namespace bbGC{
  6. void *malloc( size_t size );
  7. void free( void *p );
  8. }
  9. class bbCString;
  10. class bbString{
  11. struct Rep{
  12. int refs;
  13. int length;
  14. bbChar data[0];
  15. static Rep *alloc( int length );
  16. template<class C> static Rep *create( const C *p,int length ){
  17. Rep *rep=alloc( length );
  18. for( int i=0;i<length;++i ) rep->data[i]=p[i];
  19. return rep;
  20. }
  21. template<class C> static Rep *create( const C *p ){
  22. const C *e=p;
  23. while( *e ) ++e;
  24. return create( p,e-p );
  25. }
  26. };
  27. Rep *_rep;
  28. static Rep _nullRep;
  29. void retain()const{
  30. ++_rep->refs;
  31. }
  32. void release(){
  33. if( !--_rep->refs && _rep!=&_nullRep ) bbGC::free( _rep );
  34. }
  35. bbString( Rep *rep ):_rep( rep ){
  36. }
  37. public:
  38. const char *c_str()const;
  39. bbString():_rep( &_nullRep ){
  40. }
  41. bbString( const bbString &s ):_rep( s._rep ){
  42. retain();
  43. }
  44. bbString( const void *data );
  45. bbString( const void *data,int length );
  46. bbString( const bbChar *data );
  47. bbString( const bbChar *data,int length );
  48. bbString( const wchar_t *data );
  49. bbString( const wchar_t *data,int length );
  50. #if __OBJC__
  51. bbString( const NSString *str );
  52. #endif
  53. explicit bbString( bool b );
  54. explicit bbString( int n );
  55. explicit bbString( unsigned int n );
  56. explicit bbString( long n );
  57. explicit bbString( unsigned long n );
  58. explicit bbString( long long n );
  59. explicit bbString( unsigned long long n );
  60. explicit bbString( float n );
  61. explicit bbString( double n );
  62. ~bbString(){
  63. release();
  64. }
  65. const bbChar *data()const{
  66. return _rep->data;
  67. }
  68. int length()const{
  69. return _rep->length;
  70. }
  71. bbChar operator[]( int index )const{
  72. bbDebugAssert( index>=0 && index<length(),"String character index out of range" );
  73. return data()[index];
  74. }
  75. bbString operator+()const{
  76. return *this;
  77. }
  78. bbString operator-()const;
  79. bbString operator+( const bbString &str )const;
  80. bbString operator+( const char *str )const{
  81. return operator+( bbString( str ) );
  82. }
  83. bbString operator*( int n )const;
  84. bbString &operator=( const bbString &str ){
  85. str.retain();
  86. release();
  87. _rep=str._rep;
  88. return *this;
  89. }
  90. template<class C> bbString &operator=( const C *data ){
  91. release();
  92. _rep=Rep::create( data );
  93. return *this;
  94. }
  95. bbString &operator+=( const bbString &str ){
  96. *this=*this+str;
  97. return *this;
  98. }
  99. bbString &operator+=( const char *str ){
  100. return operator+=( bbString( str ) );
  101. }
  102. int find( bbString str,int from=0 )const;
  103. int findLast( const bbString &str,int from=0 )const;
  104. bool contains( const bbString &str )const{
  105. return find( str )!=-1;
  106. }
  107. bbString slice( int from )const;
  108. bbString slice( int from,int term )const;
  109. bbString left( int count )const{
  110. return slice( 0,count );
  111. }
  112. bbString right( int count )const{
  113. return slice( -count );
  114. }
  115. bbString mid( int from,int count )const{
  116. return slice( from,from+count );
  117. }
  118. bool startsWith( const bbString &str )const;
  119. bool endsWith( const bbString &str )const;
  120. bbString toUpper()const;
  121. bbString toLower()const;
  122. bbString capitalize()const;
  123. bbString trim()const;
  124. bbString trimStart()const;
  125. bbString trimEnd()const;
  126. bbString dup( int n )const;
  127. bbString replace( const bbString &str,const bbString &repl )const;
  128. bbArray<bbString> split( bbString sep )const;
  129. bbString join( bbArray<bbString> bits )const;
  130. int compare( const bbString &t )const;
  131. bool operator<( const bbString &t )const{
  132. return compare( t )<0;
  133. }
  134. bool operator>( const bbString &t )const{
  135. return compare( t )>0;
  136. }
  137. bool operator<=( const bbString &t )const{
  138. return compare( t )<=0;
  139. }
  140. bool operator>=( const bbString &t )const{
  141. return compare( t )>=0;
  142. }
  143. bool operator==( const bbString &t )const{
  144. return compare( t )==0;
  145. }
  146. bool operator!=( const bbString &t )const{
  147. return compare( t )!=0;
  148. }
  149. operator bbBool()const{
  150. return length()!=0;
  151. }
  152. operator bbInt()const;
  153. operator bbByte()const;
  154. operator bbUByte()const;
  155. operator bbShort()const;
  156. operator bbUShort()const;
  157. operator bbUInt()const;
  158. operator bbLong()const;
  159. operator bbULong()const;
  160. operator float()const;
  161. operator double()const;
  162. int utf8Length()const;
  163. void toCString( void *buf,int size )const;
  164. void toWString( void *buf,int size )const;
  165. #if __OBJC__
  166. NSString *ToNSString()const;
  167. #endif
  168. static bbString fromChar( int chr );
  169. static bbString fromChars( bbArray<int> chrs );
  170. static bbString fromCString( const void *data ){ return bbString( data ); }
  171. static bbString fromCString( const void *data,int size ){ return bbString( data,size ); }
  172. static bbString fromWString( const void *data ){ return bbString( (const wchar_t*)data ); }
  173. static bbString fromWString( const void *data,int size ){ return bbString( (const wchar_t*)data,size ); }
  174. };
  175. class bbCString{
  176. char *_data;
  177. public:
  178. bbCString():_data(0){}
  179. bbCString( const bbString &str );
  180. ~bbCString();
  181. operator char*()const;
  182. operator signed char*()const;
  183. operator unsigned char*()const;
  184. };
  185. class bbWString{
  186. wchar_t *_data;
  187. public:
  188. bbWString():_data(0){}
  189. bbWString( const bbString &str );
  190. ~bbWString();
  191. operator wchar_t*()const;
  192. };
  193. template<class C> bbString operator+( const C *str,const bbString &str2 ){
  194. return bbString::fromCString( str )+str2;
  195. }
  196. inline bbString BB_T( const char *p ){
  197. return bbString::fromCString( p );
  198. }
  199. #endif