File: byte_string.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 (125 lines) | stat: -rw-r--r-- 2,420 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
### jsoncons::byte_string

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

typedef basic_byte_string<Allocator = std::allocator<uint8_t>> byte_string;
```
The `byte_string` class is an instantiation of the `basic_byte_string` class template that uses `std::allocator<uint8_t>` as the allocator type.

#### Member types

Member type                         |Definition
------------------------------------|------------------------------
`const_iterator`|
`iterator`|Same as `const_iterator`
`size_type`|std::size_t

#### Constructor

    byte_string();

    explicit byte_string(const Allocator& alloc);

    byte_string(std::initializer_list<uint8_t> init);

    byte_string(std::initializer_list<uint8_t> init, const Allocator& alloc);

    explicit byte_string(const byte_string_view& v);

    byte_string(const byte_string_view& v, const Allocator& alloc);

    byte_string(const char* s);

    byte_string(const byte_string& s); 

    byte_string(byte_string&& s) noexcept; 

#### Assignment

    byte_string& operator=(const byte_string& s);

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

#### Iterators

    const_iterator begin() const noexcept;

    const_iterator end() const noexcept;

#### Element access

    const uint8_t* data() const;

    uint8_t operator[](size_type pos) const; 

    operator byte_string_view() const noexcept;

#### Capacity

    std::size_t size() const;

#### Non-member functions

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

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

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

### Examples

#### Byte string from initializer list

```cpp
json j(byte_string{'H','e','l','l','o'});
byte_string bytes = j.as<byte_string>();

std::cout << "(1) "<< bytes << "\n\n";

std::cout << "(2) ";
for (auto b : bytes)
{
    std::cout << (char)b;
}
std::cout << "\n\n";

std::cout << "(3) " << j << '\n';
```

Output:
```
(1) 48 65 6c 6c 6f

(2) Hello

(3) "SGVsbG8"
```

#### Byte string from char array

```cpp
json j(byte_string{'H','e','l','l','o'});
byte_string bytes = j.as<byte_string>();

std::cout << "(1) "<< bytes << "\n\n";

std::cout << "(2) ";
for (auto b : bytes)
{
    std::cout << (char)b;
}
std::cout << "\n\n";

std::cout << "(3) " << j << '\n';
```

Output:
```
(1) 48 65 6c 6c 6f

(2) Hello

(3) "SGVsbG8"
```