bool PyUnicodeUCS2::resize(size_t newsize) { if (mStr != NULL) mStr = (wchar_t*)ASCENT_REALLOC(mStr,(newsize+1) * 2); else mStr = (wchar_t*)ASCENT_MALLOC((newsize+1) * 2); mStrLen = newsize; if (mStr == NULL) { mStrLen = 0; return false; } return true; }
bool PyString::set(const char* str, size_t len) { if (str == NULL || len == 0) return false; if (mStr != NULL) mStr = static_cast<char*>(ASCENT_REALLOC(mStr, len+1)); else mStr = static_cast<char*>(ASCENT_MALLOC(len+1)); mStrLen = len; if (mStr == NULL) return false; ASCENT_MEMCPY(mStr, str, mStrLen); mStr[mStrLen] = '\0'; return true; }
/* the trick is that we can also append a string to a empty string. */ PyString& PyString::append( const std::string& str, size_t pos, size_t n ) { // we are appending a empty string if(mStrLen == 0) { if (n > str.size()) n = str.size(); // we can use the set function as we are empty (void)set(str.c_str(), n); return *this; } else // we aren't a empty string... complicated { size_t newSize = mStrLen + n; assert(mStr); // we should have a existing piece of memory mStr = (char*)ASCENT_REALLOC(mStr, newSize + 1); // realloc... it may move memory around... /* this would mean a huge fail */ if (mStr == NULL) return *this; // the point where its added char* crossA = reinterpret_cast<char*>(&mStr[pos]); // the point where it sends char* crossB = reinterpret_cast<char*>(&mStr[pos + n]); // chunk new_size size_t crossSize = mStrLen - pos; // first move that chunk memcpy(crossB,crossA, crossSize); // append the string memcpy(crossA,str.c_str(), n); mStrLen = newSize; mStr[mStrLen] = '\0'; return *this; } }
bool PyString::resize( size_t new_size ) { /* handling rare conditions separately */ if (mStr != NULL && new_size == 0) { SafeFree(mStr); mStrLen = new_size; return true; } mStrLen = new_size; if (mStr != NULL) mStr = static_cast<char*>(ASCENT_REALLOC(mStr, mStrLen+1)); else mStr = static_cast<char*>(ASCENT_MALLOC(mStrLen+1)); if (mStr == NULL) return false; mStr[mStrLen] = '\0'; return true; }