2012-04-19 14:35:52 +08:00
/****************************************************************************
Copyright ( c ) 2010 cocos2d - x . org
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef __CCSTRING_H__
# define __CCSTRING_H__
2012-08-18 05:45:21 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
# include <string.h>
# endif
2012-08-03 11:07:59 +08:00
# include <stdarg.h>
2012-04-19 14:35:52 +08:00
# include <string>
2012-06-16 12:48:20 +08:00
# include <functional>
2012-04-19 14:35:52 +08:00
# include "CCObject.h"
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @ addtogroup data_structures
* @ {
*/
2013-07-08 23:05:47 +08:00
class CC_DLL String : public Object , public Clonable
2012-04-19 14:35:52 +08:00
{
public :
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
String ( ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
String ( const char * str ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
String ( const std : : string & str ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
String ( const String & str ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
virtual ~ String ( ) ;
2012-04-19 14:35:52 +08:00
2013-09-13 11:41:20 +08:00
/* override assignment operator
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
String & operator = ( const String & other ) ;
2012-04-19 14:35:52 +08:00
2013-09-13 11:41:20 +08:00
/** init a string with format, it's similar with the c function 'sprintf'
* @ js NA
* @ lua NA
*/
2013-04-19 17:56:27 +08:00
bool initWithFormat ( const char * format , . . . ) CC_FORMAT_PRINTF ( 2 , 3 ) ;
2012-04-19 14:35:52 +08:00
2013-09-13 11:41:20 +08:00
/** convert to int value
* @ js NA
*/
2012-04-19 14:35:52 +08:00
int intValue ( ) const ;
2013-09-13 11:41:20 +08:00
/** convert to unsigned int value
* @ js NA
*/
2012-04-19 14:35:52 +08:00
unsigned int uintValue ( ) const ;
2013-09-13 11:41:20 +08:00
/** convert to float value
* @ js NA
*/
2012-04-19 14:35:52 +08:00
float floatValue ( ) const ;
2013-09-13 11:41:20 +08:00
/** convert to double value
* @ js NA
*/
2012-04-19 14:35:52 +08:00
double doubleValue ( ) const ;
2013-09-13 11:41:20 +08:00
/** convert to bool value
* @ js NA
*/
2012-04-19 14:35:52 +08:00
bool boolValue ( ) const ;
2013-09-13 11:41:20 +08:00
/** get the C string
* @ js NA
*/
2012-04-19 14:35:52 +08:00
const char * getCString ( ) const ;
2013-09-13 11:41:20 +08:00
/** get the length of string
* @ js NA
*/
2012-04-19 14:35:52 +08:00
unsigned int length ( ) const ;
2013-09-13 11:41:20 +08:00
/** compare to a c string
* @ js NA
*/
2012-06-14 14:56:21 +08:00
int compare ( const char * ) const ;
2013-09-13 11:41:20 +08:00
/** append additional characters at the end of its current value
* @ js NA
* @ lua NA
*/
2013-07-10 09:50:13 +08:00
void append ( const std : : string & str ) ;
2013-09-13 11:41:20 +08:00
/** append(w/ format) additional characters at the end of its current value
* @ js NA
* @ lua NA
*/
2013-07-10 09:50:13 +08:00
void appendWithFormat ( const char * format , . . . ) ;
2013-09-13 11:41:20 +08:00
/** split a string
* @ js NA
* @ lua NA
*/
2013-07-14 16:56:34 +08:00
Array * componentsSeparatedByString ( const char * delimiter ) ;
2013-09-13 11:41:20 +08:00
/* override functions
* @ js NA
*/
2013-06-20 14:13:12 +08:00
virtual bool isEqual ( const Object * pObject ) ;
2012-04-19 14:35:52 +08:00
2012-09-17 15:02:24 +08:00
/** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer.
2013-06-20 14:13:12 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2012-06-14 16:05:58 +08:00
* it means that you needn ' t do a release operation unless you retain it .
2013-09-13 11:41:20 +08:00
* @ js NA
2012-06-14 16:05:58 +08:00
*/
2013-06-20 14:13:12 +08:00
static String * create ( const std : : string & str ) ;
2012-06-14 16:05:58 +08:00
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
2013-06-20 14:13:12 +08:00
* if you want to change it , you should modify the kMaxStringLen macro in String . cpp file .
* @ return A String pointer which is an autorelease object pointer ,
2012-06-14 16:05:58 +08:00
* it means that you needn ' t do a release operation unless you retain it .
2013-09-13 11:41:20 +08:00
* @ js NA
*/
2013-06-20 14:13:12 +08:00
static String * createWithFormat ( const char * format , . . . ) CC_FORMAT_PRINTF ( 1 , 2 ) ;
2012-06-14 16:05:58 +08:00
/** create a string with binary data
2013-06-20 14:13:12 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2012-06-14 16:05:58 +08:00
* it means that you needn ' t do a release operation unless you retain it .
2013-09-13 11:41:20 +08:00
* @ js NA
2012-06-14 16:05:58 +08:00
*/
2013-06-20 14:13:12 +08:00
static String * createWithData ( const unsigned char * pData , unsigned long nLen ) ;
2012-06-14 16:05:58 +08:00
/** create a string with a file,
2013-06-20 14:13:12 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2012-06-14 16:05:58 +08:00
* it means that you needn ' t do a release operation unless you retain it .
2013-09-13 11:41:20 +08:00
* @ js NA
2012-06-14 16:05:58 +08:00
*/
2013-07-26 06:53:24 +08:00
static String * createWithContentsOfFile ( const char * filename ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
virtual void acceptVisitor ( DataVisitor & visitor ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-08 23:05:47 +08:00
virtual String * clone ( ) const ;
2012-04-19 14:35:52 +08:00
private :
/** only for internal use */
bool initWithFormatAndValist ( const char * format , va_list ap ) ;
public :
2013-06-15 14:03:30 +08:00
std : : string _string ;
2012-04-19 14:35:52 +08:00
} ;
2013-06-20 14:13:12 +08:00
struct StringCompare : public std : : binary_function < String * , String * , bool > {
2012-06-15 04:50:47 +08:00
public :
2013-06-20 14:13:12 +08:00
bool operator ( ) ( String * a , String * b ) const {
2012-06-15 04:50:47 +08:00
return strcmp ( a - > getCString ( ) , b - > getCString ( ) ) < 0 ;
}
} ;
2013-06-20 14:13:12 +08:00
# define StringMake(str) String::create(str)
# define ccs StringMake
2012-04-19 14:35:52 +08:00
2012-06-20 18:09:11 +08:00
// end of data_structure group
/// @}
2012-04-19 14:35:52 +08:00
NS_CC_END
2012-08-02 13:02:59 +08:00
# endif //__CCSTRING_H__