Commit 6ad622e8 authored by Vladimir Bespalov's avatar Vladimir Bespalov

Address array and vector storage through .data()

When used like &vec[0], there would be an assert in gnu std lib:

/usr/include/c++/8/bits/stl_vector.h:932: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type)
[with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::reference = char&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]:
Assertion '__builtin_expect(__n < this->size(), true)' failed.

on the other hand, using .data() is allowed even on empty uninited vectors

array<> access is modified for consistency.
parent 654abe92
...@@ -159,12 +159,12 @@ public: ...@@ -159,12 +159,12 @@ public:
} }
RawBuffer buffer() const { RawBuffer buffer() const {
return RawBuffer((const char *)data_.data(), pptr() - &data_[0]); return RawBuffer(data_.data(), pptr() - data_.data());
} }
void clear() { void clear() {
data_.clear(); // reset stream buffer to the whole backing storage.
this->setp(&data_[0], &data_[0] + data_.capacity()); this->setp(data_.data(), data_.data() + data_.size());
} }
protected: protected:
......
...@@ -142,7 +142,7 @@ template <typename T, size_t N> struct ViewBuilder<std::array<T, N>> { ...@@ -142,7 +142,7 @@ template <typename T, size_t N> struct ViewBuilder<std::array<T, N>> {
if (size > arr.size()) if (size > arr.size())
throw std::invalid_argument("out of bounds size"); throw std::invalid_argument("out of bounds size");
return View<T>(&arr[0], size); return View<T>(arr.data(), size);
} }
}; };
...@@ -170,7 +170,7 @@ template <typename T> struct ViewBuilder<std::vector<T>> { ...@@ -170,7 +170,7 @@ template <typename T> struct ViewBuilder<std::vector<T>> {
if (size > vec.size()) if (size > vec.size())
throw std::invalid_argument("out of bounds size"); throw std::invalid_argument("out of bounds size");
return View<T>(&vec[0], size); return View<T>(vec.data(), size);
} }
}; };
......
...@@ -28,7 +28,7 @@ static constexpr const char *UA = "pistache/0.1"; ...@@ -28,7 +28,7 @@ static constexpr const char *UA = "pistache/0.1";
namespace { namespace {
std::pair<StringView, StringView> splitUrl(const std::string &url) { std::pair<StringView, StringView> splitUrl(const std::string &url) {
RawStreamBuf<char> buf(const_cast<char *>(&url[0]), url.size()); RawStreamBuf<char> buf(const_cast<char *>(url.data()), url.size());
StreamCursor cursor(&buf); StreamCursor cursor(&buf);
match_string("http://", cursor); match_string("http://", cursor);
......
...@@ -95,7 +95,7 @@ void DynamicStreamBuf::reserve(size_t size) { ...@@ -95,7 +95,7 @@ void DynamicStreamBuf::reserve(size_t size) {
size = maxSize; size = maxSize;
const size_t oldSize = data_.size(); const size_t oldSize = data_.size();
data_.resize(size); data_.resize(size);
this->setp(&data_[0] + oldSize, &data_[0] + size); this->setp(data_.data() + oldSize, data_.data() + size);
} }
bool StreamCursor::advance(size_t count) { bool StreamCursor::advance(size_t count) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment