Enhanced CCString with two 'constructors' and a compare function.

This commit is contained in:
Nicolas Gramlich 2012-06-13 23:56:21 -07:00
parent 0c4a6be8ff
commit 8e67d3e2d6
2 changed files with 46 additions and 1 deletions

View File

@ -124,6 +124,11 @@ unsigned int CCString::length() const
return m_sString.length(); return m_sString.length();
} }
int CCString::compare(const char * pStr) const
{
return strcmp(getCString(), pStr);
}
CCObject* CCString::copyWithZone(CCZone* pZone) CCObject* CCString::copyWithZone(CCZone* pZone)
{ {
CCAssert(pZone == NULL, "CCString should not be inherited."); CCAssert(pZone == NULL, "CCString should not be inherited.");
@ -152,6 +157,31 @@ CCString* CCString::stringWithCString(const char* pStr)
return pRet; return pRet;
} }
CCString* CCString::stringWithString(const std::string& pStr)
{
CCString* pRet = new CCString(pStr);
pRet->autorelease();
return pRet;
}
CCString* CCString::stringWithCStringData(const char* pData, unsigned long nLen)
{
CCString* pRet = NULL;
if (pData != NULL)
{
char* pStr = (char*)malloc(nLen+1);
if (pStr != NULL)
{
pStr[nLen] = '\0';
memcpy(pStr, pData, nLen);
pRet = CCString::stringWithCString(pStr);
free(pStr);
}
}
return pRet;
}
CCString* CCString::stringWithData(unsigned char* pData, unsigned long nLen) CCString* CCString::stringWithData(unsigned char* pData, unsigned long nLen)
{ {
CCString* pRet = NULL; CCString* pRet = NULL;

View File

@ -66,6 +66,9 @@ public:
/** get the length of string */ /** get the length of string */
unsigned int length() const; unsigned int length() const;
/** compare to a c string */
int compare(const char *) const;
/* override functions */ /* override functions */
virtual CCObject* copyWithZone(CCZone* pZone); virtual CCObject* copyWithZone(CCZone* pZone);
virtual bool isEqual(const CCObject* pObject); virtual bool isEqual(const CCObject* pObject);
@ -77,6 +80,18 @@ public:
*/ */
static CCString* stringWithCString(const char* pStr); static CCString* stringWithCString(const char* pStr);
/** create a string with c string
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithCStringData(const char* pData, unsigned long nLen);
/** create a string with std::string
* @return A CCString pointer which is an autorelease object pointer,
* it means that you needn't do a release operation unless you retain it.
*/
static CCString* stringWithString(const std::string& str);
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes, /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
* if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file. * if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file.
* @return A CCString pointer which is an autorelease object pointer, * @return A CCString pointer which is an autorelease object pointer,