Skip to content

Commit 487cac6

Browse files
committed
Enhance byte_string_view
1 parent a997589 commit 487cac6

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

doc/ref/byte_string_view.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Member type |Definition
3131
template <class Container>
3232
constexpr explicit byte_string_view(const Container& cont);
3333

34-
byte_string_view(const byte_string_view&) noexcept = default;
34+
constexpr byte_string_view(const byte_string_view&) noexcept = default;
3535

36-
byte_string_view(byte_string_view&& other) noexcept;
36+
constexpr byte_string_view(byte_string_view&& other) noexcept;
3737

3838
#### Assignment
3939

@@ -43,19 +43,23 @@ Member type |Definition
4343

4444
#### Iterators
4545

46-
const_iterator begin() const noexcept;
46+
constexpr const_iterator begin() const noexcept;
4747

48-
const_iterator end() const noexcept;
48+
constexpr const_iterator end() const noexcept;
49+
50+
constexpr const_iterator cbegin() const noexcept;
51+
52+
constexpr const_iterator cend() const noexcept;
4953

5054
#### Element access
5155

5256
constexpr const uint8_t* data() const noexcept;
5357

54-
uint8_t operator[](size_type pos) const;
58+
constexpr uint8_t operator[](size_type pos) const;
5559

56-
byte_string_view substr(size_type pos) const;
60+
constexpr byte_string_view substr(size_type pos) const;
5761

58-
byte_string_view substr(size_type pos, size_type n) const;
62+
constexpr byte_string_view substr(size_type pos, size_type n) const;
5963

6064
#### Capacity
6165

include/jsoncons/byte_string.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ namespace jsoncons {
332332
using traits_type = byte_traits;
333333

334334
using const_iterator = const uint8_t*;
335-
using iterator = const uint8_t*;
335+
using iterator = const_iterator;
336336
using size_type = std::size_t;
337337
using value_type = uint8_t;
338338
using reference = uint8_t&;
@@ -363,11 +363,16 @@ namespace jsoncons {
363363

364364
constexpr byte_string_view(const byte_string_view&) noexcept = default;
365365

366-
byte_string_view(byte_string_view&& other) noexcept
366+
JSONCONS_CPP14_CONSTEXPR byte_string_view(byte_string_view&& other) noexcept
367367
: data_(nullptr), size_(0)
368368
{
369-
std::swap(data_, other.data_);
370-
std::swap(size_, other.size_);
369+
const_pointer temp_data = data_;
370+
data_ = other.data_;
371+
other.data_ = temp_data;
372+
373+
size_type temp_size = size_;
374+
size_ = other.size_;
375+
other.size_ = temp_size;
371376
}
372377

373378
byte_string_view& operator=(const byte_string_view&) = default;
@@ -396,21 +401,29 @@ namespace jsoncons {
396401
}
397402

398403
// iterator support
399-
const_iterator begin() const noexcept
404+
constexpr const_iterator begin() const noexcept
400405
{
401406
return data_;
402407
}
403-
const_iterator end() const noexcept
408+
constexpr const_iterator end() const noexcept
409+
{
410+
return data_ + size_;
411+
}
412+
constexpr const_iterator cbegin() const noexcept
413+
{
414+
return data_;
415+
}
416+
constexpr const_iterator cend() const noexcept
404417
{
405418
return data_ + size_;
406419
}
407420

408-
uint8_t operator[](size_type pos) const
421+
constexpr uint8_t operator[](size_type pos) const
409422
{
410423
return data_[pos];
411424
}
412425

413-
byte_string_view substr(size_type pos) const
426+
JSONCONS_CPP14_CONSTEXPR byte_string_view substr(size_type pos) const
414427
{
415428
if (pos > size_)
416429
{

tests/src/byte_string_tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ TEST_CASE("byte_string_view constructors")
110110
{
111111
std::vector<uint8_t> v = {'f','o','o','b','a','r'};
112112
byte_string_view bstr(v);
113+
CHECK(bstr[0] == 'f');
114+
CHECK(bstr[1] == 'o');
115+
CHECK(bstr[2] == 'o');
116+
CHECK(bstr[3] == 'b');
117+
CHECK(bstr[4] == 'a');
118+
CHECK(bstr[5] == 'r');
119+
120+
byte_string_view copied(bstr);
121+
CHECK(copied == bstr);
122+
123+
byte_string_view moved(std::move(bstr));
124+
CHECK(bstr.data() == nullptr);
125+
CHECK(bstr.size() == 0);
126+
127+
REQUIRE(moved.size() == 6);
128+
CHECK(moved[0] == 'f');
129+
CHECK(moved[1] == 'o');
130+
CHECK(moved[2] == 'o');
131+
CHECK(moved[3] == 'b');
132+
CHECK(moved[4] == 'a');
133+
CHECK(moved[5] == 'r');
134+
}
135+
}
136+
137+
TEST_CASE("byte_string_view iterators")
138+
{
139+
SECTION("begin/end")
140+
{
141+
std::vector<uint8_t> v = {'f','o','o'};
142+
byte_string_view bstr(v);
143+
144+
auto it = bstr.begin();
145+
REQUIRE(it != bstr.end());
146+
CHECK(*it++ == 'f');
147+
REQUIRE(it != bstr.end());
148+
CHECK(*it++ == 'o');
149+
REQUIRE(it != bstr.end());
150+
CHECK(*it++ == 'o');
151+
CHECK(it == bstr.end());
113152
}
114153
}
115154

0 commit comments

Comments
 (0)