File: byte_string_view.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (111 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
### jsoncons::byte_string_view

```cpp
#include <jsoncons/utility/byte_string.hpp>

class byte_string_view;
```
A `byte_string_view` refers to a constant contiguous sequence of byte-like objects with the first element of the sequence at position zero.
It  holds two members: a pointer to constant `uint8_t*` and a size.

#### Member types

Member type                         |Definition
------------------------------------|------------------------------
`const_iterator`|
`iterator`|Same as `const_iterator`
`size_type`|`std::size_t`
`value_type`|`uint8_t`
`reference`|`uint8_t&`
`const_reference`|`const uint8_t&`
`difference_type`|`std::ptrdiff_t`
`pointer`|`uint8_t*`
`const_pointer`|`const uint8_t*`

#### Constructor

    constexpr byte_string_view() noexcept;

    constexpr byte_string_view(const uint8_t* data, std::size_t length) noexcept;

    template <typename Container>
    constexpr explicit byte_string_view(const Container& cont); 

    constexpr byte_string_view(const byte_string_view&) noexcept = default;

    constexpr byte_string_view(byte_string_view&& other) noexcept;

#### Assignment

    byte_string_view& operator=(const byte_string_view& s) noexcept = default;

    byte_string_view& operator=(byte_string_view&& s) noexcept;

#### Iterators

    constexpr const_iterator begin() const noexcept;

    constexpr const_iterator end() const noexcept;

    constexpr const_iterator cbegin() const noexcept;

    constexpr const_iterator cend() const noexcept;

#### Element access

    constexpr const uint8_t* data() const noexcept;

    constexpr uint8_t operator[](size_type pos) const; 

    constexpr byte_string_view substr(size_type pos) const;

    constexpr byte_string_view substr(size_type pos, size_type n) const;

#### Capacity

    constexpr size_t size() const noexcept;

#### Non-member functions

    bool operator==(const byte_string_view& lhs, const byte_string_view& rhs);

    bool operator!=(const byte_string_view& lhs, const byte_string_view& rhs);

    template <typename CharT>
    friend std::ostream& operator<<(std::ostream& os, const byte_string_view& o);

### Examples

#### Display bytes

```cpp
std::vector<uint8_t> v = {'H','e','l','l','o',' ','W','o','r','l','d'};

std::cout << "(1) " << byte_string_view(v) << "\n\n";

std::cout << "(2) " << byte_string_view(v).substr(0, 5) << "\n\n";
```

Output:
```
(1) 48,65,6c,6c,6f,20,57,6f,72,6c,64

(2) 48,65,6c,6c,6f
```

#### Display bytes from std::string

```cpp
std::string s = {'H','e','l','l','o',' ','W','o','r','l','d'};

std::cout << "(1) " << byte_string_view(s) << "\n\n";

std::cout << "(2) " << byte_string_view(s).substr(0, 5) << "\n\n";
```

Output:
```
(1) 48,65,6c,6c,6f,20,57,6f,72,6c,64

(2) 48,65,6c,6c,6f
```