本文整理汇总了C++中std::string::reserve方法的典型用法代码示例。如果您正苦于以下问题:C++ string::reserve方法的具体用法?C++ string::reserve怎么用?C++ string::reserve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::string
的用法示例。
在下文中一共展示了string::reserve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cbc_encrypt
bool CCCrypto::cbc_encrypt(const char *pBegin, const char *pEnd, block_encrypt_func encrypt_func, int iBlockSize, void *ctx, std::string &sResult)
{
unsigned char buf[32] = { 0 };
if (iBlockSize < 8 || iBlockSize > (int)sizeof(buf))
{
return false;
}
int iDataSize = pEnd - pBegin;
int rem = iDataSize % iBlockSize;
if (rem == 0)
{
sResult.reserve(sResult.size() + iDataSize);
}
else
{
sResult.reserve(sResult.size() + iDataSize - rem + iBlockSize + 1);
}
for (const char *r = pBegin; r < pEnd; r += iBlockSize)
{
int len = std::min(iBlockSize, (int)(pEnd - r));
for (int i = 0; i < len; ++i)
{
buf[i] = *(r + i) ^ buf[i];
}
encrypt_func(ctx, buf, buf);
sResult.append(buf, buf + iBlockSize);
}
if (rem != 0)
{
sResult.append(1, rem);
}
return true;
}
示例2: loadBmpURL
bool BitmapHandler::loadBmpURL(const std::string &name, std::string &data, bool useCache)
{
AmigoRest restClient(amigoCoreAgent.c_str());
restClient.enableCacheWrite(useCache);
RestClient::response response = restClient.get(name);
if (!response.isBad())
{
data.reserve(response.body.size());
data.insert(0, response.body.c_str(), response.body.size());
} else
{
if(useCache)
{
restClient.enableCacheRead(true);
response = restClient.get(name);
if (!response.isBad())
{
data.reserve(response.body.size());
data.insert(0, response.body.c_str(), response.body.size());
return true;
}
}
AMIGO_LOG_I(TAG, "::loadBmpURL() FILED to load '%s'\n", name.c_str());
return false;
}
return true;
}
示例3: FileToString
int FileToString( const char* filename, std::string& out_contents )
{
FILE *fp;
fp = fopen(filename,"r");
if (!fp)
{
printf("Unable to open file '%s' for read into string (%s, %d)\n",filename, __FILE__, __LINE__);
return 1;
}
const size_t capacity_increment = 512;
try
{
out_contents.clear();
out_contents.reserve(capacity_increment);
for (int this_char = fgetc(fp); this_char != EOF; this_char = fgetc(fp))
{
//embiggenate the buffer, if needed
if(out_contents.capacity() == out_contents.size())
{
out_contents.reserve(out_contents.capacity() + capacity_increment);
}
out_contents.push_back(static_cast<char>(this_char));
}
}
catch (const std::exception& e)
{
printf("caught exception '%s' while reading file '%s' into string (%s, %d)\n",e.what(), filename, __FILE__, __LINE__);
return 1;
}
return 0;
}
示例4: cbc_decrypt
bool CCCrypto::cbc_decrypt(const char *pBegin, const char *pEnd, block_decrypt_func decrypt_func, int iBlockSize, void *ctx, std::string &sResult)
{
unsigned char buf[32] = { 0 };
if (iBlockSize < 8 || iBlockSize > (int)sizeof(buf))
{
return false;
}
int iDataSize = pEnd - pBegin;
int rem = iDataSize % iBlockSize;
if (rem == 0)
{
sResult.reserve(sResult.size() + iDataSize);
}
else if (rem == 1)
{
if (iDataSize < iBlockSize + 1)
{
return false;
}
rem = *(pEnd - 1);
if (rem <= 0 || rem >= iBlockSize)
{
return false;
}
sResult.reserve(sResult.size() + iDataSize - iBlockSize - 1 + rem);
--pEnd;
}
else
{
return false;
}
for (const char *r = pBegin; r < pEnd; r += iBlockSize)
{
decrypt_func(ctx, r, buf);
if (r != pBegin)
{
for (int i = 0; i < iBlockSize; ++i)
{
buf[i] ^= *(r - iBlockSize + i);
}
}
if (rem != 0 && r + iBlockSize >= pEnd)
{
sResult.append(buf, buf + rem);
}
else
{
sResult.append(buf, buf + iBlockSize);
}
}
return true;
}
示例5: load_file
void load_file(std::string& s, std::istream& is)
{
s.erase();
if(is.bad()) return;
s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
char c;
while(is.get(c))
{
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}
示例6: load_file1
void load_file1(std::string& s, std::istream& is)
{
s.erase(); //删除字符串
//bad() 如果出现错误则返回true
if(is.bad()) return;
s.reserve(is.rdbuf()->in_avail()); //reserve 预留空间
char c;
while(is.get(c))
{ //capacity 在不重新分配内存的情况下,字符串可能的大小 size 得到字符串的大小
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c); //append 追加字符
}
}
示例7: MakeMessagePOST
void WSRequest::MakeMessagePOST(std::string& msg) const
{
char buf[32];
size_t content_len = m_contentData.size();
msg.clear();
msg.reserve(256);
msg.append("POST ").append(m_service_url).append(" HTTP/1.1\r\n");
sprintf(buf, "%u", m_port);
msg.append("Host: ").append(m_server).append(":").append(buf).append("\r\n");
msg.append("Connection: keep-alive\r\n");
if (m_accept != CT_NONE)
msg.append("Accept: ").append(MimeFromContentType(m_accept)).append("\r\n");
msg.append("Accept-Charset: ").append(m_charset).append("\r\n");
if (content_len)
{
sprintf(buf, "%lu", (unsigned long)content_len);
msg.append("Content-Type: ").append(MimeFromContentType(m_contentType));
msg.append("; charset=" REQUEST_STD_CHARSET "\r\n");
msg.append("Content-Length: ").append(buf).append("\r\n\r\n");
msg.append(m_contentData);
}
else
msg.append("\r\n");
}
示例8: encodequotedstring
void encodequotedstring(std::string& str)
{
std::string tmp;
tmp.swap( str );
const char* s = tmp.c_str();
str.reserve( tmp.size() + 2 );
str += "\"";
while( *s )
{
char ch = *s++;
switch( ch )
{
case '\\':
str += "\\\\";
break;
case '\"':
str += "\\\"";
break;
case '\n':
str += "\\n";
break;
default:
str += ch;
break;
}
}
str += "\"";
}
示例9: MakeEncryptedPackage
/*
encryptedPackage = [uint64_t:encData]
*/
inline void MakeEncryptedPackage(std::string& encryptedPackage, const std::string& encData)
{
encryptedPackage.reserve(encData.size() + 8);
encryptedPackage.resize(8);
cybozu::Set64bitAsLE(&encryptedPackage[0], encData.size());
encryptedPackage += encData;
}
示例10: string_by_replacing_double_curly_braces_from_file
int string::string_by_replacing_double_curly_braces_from_file(std::string& out,
std::string& replacement_warnings,
const char* filename,
replacement replacement) {
out.clear();
// ----------------------------------------
// Validate parameters.
if (!filename) {
return -1;
}
// ----------------------------------------
std::ifstream istream(filename);
if (!istream) {
return -1;
}
// ----------------------------------------
// Get length of file and call string.reserve with file length.
istream.seekg(0, std::ios::end);
out.reserve(static_cast<size_t>(istream.tellg()));
istream.seekg(0, std::ios::beg);
// ----------------------------------------
return string_by_replacing_double_curly_braces_(out, replacement_warnings, istream, replacement);
}
示例11: decodeStandardEncryption
inline bool decodeStandardEncryption(std::string& dec, const std::string& encryptedPackage, const EncryptionInfo& info, const std::string& pass, std::string secretKey)
{
const EncryptionHeader& header = info.seHeader;
const EncryptionVerifier& verifier = info.seVerifier;
if (secretKey.empty()) {
if (!verifyStandardEncryption(secretKey, header, verifier, pass)) {
return false;
}
}
if (isDebug()) {
printf("secretKey = "); dump(secretKey, false);
}
const char *p = encryptedPackage.data();
size_t decSize = cybozu::Get32bitAsLE(p);
p += 8;
const size_t dataSize = encryptedPackage.size();
if (decSize > dataSize) {
throw cybozu::Exception("ms:decodeStandardEncryption:bad decSize") << decSize << dataSize;
}
const size_t blockSize = 4096;
dec.reserve(dataSize);
const size_t n = (dataSize + blockSize - 1) / blockSize;
const std::string iv;
for (size_t i = 0; i < n; i++) {
const size_t len = (i < n - 1) ? blockSize : (dataSize % blockSize);
dec.append(cipher(header.cipherName, p + i * blockSize, len, secretKey, iv, cybozu::crypto::Cipher::Decoding));
}
dec.resize(decSize);
return true;
}
示例12: Read
///////////////////////////////////////////////////////////////////////////////
// Descripcion:
// - Lee desde un archivo en binario una cadena de caracteres. Siempre seguiran
// el formato:
// * Tamaño de la cadena (word)
// * Caracteres que forman la cadena (un sbyte por cada uno)
// Parametros:
// - hFile. Handle al fichero.
// - udOffset. Offset de donde comenzar a leer.
// - szDestStr. Referencia al string donde depositar los datos
// Devuelve:
// - Cantidad de datos leidos del archivo.
// Notas:
// - Siempre se liberara el contenido de la cadena recibida
///////////////////////////////////////////////////////////////////////////////
dword
CFileSystem::ReadStringFromBinary(const FileDefs::FileHandle& hFile,
const dword udOffset,
std::string& szDestStr)
{
// Lee el tamaño de la cadena
word uwStrSize;
word udDataReaded = Read(hFile, (sbyte *)(&uwStrSize), sizeof(word), udOffset);
// ¿Hay cadena asociada?
szDestStr = "";
if (uwStrSize > 0) {
// Se reserva espacio y procede a leer byte a byte
szDestStr.reserve(uwStrSize);
word uwIt = 0;
for (; uwIt < uwStrSize; ++uwIt) {
sbyte sbChar;
udDataReaded += Read(hFile, &sbChar, sizeof(sbyte), udOffset + udDataReaded);
szDestStr += sbChar;
}
}
// Retorna
return udDataReaded;
}
示例13: SetUp
virtual void SetUp()
{
const ModelTestParameters& params = GetParam();
std::ifstream in(params.file.c_str());
ASSERT_TRUE(!!in);
in.seekg(0, std::ios::end);
original_xml.reserve(in.tellg());
in.seekg(0, std::ios::beg);
original_xml.assign(std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>());
if (std::string("2015") != OME_XML_MODEL_VERSION)
{
// Apply upgrade if needed.
ome::common::xsl::Platform xslplat;
ome::common::xml::dom::Document upgraded_doc;
ome::common::xml::dom::Element docroot;
ome::xml::OMEEntityResolver entity_resolver;
ome::xml::OMETransformResolver transform_resolver;
std::string upgraded_xml;
ome::xml::transform(OME_XML_MODEL_VERSION, original_xml, upgraded_xml,
entity_resolver, transform_resolver);
doc = ome::xml::createDocument(upgraded_xml);
}
else
{
doc = ome::xml::createDocument(original_xml);
}
}
示例14: switch
void
Logs::format (std::string& output, std::string const& message,
beast::Journal::Severity severity, std::string const& partition)
{
output.reserve (message.size() + partition.size() + 100);
output = boost::posix_time::to_simple_string (
boost::posix_time::second_clock::universal_time ());
output += boost::str( boost::format(" <%X>") % std::this_thread::get_id());
if (! partition.empty ())
output += partition + ":";
switch (severity)
{
case beast::Journal::kTrace: output += "TRC "; break;
case beast::Journal::kDebug: output += "DBG "; break;
case beast::Journal::kInfo: output += "NFO "; break;
case beast::Journal::kWarning: output += "WRN "; break;
case beast::Journal::kError: output += "ERR "; break;
default:
assert(false);
case beast::Journal::kFatal: output += "FTL "; break;
}
output += scrub (message);
if (output.size() > maximumMessageCharacters)
{
output.resize (maximumMessageCharacters - 3);
output += "...";
}
}
示例15: xEscapeAndAppend
void NotationClass::xEscapeAndAppend(std::string &strAppendTo, const std::string &strSrc){
strAppendTo.reserve(strAppendTo.size() + strSrc.size() * 2);
for(auto iter = strSrc.cbegin(); iter != strSrc.cend(); ++iter){
const char ch = *iter;
switch(ch){
case '\\':
case '=':
case '{':
case '}':
case ';':
strAppendTo.push_back('\\');
strAppendTo.push_back(ch);
break;
case '\n':
strAppendTo.push_back('\\');
strAppendTo.push_back('n');
break;
case '\b':
strAppendTo.push_back('\\');
strAppendTo.push_back('b');
break;
case '\r':
strAppendTo.push_back('\\');
strAppendTo.push_back('r');
break;
case '\t':
strAppendTo.push_back('\\');
strAppendTo.push_back('t');
break;
default:
strAppendTo.push_back(ch);
break;
}
}
}