Replace &range[0] with range.data() where appropriate

I had been under the impression that .data() on containers returns a const pointer and so hadn't been using it too much. I was wrong; I think that may be limited to std::basic_string. EDIT: I've checked, even std::basic_string has a non-const .data() since C++17, nice.
This commit is contained in:
Tamás Bálint Misius 2024-08-18 13:05:31 +02:00
parent ba0a9fa2a5
commit 1854bc25f0
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
23 changed files with 88 additions and 83 deletions

View File

@ -46,7 +46,7 @@ void Client::MigrateStampsDef()
}
for (auto i = 0; i < int(data.size()); i += 10)
{
stampIDs.push_back(ByteString(&data[0] + i, &data[0] + i + 10));
stampIDs.push_back(ByteString(data.data() + i, data.data() + i + 10));
}
}

View File

@ -439,7 +439,12 @@ void GameSave::readOPS(const std::vector<char> &data)
Renderer::PopulateTables();
unsigned char *inputData = (unsigned char*)&data[0], *partsData = NULL, *partsPosData = NULL, *fanData = NULL, *wallData = NULL, *soapLinkData = NULL;
auto *inputData = reinterpret_cast<const unsigned char *>(data.data());
unsigned char *partsData = nullptr;
unsigned char *partsPosData = nullptr;
unsigned char *fanData = nullptr;
unsigned char *wallData = nullptr;
unsigned char *soapLinkData = nullptr;
unsigned char *pressData = NULL, *vxData = NULL, *vyData = NULL, *ambientData = NULL, *blockAirData = nullptr, *gravityData = nullptr;
unsigned int inputDataLen = data.size(), bsonDataLen = 0, partsDataLen, partsPosDataLen, fanDataLen, wallDataLen, soapLinkDataLen;
unsigned int pressDataLen, vxDataLen, vyDataLen, ambientDataLen, blockAirDataLen, gravityDataLen;
@ -1314,7 +1319,7 @@ void GameSave::readPSv(const std::vector<char> &dataVec)
auto &builtinGol = SimulationData::builtinGol;
Renderer::PopulateTables();
unsigned char * saveData = (unsigned char *)&dataVec[0];
auto *saveData = reinterpret_cast<const unsigned char *>(dataVec.data());
auto dataLength = int(dataVec.size());
int q,p=0, pty, ty, legacy_beta=0;
Vec2<int> blockP = { 0, 0 };
@ -1388,7 +1393,7 @@ void GameSave::readPSv(const std::vector<char> &dataVec)
}
setSize(blockS);
const auto *data = reinterpret_cast<unsigned char *>(&bsonData[0]);
const auto *data = reinterpret_cast<const unsigned char *>(bsonData.data());
dataLength = bsonData.size();
if constexpr (DEBUG)
@ -1509,7 +1514,7 @@ void GameSave::readPSv(const std::vector<char> &dataVec)
}
if (j)
{
memset(&particles[0]+k, 0, sizeof(Particle));
memset(&particles[k], 0, sizeof(Particle));
particles[k].type = j;
if (j == PT_COAL)
particles[k].tmp = 50;
@ -1920,7 +1925,7 @@ void GameSave::readPSv(const std::vector<char> &dataVec)
throw ParseException(ParseException::Corrupt, "Not enough data at line " MTOS(__LINE__) " in " MTOS(__FILE__));
if(l>254)
l = 254;
memcpy(tempSignText, &data[0]+p, l);
memcpy(tempSignText, &data[p], l);
tempSignText[l] = 0;
p += l;
}
@ -1981,14 +1986,14 @@ std::pair<bool, std::vector<char>> GameSave::serialiseOPS() const
std::vector<unsigned char> ambientData(blockSize.X*blockSize.Y*2, 0);
std::vector<unsigned char> blockAirData(blockSize.X * blockSize.Y * 2);
PlaneAdapter<PlaneBase<unsigned char>> blockAirDataPlane (blockSize, std::in_place, &blockAirData[0] );
PlaneAdapter<PlaneBase<unsigned char>> blockAirhDataPlane(blockSize, std::in_place, &blockAirData[0] + blockSize.X * blockSize.Y);
PlaneAdapter<PlaneBase<unsigned char>> blockAirDataPlane (blockSize, std::in_place, blockAirData.data() );
PlaneAdapter<PlaneBase<unsigned char>> blockAirhDataPlane(blockSize, std::in_place, blockAirData.data() + blockSize.X * blockSize.Y);
std::vector<unsigned char> gravityData(blockSize.X * blockSize.Y * 4 * sizeof(float));
PlaneAdapter<PlaneBase<float >> massDataPlane (blockSize, std::in_place, reinterpret_cast<float *>(&gravityData[0] ));
PlaneAdapter<PlaneBase<uint32_t>> maskDataPlane (blockSize, std::in_place, reinterpret_cast<uint32_t *>(&gravityData[0] + blockSize.X * blockSize.Y * sizeof(float)));
PlaneAdapter<PlaneBase<float >> forceXDataPlane(blockSize, std::in_place, reinterpret_cast<float *>(&gravityData[0] + 2 * blockSize.X * blockSize.Y * sizeof(float)));
PlaneAdapter<PlaneBase<float >> forceYDataPlane(blockSize, std::in_place, reinterpret_cast<float *>(&gravityData[0] + 3 * blockSize.X * blockSize.Y * sizeof(float)));
PlaneAdapter<PlaneBase<float >> massDataPlane (blockSize, std::in_place, reinterpret_cast<float *>(gravityData.data() ));
PlaneAdapter<PlaneBase<uint32_t>> maskDataPlane (blockSize, std::in_place, reinterpret_cast<uint32_t *>(gravityData.data() + blockSize.X * blockSize.Y * sizeof(float)));
PlaneAdapter<PlaneBase<float >> forceXDataPlane(blockSize, std::in_place, reinterpret_cast<float *>(gravityData.data() + 2 * blockSize.X * blockSize.Y * sizeof(float)));
PlaneAdapter<PlaneBase<float >> forceYDataPlane(blockSize, std::in_place, reinterpret_cast<float *>(gravityData.data() + 3 * blockSize.X * blockSize.Y * sizeof(float)));
unsigned int wallDataLen = blockSize.X*blockSize.Y, fanDataLen = 0, pressDataLen = 0, vxDataLen = 0, vyDataLen = 0, ambientDataLen = 0;
@ -2115,7 +2120,7 @@ std::pair<bool, std::vector<char>> GameSave::serialiseOPS() const
unsigned int partsDataLen = 0;
std::vector<unsigned> partsSaveIndex(NPART);
unsigned int partsCount = 0;
std::fill(&partsSaveIndex[0], &partsSaveIndex[0] + NPART, 0);
std::fill(partsSaveIndex.data(), partsSaveIndex.data() + NPART, 0);
auto &sd = SimulationData::CRef();
auto &elements = sd.elements;
std::set<int> paletteSet;
@ -2539,7 +2544,7 @@ std::pair<bool, std::vector<char>> GameSave::serialiseOPS() const
bson_append_int(&b, "pmapbits", pmapbits);
if (partsDataLen)
{
bson_append_binary(&b, "parts", (char)BSON_BIN_USER, (const char *)&partsData[0], partsDataLen);
bson_append_binary(&b, "parts", (char)BSON_BIN_USER, reinterpret_cast<const char *>(partsData.data()), partsDataLen);
if (paletteData.size())
{
@ -2552,33 +2557,33 @@ std::pair<bool, std::vector<char>> GameSave::serialiseOPS() const
}
if (partsPosDataLen)
bson_append_binary(&b, "partsPos", (char)BSON_BIN_USER, (const char *)&partsPosData[0], partsPosDataLen);
bson_append_binary(&b, "partsPos", (char)BSON_BIN_USER, reinterpret_cast<const char *>(partsPosData.data()), partsPosDataLen);
}
if (hasWallData)
bson_append_binary(&b, "wallMap", (char)BSON_BIN_USER, (const char *)wallData.data(), wallDataLen);
if (fanDataLen)
bson_append_binary(&b, "fanMap", (char)BSON_BIN_USER, (const char *)&fanData[0], fanDataLen);
bson_append_binary(&b, "fanMap", (char)BSON_BIN_USER, reinterpret_cast<const char *>(fanData.data()), fanDataLen);
if (hasPressure && pressDataLen)
bson_append_binary(&b, "pressMap", (char)BSON_BIN_USER, (const char*)&pressData[0], pressDataLen);
bson_append_binary(&b, "pressMap", (char)BSON_BIN_USER, reinterpret_cast<const char *>(pressData.data()), pressDataLen);
if (hasPressure && vxDataLen)
bson_append_binary(&b, "vxMap", (char)BSON_BIN_USER, (const char*)&vxData[0], vxDataLen);
bson_append_binary(&b, "vxMap", (char)BSON_BIN_USER, reinterpret_cast<const char *>(vxData.data()), vxDataLen);
if (hasPressure && vyDataLen)
bson_append_binary(&b, "vyMap", (char)BSON_BIN_USER, (const char*)&vyData[0], vyDataLen);
bson_append_binary(&b, "vyMap", (char)BSON_BIN_USER, reinterpret_cast<const char *>(vyData.data()), vyDataLen);
if (hasAmbientHeat && this->aheatEnable && ambientDataLen)
bson_append_binary(&b, "ambientMap", (char)BSON_BIN_USER, (const char*)&ambientData[0], ambientDataLen);
bson_append_binary(&b, "ambientMap", (char)BSON_BIN_USER, reinterpret_cast<const char *>(ambientData.data()), ambientDataLen);
if (soapLinkDataLen)
bson_append_binary(&b, "soapLinks", (char)BSON_BIN_USER, (const char *)&soapLinkData[0], soapLinkDataLen);
bson_append_binary(&b, "blockAir", (char)BSON_BIN_USER, (const char *)blockAirData.data(), blockAirData.size());
bson_append_binary(&b, "soapLinks", (char)BSON_BIN_USER, reinterpret_cast<const char *>(soapLinkData.data()), soapLinkDataLen);
bson_append_binary(&b, "blockAir", (char)BSON_BIN_USER, reinterpret_cast<const char *>(blockAirData.data()), blockAirData.size());
if (ensureDeterminism)
{
bson_append_bool(&b, "ensureDeterminism", ensureDeterminism);
bson_append_long(&b, "frameCount", int64_t(frameCount));
bson_append_binary(&b, "rngState", (char)BSON_BIN_USER, (const char *)&rngState, sizeof(rngState));
bson_append_binary(&b, "rngState", (char)BSON_BIN_USER, reinterpret_cast<const char *>(&rngState), sizeof(rngState));
RESTRICTVERSION(98, 0);
}
if (gravityEnable)
{
bson_append_binary(&b, "gravity", (char)BSON_BIN_USER, (const char *)gravityData.data(), gravityData.size());
bson_append_binary(&b, "gravity", (char)BSON_BIN_USER, reinterpret_cast<const char *>(gravityData.data()), gravityData.size());
}
unsigned int signsCount = 0;
for (size_t i = 0; i < signs.size(); i++)

View File

@ -77,7 +77,7 @@ namespace http
if (doOnce.allPems.size())
{
curl_blob blob;
blob.data = &doOnce.allPems[0];
blob.data = doOnce.allPems.data();
blob.len = doOnce.allPems.size();
blob.flags = CURL_BLOB_COPY;
HandleCURLcode(curl_easy_setopt(easy, CURLOPT_CAINFO_BLOB, &blob));

View File

@ -190,7 +190,7 @@ namespace http
new Blob([ HEAP8.slice($2, $2 + $3) ]),
UTF8ToString($4)
);
}, handle->id, field.name.c_str(), &field.value[0], field.value.size(), field.filename->c_str());
}, handle->id, field.name.c_str(), field.value.data(), field.value.size(), field.filename->c_str());
}
else
{
@ -212,7 +212,7 @@ namespace http
HEAP8.byteOffset + $1,
$2
);
}, handle->id, &stringData[0], stringData.size());
}, handle->id, stringData.data(), stringData.size());
}
if (handle->isPost)
{
@ -328,7 +328,7 @@ namespace http
EM_ASM({
let responseData = Module.emscriptenRequestManager.requests[$0].responseData;
writeArrayToMemory(new Int8Array(responseData), $1);
}, handle->id, &handle->responseData[0]);
}, handle->id, handle->responseData.data());
}
auto headerCount = EM_ASM_INT({
let responseHeaders = Module.emscriptenRequestManager.requests[$0].responseHeaders;

View File

@ -415,7 +415,7 @@ namespace http
// Hopefully this is what a NULL from curl_mime_addpart means.
HandleCURLcode(CURLE_OUT_OF_MEMORY);
}
HandleCURLcode(curl_mime_data(part, &field.value[0], field.value.size()));
HandleCURLcode(curl_mime_data(part, field.value.data(), field.value.size()));
HandleCURLcode(curl_mime_name(part, field.name.c_str()));
if (field.filename.has_value())
{
@ -431,7 +431,7 @@ namespace http
HandleCURLFORMcode(curl_formadd(&handle->curlPostFieldsFirst, &handle->curlPostFieldsLast,
CURLFORM_COPYNAME, field.name.c_str(),
CURLFORM_BUFFER, field.filename->c_str(),
CURLFORM_BUFFERPTR, &field.value[0],
CURLFORM_BUFFERPTR, field.value.data(),
CURLFORM_BUFFERLENGTH, field.value.size(),
CURLFORM_END));
}
@ -439,7 +439,7 @@ namespace http
{
HandleCURLFORMcode(curl_formadd(&handle->curlPostFieldsFirst, &handle->curlPostFieldsLast,
CURLFORM_COPYNAME, field.name.c_str(),
CURLFORM_PTRCONTENTS, &field.value[0],
CURLFORM_PTRCONTENTS, field.value.data(),
CURLFORM_CONTENTLEN, field.value.size(),
CURLFORM_END));
}
@ -450,7 +450,7 @@ namespace http
else if (std::holds_alternative<http::StringData>(postData) && std::get<http::StringData>(postData).size())
{
auto &stringData = std::get<http::StringData>(postData);
HandleCURLcode(curl_easy_setopt(handle->curlEasy, CURLOPT_POSTFIELDS, &stringData[0]));
HandleCURLcode(curl_easy_setopt(handle->curlEasy, CURLOPT_POSTFIELDS, stringData.data()));
HandleCURLcode(curl_easy_setopt(handle->curlEasy, CURLOPT_POSTFIELDSIZE_LARGE, curl_off_t(stringData.size())));
}
else if (handle->isPost)

View File

@ -64,11 +64,11 @@ namespace http
}
std::vector<char> pem(pemLength);
// actually get the data
if (!CryptBinaryToStringA(context->pbCertEncoded, context->cbCertEncoded, CRYPT_STRING_BASE64HEADER, &pem[0], &pemLength))
if (!CryptBinaryToStringA(context->pbCertEncoded, context->cbCertEncoded, CRYPT_STRING_BASE64HEADER, pem.data(), &pemLength))
{
return die("CryptBinaryToStringA failed");
}
allPems += ByteString(&pem[0], &pem[0] + pem.size() - 1); // buffer includes the zero terminator, omit that
allPems += ByteString(pem.data(), pem.data() + pem.size() - 1); // buffer includes the zero terminator, omit that
}
if (!allPems.size())
{
@ -86,7 +86,7 @@ namespace http
if (doOnce.allPems.size())
{
curl_blob blob;
blob.data = &doOnce.allPems[0];
blob.data = doOnce.allPems.data();
blob.len = doOnce.allPems.size();
blob.flags = CURL_BLOB_COPY;
HandleCURLcode(curl_easy_setopt(easy, CURLOPT_CAINFO_BLOB, &blob));

View File

@ -19,7 +19,7 @@ namespace Clipboard
changeCount = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
std::vector<char> saveData;
SerializeClipboard(saveData);
const auto *base = &saveData[0];
const auto *base = saveData.data();
auto size = saveData.size();
NSData *data = [NSData dataWithBytes:base length:size];
if (![pasteboard setData:data forType:format])

View File

@ -141,7 +141,7 @@ namespace Clipboard
auto bail = false;
std::vector<char> saveData;
SerializeClipboard(saveData);
if (fwrite(&saveData[0], 1, saveData.size(), handle) != saveData.size())
if (fwrite(saveData.data(), 1, saveData.size(), handle) != saveData.size())
{
std::cerr << "failed to set clipboard data: fwrite: " << strerror(errno) << std::endl;
bail = true;

View File

@ -58,7 +58,7 @@ bool ReadFile(std::vector<char> &fileData, ByteString filename)
if (f) f.seekg(0, std::ios::end);
if (f) fileData.resize(f.tellg());
if (f) f.seekg(0);
if (f && fileData.size()) f.read(&fileData[0], fileData.size());
if (f && fileData.size()) f.read(fileData.data(), fileData.size());
if (!f)
{
std::cerr << "ReadFile: " << filename << ": " << strerror(errno) << std::endl;
@ -85,7 +85,7 @@ bool WriteFile(const std::vector<char> &fileData, ByteString filename)
bool ok = false;
{
std::ofstream f(writeFileName, std::ios::binary);
if (f) f.write(&fileData[0], fileData.size());
if (f) f.write(fileData.data(), fileData.size());
ok = bool(f);
}
if (!ok)

View File

@ -27,12 +27,12 @@ ByteString ExecutableNameFirstApprox()
ByteString firstApproximation("?");
{
auto bufSize = uint32_t(firstApproximation.size());
auto ret = _NSGetExecutablePath(&firstApproximation[0], &bufSize);
auto ret = _NSGetExecutablePath(firstApproximation.data(), &bufSize);
if (ret == -1)
{
// Buffer not large enough; likely to happen since it's initially a single byte.
firstApproximation.resize(bufSize);
ret = _NSGetExecutablePath(&firstApproximation[0], &bufSize);
ret = _NSGetExecutablePath(firstApproximation.data(), &bufSize);
}
if (ret != 0)
{

View File

@ -42,9 +42,9 @@ ByteString ExecutableNameFirstApprox()
mib[3] = -1;
std::array<char, 1000> buf;
size_t cb = buf.size();
if (!sysctl(mib, 4, &buf[0], &cb, NULL, 0))
if (!sysctl(mib, 4, buf.data(), &cb, NULL, 0))
{
return ByteString(&buf[0], &buf[0] + cb);
return ByteString(buf.data(), buf.data() + cb);
}
}
#endif

View File

@ -28,7 +28,7 @@ void DoRestart()
ByteString ExecutableName()
{
auto firstApproximation = ExecutableNameFirstApprox();
auto rp = std::unique_ptr<char, decltype(std::free) *>(realpath(&firstApproximation[0], NULL), std::free);
auto rp = std::unique_ptr<char, decltype(std::free) *>(realpath(firstApproximation.data(), NULL), std::free);
if (!rp)
{
std::cerr << "realpath: " << errno << std::endl;

View File

@ -173,7 +173,7 @@ ByteString WinNarrow(const std::wstring &source)
return "";
}
std::string output(buffer_size, 0);
if (!WideCharToMultiByte(CP_UTF8, 0, source.c_str(), source.size(), &output[0], buffer_size, NULL, NULL))
if (!WideCharToMultiByte(CP_UTF8, 0, source.c_str(), source.size(), output.data(), buffer_size, NULL, NULL))
{
return "";
}
@ -188,7 +188,7 @@ std::wstring WinWiden(const ByteString &source)
return L"";
}
std::wstring output(buffer_size, 0);
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), source.size(), &output[0], buffer_size))
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), source.size(), output.data(), buffer_size))
{
return L"";
}
@ -201,7 +201,7 @@ ByteString ExecutableName()
while (true)
{
SetLastError(ERROR_SUCCESS);
if (!GetModuleFileNameW(NULL, &buf[0], DWORD(buf.size())))
if (!GetModuleFileNameW(NULL, buf.data(), DWORD(buf.size())))
{
std::cerr << "GetModuleFileNameW: " << GetLastError() << std::endl;
return "";
@ -212,7 +212,7 @@ ByteString ExecutableName()
}
buf.resize(buf.size() * 2);
}
return WinNarrow(&buf[0]); // Pass pointer to copy only up to the zero terminator.
return WinNarrow(buf.data()); // Pass pointer to copy only up to the zero terminator.
}
void DoRestart()

View File

@ -11,8 +11,8 @@ namespace Platform
std::optional<std::vector<String>> StackTrace()
{
std::array<void *, 100> buf;
auto used = backtrace(&buf[0], buf.size());
auto *strs = backtrace_symbols(&buf[0], used);
auto used = backtrace(buf.data(), buf.size());
auto *strs = backtrace_symbols(buf.data(), used);
Defer freeStrs([strs]() {
free(strs);
});

View File

@ -20,7 +20,7 @@ static std::optional<SymbolInfo> GetSymbolInfo(HANDLE process, uintptr_t offset)
{
DWORD64 displacement;
std::array<char, sizeof(SYMBOL_INFOW) + 1000> symbolData{};
auto &symbol = *reinterpret_cast<SYMBOL_INFOW *>(&symbolData[0]);
auto &symbol = *reinterpret_cast<SYMBOL_INFOW *>(symbolData.data());
symbol.SizeOfStruct = sizeof(symbol);
symbol.MaxNameLen = symbolData.size() - sizeof(symbol);
if (SymFromAddrW(process, offset, &displacement, &symbol))

View File

@ -29,9 +29,9 @@ static bool InitFontData()
}
int first = -1;
int last = -1;
char *begin = &fontDataBuf[0];
char *ptr = &fontDataBuf[0];
char *end = &fontDataBuf[0] + fontDataBuf.size();
char *begin = fontDataBuf.data();
char *ptr = fontDataBuf.data();
char *end = fontDataBuf.data() + fontDataBuf.size();
while (ptr != end)
{
if (ptr + 4 > end)

View File

@ -28,7 +28,7 @@ void FontEditor::ReadDataFile(ByteString dataFile)
file.seekg(0, std::ios_base::end);
std::vector<char> fileData(file.tellg());
file.seekg(0);
file.read(&fileData[0], fileData.size());
file.read(fileData.data(), fileData.size());
file.close();
std::vector<char> fontDataBuf;
@ -40,9 +40,9 @@ void FontEditor::ReadDataFile(ByteString dataFile)
}
int first = -1;
int last = -1;
char *begin = &fontDataBuf[0];
char *ptr = &fontDataBuf[0];
char *end = &fontDataBuf[0] + fontDataBuf.size();
char *begin = fontDataBuf.data();
char *ptr = fontDataBuf.data();
char *end = fontDataBuf.data() + fontDataBuf.size();
while (ptr != end)
{
if (ptr + 4 > end)

View File

@ -61,7 +61,7 @@ GameModel::GameModel():
sim->useLuaCallbacks = true;
ren = new Renderer(sim);
activeTools = &regularToolset[0];
activeTools = regularToolset.data();
std::fill(decoToolset.begin(), decoToolset.end(), nullptr);
std::fill(regularToolset.begin(), regularToolset.end(), nullptr);
@ -883,17 +883,17 @@ void GameModel::SetActiveMenu(int menuID)
if(menuID == SC_DECO)
{
if(activeTools != &decoToolset[0])
if(activeTools != decoToolset.data())
{
activeTools = &decoToolset[0];
activeTools = decoToolset.data();
notifyActiveToolsChanged();
}
}
else
{
if(activeTools != &regularToolset[0])
if(activeTools != regularToolset.data())
{
activeTools = &regularToolset[0];
activeTools = regularToolset.data();
notifyActiveToolsChanged();
}
}

View File

@ -93,7 +93,7 @@ private:
std::vector<char> res(uncompressedLength);
int dstate;
dstate = BZ2_bzBuffToBuffDecompress(&res[0], (unsigned *)&uncompressedLength, &data[8], data.size()-8, 0, 0);
dstate = BZ2_bzBuffToBuffDecompress(res.data(), (unsigned *)&uncompressedLength, &data[8], data.size()-8, 0, 0);
if (dstate)
{
return niceNotifyError(String::Build("Unable to decompress update: ", dstate));

View File

@ -307,7 +307,7 @@ AnyType CommandInterface::tptS_set(std::deque<String> * words)
AnyType value = eval(words);
Simulation * sim = m->GetSimulation();
unsigned char * partsBlock = (unsigned char*)&sim->parts[0];
unsigned char * partsBlock = reinterpret_cast<unsigned char *>(&sim->parts[0]);
int returnValue = 0;

View File

@ -156,7 +156,7 @@ namespace LuaSocket
CURLcode res = CURLE_OK;
if (!tcps->writeClosed)
{
res = curl_easy_send(tcps->easy, &data[0] + writtenTotal, len - writtenTotal, &writtenNow);
res = curl_easy_send(tcps->easy, data + writtenTotal, len - writtenTotal, &writtenNow);
}
writtenTotal += writtenNow;
if (writtenTotal >= len)
@ -250,7 +250,7 @@ namespace LuaSocket
}
else
{
res = curl_easy_recv(tcps->easy, &tcps->recvBuf[0] + readTotal, len - readTotal, &readNow);
res = curl_easy_recv(tcps->easy, tcps->recvBuf.data() + readTotal, len - readTotal, &readNow);
}
readTotal += readNow;
returning = readTotal;
@ -353,7 +353,7 @@ namespace LuaSocket
}
returning = curOut;
}
lua_pushlstring(L, &tcps->recvBuf[0], returning);
lua_pushlstring(L, tcps->recvBuf.data(), returning);
// * This copy makes ReceiveNoPrefix quadratic if there's a lot of stuff in
// the stash (as a result of a *very* long line being returned by an "*L"
// pattern and then whatever was left being stashed) and it's all *very*
@ -361,9 +361,9 @@ namespace LuaSocket
// of view of an "*L" pattern). Handling this edge case in a special,
// sub-quadratic way isn't worth the effort.
std::copy(
&tcps->recvBuf[0] + readTotal - tcps->stashedLen,
&tcps->recvBuf[0] + readTotal,
&tcps->recvBuf[0]
tcps->recvBuf.data() + readTotal - tcps->stashedLen,
tcps->recvBuf.data() + readTotal,
tcps->recvBuf.data()
);
return retn;
}

View File

@ -25,7 +25,7 @@ void Prefs::Read()
std::cerr << "no json data" << std::endl;
return;
}
if (!reader->parse(&data[0], &data[0] + data.size(), &root, &errs))
if (!reader->parse(data.data(), data.data() + data.size(), &root, &errs))
{
std::cerr << errs << std::endl;
return;

View File

@ -156,7 +156,7 @@ void FillHunkVectorPtr(const Item *oldItems, const Item *newItems, SnapshotDelta
template<class Item>
void FillHunkVector(const std::vector<Item> &oldItems, const std::vector<Item> &newItems, SnapshotDelta::HunkVector<Item> &out)
{
FillHunkVectorPtr<Item>(&oldItems[0], &newItems[0], out, std::min(oldItems.size(), newItems.size()));
FillHunkVectorPtr<Item>(oldItems.data(), newItems.data(), out, std::min(oldItems.size(), newItems.size()));
}
template<class Item>
@ -187,7 +187,7 @@ void ApplyHunkVectorPtr(const SnapshotDelta::HunkVector<Item> &in, Item *items)
template<bool UseOld, class Item>
void ApplyHunkVector(const SnapshotDelta::HunkVector<Item> &in, std::vector<Item> &items)
{
ApplyHunkVectorPtr<UseOld, Item>(in, &items[0]);
ApplyHunkVectorPtr<UseOld, Item>(in, items.data());
}
template<bool UseOld, class Item>
@ -221,12 +221,12 @@ std::unique_ptr<SnapshotDelta> SnapshotDelta::FromSnapshots(const Snapshot &oldS
FillSingleDiff(oldSnap.Authors , newSnap.Authors , delta.Authors );
FillSingleDiff(oldSnap.FrameCount , newSnap.FrameCount , delta.FrameCount );
FillSingleDiff(oldSnap.RngState , newSnap.RngState , delta.RngState );
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(&oldSnap.PortalParticles[0]), reinterpret_cast<const uint32_t *>(&newSnap.PortalParticles[0]), delta.PortalParticles, newSnap.PortalParticles.size() * ParticleUint32Count);
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(&oldSnap.stickmen[0]) , reinterpret_cast<const uint32_t *>(&newSnap.stickmen[0] ), delta.stickmen , newSnap.stickmen .size() * playerstUint32Count);
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(oldSnap.PortalParticles.data()), reinterpret_cast<const uint32_t *>(newSnap.PortalParticles.data()), delta.PortalParticles, newSnap.PortalParticles.size() * ParticleUint32Count);
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(oldSnap.stickmen.data()) , reinterpret_cast<const uint32_t *>(newSnap.stickmen.data() ), delta.stickmen , newSnap.stickmen .size() * playerstUint32Count);
// * Slightly more interesting; this will only diff the common parts, the rest is copied separately.
auto commonSize = std::min(oldSnap.Particles.size(), newSnap.Particles.size());
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(&oldSnap.Particles[0]), reinterpret_cast<const uint32_t *>(&newSnap.Particles[0]), delta.commonParticles, commonSize * ParticleUint32Count);
FillHunkVectorPtr(reinterpret_cast<const uint32_t *>(oldSnap.Particles.data()), reinterpret_cast<const uint32_t *>(newSnap.Particles.data()), delta.commonParticles, commonSize * ParticleUint32Count);
delta.extraPartsOld.resize(oldSnap.Particles.size() - commonSize);
std::copy(oldSnap.Particles.begin() + commonSize, oldSnap.Particles.end(), delta.extraPartsOld.begin());
delta.extraPartsNew.resize(newSnap.Particles.size() - commonSize);
@ -257,11 +257,11 @@ std::unique_ptr<Snapshot> SnapshotDelta::Forward(const Snapshot &oldSnap)
ApplySingleDiff<false>(Authors , newSnap.Authors );
ApplySingleDiff<false>(FrameCount , newSnap.FrameCount );
ApplySingleDiff<false>(RngState , newSnap.RngState );
ApplyHunkVectorPtr<false>(PortalParticles, reinterpret_cast<uint32_t *>(&newSnap.PortalParticles[0]));
ApplyHunkVectorPtr<false>(stickmen , reinterpret_cast<uint32_t *>(&newSnap.stickmen[0] ));
ApplyHunkVectorPtr<false>(PortalParticles, reinterpret_cast<uint32_t *>(newSnap.PortalParticles.data()));
ApplyHunkVectorPtr<false>(stickmen , reinterpret_cast<uint32_t *>(newSnap.stickmen.data() ));
// * Slightly more interesting; apply the common hunk vector, copy the extra portion separaterly.
ApplyHunkVectorPtr<false>(commonParticles, reinterpret_cast<uint32_t *>(&newSnap.Particles[0]));
ApplyHunkVectorPtr<false>(commonParticles, reinterpret_cast<uint32_t *>(newSnap.Particles.data()));
auto commonSize = oldSnap.Particles.size() - extraPartsOld.size();
newSnap.Particles.resize(commonSize + extraPartsNew.size());
std::copy(extraPartsNew.begin(), extraPartsNew.end(), newSnap.Particles.begin() + commonSize);
@ -291,11 +291,11 @@ std::unique_ptr<Snapshot> SnapshotDelta::Restore(const Snapshot &newSnap)
ApplySingleDiff<true>(Authors , oldSnap.Authors );
ApplySingleDiff<true>(FrameCount , oldSnap.FrameCount );
ApplySingleDiff<true>(RngState , oldSnap.RngState );
ApplyHunkVectorPtr<true>(PortalParticles, reinterpret_cast<uint32_t *>(&oldSnap.PortalParticles[0]));
ApplyHunkVectorPtr<true>(stickmen , reinterpret_cast<uint32_t *>(&oldSnap.stickmen[0] ));
ApplyHunkVectorPtr<true>(PortalParticles, reinterpret_cast<uint32_t *>(oldSnap.PortalParticles.data()));
ApplyHunkVectorPtr<true>(stickmen , reinterpret_cast<uint32_t *>(oldSnap.stickmen.data() ));
// * Slightly more interesting; apply the common hunk vector, copy the extra portion separaterly.
ApplyHunkVectorPtr<true>(commonParticles, reinterpret_cast<uint32_t *>(&oldSnap.Particles[0]));
ApplyHunkVectorPtr<true>(commonParticles, reinterpret_cast<uint32_t *>(oldSnap.Particles.data()));
auto commonSize = newSnap.Particles.size() - extraPartsNew.size();
oldSnap.Particles.resize(commonSize + extraPartsOld.size());
std::copy(extraPartsOld.begin(), extraPartsOld.end(), oldSnap.Particles.begin() + commonSize);