Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ts/MemView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ svtoi(StringView src, StringView *out, int base)

intmax_t zret = 0;

if (*out)
if (out)
out->clear();
if (!(1 < base && base <= 36))
return 0;
Expand Down
36 changes: 24 additions & 12 deletions lib/ts/MemView.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ class MemView
);

/** Construct from a half open range of two pointers.
@note The byte at @start is in the view but the byte at @a end is not.
@note The instance at @start is in the view but the instance at @a end is not.
*/
constexpr MemView(const void *start, ///< First byte in the view.
const void *end ///< First byte not in the view.
template <typename T>
constexpr MemView(T const *start, ///< First byte in the view.
T const *end ///< First byte not in the view.
);

/** Construct from a half open range of two pointers.
@note The instance at @start is in the view but the instance at @a end is not.
*/
MemView(void const *start, ///< First byte in the view.
void const *end ///< First byte not in the view.
);

/** Construct from nullptr.
This implicitly makes the length 0.
*/
Expand Down Expand Up @@ -305,7 +313,8 @@ class StringView
explicit StringView(const char *s);

/// Construct from @c MemView to reference the same view.
constexpr StringView(MemView const &that);
/// @internal Can't be @c constexpr because @c static_cast of @c <void*> is not permitted.
StringView(MemView const &that);

/// Construct from @c std::string, referencing the entire string contents.
StringView(std::string const &str);
Expand Down Expand Up @@ -595,8 +604,13 @@ inline constexpr MemView::MemView()
inline constexpr MemView::MemView(void const *ptr, size_t n) : _ptr(ptr), _size(n)
{
}
inline constexpr MemView::MemView(void const *start, void const *end)
: _ptr(start), _size(static_cast<const char *>(end) - static_cast<const char *>(start))
template <typename T> constexpr MemView::MemView(const T *start, const T *end) : _ptr(start), _size((end - start) * sizeof(T))
{
}
// <void*> is magic, handle that specially.
// No constexpr because the spec specifically forbids casting from <void*> to a typed pointer.
inline MemView::MemView(void const *start, void const *end)
: _ptr(start), _size(static_cast<const char *>(end) - static_cast<char const *>(start))
{
}
inline constexpr MemView::MemView(std::nullptr_t) : _ptr(nullptr), _size(0)
Expand Down Expand Up @@ -820,7 +834,7 @@ inline StringView::StringView(const char *s) : _ptr(s), _size(strlen(s))
inline constexpr StringView::StringView(std::nullptr_t) : _ptr(nullptr), _size(0)
{
}
inline constexpr StringView::StringView(MemView const &that) : _ptr(static_cast<const char *>(that.ptr())), _size(that.size())
inline StringView::StringView(MemView const &that) : _ptr(static_cast<const char *>(that.ptr())), _size(that.size())
{
}
inline StringView::StringView(std::string const &str) : _ptr(str.data()), _size(str.size())
Expand Down Expand Up @@ -1055,24 +1069,22 @@ StringView::suffix(const char *p) const
return zret;
}

// gcc 4.9 - it considers passing this->find(...) to suffix() to be amibugous between the const char*
// overload and the std::function<bool (char)>. This shows up on Debian 7, so let's try a cast to help out.
inline auto
StringView::suffix(char c) -> self
{
return this->suffix(static_cast<const char *>(this->find(c)));
return this->suffix(this->find(c));
}

inline auto
StringView::suffix(self delimiters) -> self
{
return this->suffix(static_cast<const char *>(this->find(delimiters)));
return this->suffix(this->find(delimiters));
}

inline auto
StringView::suffix(std::function<bool(char)> const &pred) -> self
{
return this->suffix(static_cast<const char *>(this->find(pred)));
return this->suffix(this->find(pred));
}

inline StringView
Expand Down
12 changes: 12 additions & 0 deletions lib/ts/test_MemView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ Test_1()
return true;
}

// These tests are purely compile time.
void
Test_Compile()
{
int i[12];
char c[29];
void *x = i, *y = i + 12;
MemView mvi(i, i + 12);
MemView mci(c, c + 29);
MemView mcv(x, y);
}

int
main(int, char *argv[])
{
Expand Down