2013-10-12 15:25:45 +08:00
/****************************************************************************
2014-01-07 11:47:11 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
Copyright ( c ) 2013 - 2014 Chukong Technologies
2013-10-12 15:25:45 +08:00
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__
# if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
# include <string.h>
# endif
# include <stdarg.h>
# include <string>
# include <functional>
2013-12-11 14:39:21 +08:00
# include <sstream>
2014-02-20 10:53:49 +08:00
# include "CCArray.h"
2014-04-27 01:11:22 +08:00
# include "base/CCRef.h"
2013-10-12 15:25:45 +08:00
NS_CC_BEGIN
/**
* @ addtogroup data_structures
* @ {
*/
2014-02-20 10:53:49 +08:00
class CC_DLL __String : public Ref , public Clonable
2013-10-12 15:25:45 +08:00
{
public :
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
__String ( ) ;
2013-10-12 15:25:45 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
__String ( const char * str ) ;
2013-10-12 15:25:45 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
__String ( const std : : string & str ) ;
2013-10-12 15:25:45 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
__String ( const __String & str ) ;
2013-10-12 15:25:45 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
virtual ~ __String ( ) ;
2013-10-12 15:25:45 +08:00
/* override assignment operator
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
__String & operator = ( const __String & other ) ;
2013-10-12 15:25:45 +08:00
/** init a string with format, it's similar with the c function 'sprintf'
* @ js NA
* @ lua NA
*/
bool initWithFormat ( const char * format , . . . ) CC_FORMAT_PRINTF ( 2 , 3 ) ;
/** convert to int value
* @ js NA
*/
int intValue ( ) const ;
/** convert to unsigned int value
* @ js NA
*/
unsigned int uintValue ( ) const ;
/** convert to float value
* @ js NA
*/
float floatValue ( ) const ;
/** convert to double value
* @ js NA
*/
double doubleValue ( ) const ;
/** convert to bool value
* @ js NA
*/
bool boolValue ( ) const ;
/** get the C string
* @ js NA
*/
const char * getCString ( ) const ;
/** get the length of string
* @ js NA
*/
2013-12-06 16:32:06 +08:00
int length ( ) const ;
2013-10-12 15:25:45 +08:00
/** compare to a c string
* @ js NA
*/
int compare ( const char * ) const ;
/** append additional characters at the end of its current value
* @ js NA
* @ lua NA
*/
void append ( const std : : string & str ) ;
/** append(w/ format) additional characters at the end of its current value
* @ js NA
* @ lua NA
*/
void appendWithFormat ( const char * format , . . . ) ;
/** split a string
* @ js NA
* @ lua NA
*/
2013-12-07 10:48:02 +08:00
__Array * componentsSeparatedByString ( const char * delimiter ) ;
2013-10-12 15:25:45 +08:00
/* override functions
* @ js NA
*/
2014-02-20 10:53:49 +08:00
virtual bool isEqual ( const Ref * pObject ) ;
2013-10-12 15:25:45 +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-12-12 10:34:41 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2013-10-12 15:25:45 +08:00
* it means that you needn ' t do a release operation unless you retain it .
* @ js NA
*/
2013-12-11 17:15:27 +08:00
static __String * create ( const std : : string & str ) ;
2013-10-12 15:25:45 +08:00
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
2013-12-11 17:15:27 +08:00
* if you want to change it , you should modify the kMax__StringLen macro in __String . cpp file .
2013-12-12 10:34:41 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2013-10-12 15:25:45 +08:00
* it means that you needn ' t do a release operation unless you retain it .
* @ js NA
*/
2013-12-11 17:15:27 +08:00
static __String * createWithFormat ( const char * format , . . . ) CC_FORMAT_PRINTF ( 1 , 2 ) ;
2013-10-12 15:25:45 +08:00
/** create a string with binary data
2013-12-12 10:34:41 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2013-10-12 15:25:45 +08:00
* it means that you needn ' t do a release operation unless you retain it .
* @ js NA
*/
2014-03-20 16:48:12 +08:00
static __String * createWithData ( const unsigned char * pData , size_t nLen ) ;
2013-10-12 15:25:45 +08:00
/** create a string with a file,
2013-12-12 10:34:41 +08:00
* @ return A String pointer which is an autorelease object pointer ,
2013-10-12 15:25:45 +08:00
* it means that you needn ' t do a release operation unless you retain it .
* @ js NA
*/
2014-05-08 05:38:41 +08:00
static __String * createWithContentsOfFile ( const std : : string & filename ) ;
2013-10-12 15:25:45 +08:00
/**
* @ js NA
* @ lua NA
*/
virtual void acceptVisitor ( DataVisitor & visitor ) ;
/**
* @ js NA
* @ lua NA
*/
2013-12-11 17:15:27 +08:00
virtual __String * clone ( ) const ;
2013-10-12 15:25:45 +08:00
private :
/** only for internal use */
bool initWithFormatAndValist ( const char * format , va_list ap ) ;
public :
std : : string _string ;
} ;
2013-12-11 17:15:27 +08:00
struct StringCompare : public std : : binary_function < __String * , __String * , bool > {
2013-10-12 15:25:45 +08:00
public :
2013-12-11 17:15:27 +08:00
bool operator ( ) ( __String * a , __String * b ) const {
2013-10-12 15:25:45 +08:00
return strcmp ( a - > getCString ( ) , b - > getCString ( ) ) < 0 ;
}
} ;
# define StringMake(str) String::create(str)
2013-12-11 17:15:27 +08:00
# define ccs StringMake
2013-10-12 15:25:45 +08:00
2014-05-08 11:11:38 +08:00
namespace StringUtils {
template < typename T >
std : : string toString ( T arg )
2013-12-11 14:39:21 +08:00
{
2014-05-08 11:11:38 +08:00
std : : stringstream ss ;
ss < < arg ;
return ss . str ( ) ;
}
2014-07-15 12:00:31 +08:00
std : : string CC_DLL format ( const char * format , . . . ) CC_FORMAT_PRINTF ( 1 , 2 ) ;
2013-12-11 17:15:27 +08:00
2014-05-08 11:11:38 +08:00
} // namespace StringUtils {
2013-12-11 14:39:21 +08:00
2013-10-12 15:25:45 +08:00
// end of data_structure group
/// @}
NS_CC_END
# endif //__CCSTRING_H__