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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "CCArray.h"
|
2014-07-14 20:45:24 +08:00
|
|
|
#include "base/ccUtils.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define kMaxStringLen (1024*100)
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String::__String()
|
2013-10-12 15:25:45 +08:00
|
|
|
:_string("")
|
|
|
|
{}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String::__String(const char * str)
|
2013-10-12 15:25:45 +08:00
|
|
|
:_string(str)
|
|
|
|
{}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String::__String(const std::string& str)
|
2013-10-12 15:25:45 +08:00
|
|
|
:_string(str)
|
|
|
|
{}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String::__String(const __String& str)
|
2013-10-12 15:25:45 +08:00
|
|
|
:_string(str.getCString())
|
|
|
|
{}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String::~__String()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
CCLOGINFO("deallocing __String: %p", this);
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
_string.clear();
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String& __String::operator= (const __String& other)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-02 17:54:52 +08:00
|
|
|
if (this != &other) {
|
|
|
|
_string = other._string;
|
|
|
|
}
|
2013-10-12 15:25:45 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
bool __String::initWithFormatAndValist(const char* format, va_list ap)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
char* pBuf = (char*)malloc(kMaxStringLen);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pBuf != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
vsnprintf(pBuf, kMaxStringLen, format, ap);
|
|
|
|
_string = pBuf;
|
|
|
|
free(pBuf);
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
bool __String::initWithFormat(const char* format, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
_string.clear();
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
bRet = initWithFormatAndValist(format, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
int __String::intValue() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return atoi(_string.c_str());
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
unsigned int __String::uintValue() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (unsigned int)atoi(_string.c_str());
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
float __String::floatValue() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2014-07-14 20:45:24 +08:00
|
|
|
return (float)utils::atof(_string.c_str());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
double __String::doubleValue() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
2014-07-14 20:45:24 +08:00
|
|
|
return utils::atof(_string.c_str());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
bool __String::boolValue() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == strcmp(_string.c_str(), "0") || 0 == strcmp(_string.c_str(), "false"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
const char* __String::getCString() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return _string.c_str();
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
int __String::length() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return static_cast<int>(_string.length());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
int __String::compare(const char * pStr) const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
return strcmp(getCString(), pStr);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
void __String::append(const std::string& str)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
_string.append(str);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
void __String::appendWithFormat(const char* format, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
char* pBuf = (char*)malloc(kMaxStringLen);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pBuf != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
vsnprintf(pBuf, kMaxStringLen, format, ap);
|
|
|
|
_string.append(pBuf);
|
|
|
|
free(pBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__Array* __String::componentsSeparatedByString(const char *delimiter)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-07 14:26:52 +08:00
|
|
|
__Array* result = __Array::create();
|
2013-12-27 00:26:02 +08:00
|
|
|
std::string strTmp = _string;
|
2013-12-06 16:32:06 +08:00
|
|
|
size_t cutAt;
|
2013-12-27 00:26:02 +08:00
|
|
|
while( (cutAt = strTmp.find_first_of(delimiter)) != strTmp.npos )
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if(cutAt > 0)
|
|
|
|
{
|
2013-12-27 00:26:02 +08:00
|
|
|
result->addObject(__String::create(strTmp.substr(0, cutAt)));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
2013-12-27 00:26:02 +08:00
|
|
|
strTmp = strTmp.substr(cutAt + 1);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:26:09 +08:00
|
|
|
if(!strTmp.empty())
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-27 00:26:02 +08:00
|
|
|
result->addObject(__String::create(strTmp));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
bool __String::isEqual(const Ref* pObject)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-12-11 17:15:27 +08:00
|
|
|
const __String* pStr = dynamic_cast<const __String*>(pObject);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pStr != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
if (0 == _string.compare(pStr->_string))
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* __String::create(const std::string& str)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* ret = new __String(str);
|
2013-11-06 09:36:44 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 16:48:12 +08:00
|
|
|
__String* __String::createWithData(const unsigned char* data, size_t nLen)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
__String* ret = nullptr;
|
|
|
|
if (data != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
char* pStr = (char*)malloc(nLen+1);
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pStr != nullptr)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pStr[nLen] = '\0';
|
|
|
|
if (nLen > 0)
|
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
memcpy(pStr, data, nLen);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
ret = __String::create(pStr);
|
2013-10-12 15:25:45 +08:00
|
|
|
free(pStr);
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 09:36:44 +08:00
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* __String::createWithFormat(const char* format, ...)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* ret = __String::create("");
|
2013-10-12 15:25:45 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2013-11-06 09:36:44 +08:00
|
|
|
ret->initWithFormatAndValist(format, ap);
|
2013-10-12 15:25:45 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2013-11-06 09:36:44 +08:00
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 05:38:41 +08:00
|
|
|
__String* __String::createWithContentsOfFile(const std::string &filename)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-18 14:58:17 +08:00
|
|
|
std::string str = FileUtils::getInstance()->getStringFromFile(filename);
|
|
|
|
return __String::create(std::move(str));
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
void __String::acceptVisitor(DataVisitor &visitor)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
__String* __String::clone() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
return __String::create(_string);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:11:38 +08:00
|
|
|
namespace StringUtils {
|
|
|
|
|
|
|
|
std::string format(const char* format, ...)
|
|
|
|
{
|
|
|
|
#define CC_MAX_STRING_LENGTH (1024*100)
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
char* buf = (char*)malloc(CC_MAX_STRING_LENGTH);
|
|
|
|
if (buf != nullptr)
|
|
|
|
{
|
|
|
|
vsnprintf(buf, CC_MAX_STRING_LENGTH, format, ap);
|
|
|
|
ret = buf;
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace StringUtils {
|
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
NS_CC_END
|