libzypp 17.38.8
ByteArray.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPP_BYTEARRAY_H
10#define ZYPP_BYTEARRAY_H
11
12#include <vector>
13#include <cstring>
14#include <string>
15#include <string_view>
16#include <algorithm>
17
18namespace zypp {
19 class ByteArray : public std::vector<char>
20 {
21 public:
22 using vector<char>::vector;
23 explicit ByteArray ( const std::string &data ) : ByteArray( data.c_str(), data.length() ) { }
24 explicit ByteArray ( const char *data, const int len = -1 ) : ByteArray( data, data + (len == -1 ? strlen(data) : len) ) { }
25 std::string asString () const {
26 if ( size() == 0 )
27 return std::string();
28 return std::string( data(), size() );
29 }
30 std::string asString ( size_t maxsize_r ) const {
31 if ( size() == 0 )
32 return std::string();
33 return std::string( data(), std::min( maxsize_r, size() ) );
34 }
35
36#ifdef __cpp_lib_string_view
37 std::string_view asStringView () const {
38 if ( size() == 0 )
39 return std::string_view();
40 return std::string_view( data(), size() );
41 }
42#endif
43
44 static std::size_t maxSize () {
45 static const auto size = ByteArray().max_size();
46 return size;
47 }
48
49 };
50
51 class UByteArray : public std::vector<unsigned char>
52 {
53 public:
54 using vector<unsigned char>::vector;
55 explicit UByteArray ( const char *data, const int len = -1 ) : UByteArray( data, data + (len == -1 ? strlen(data) : len) ) { }
56
57 static std::size_t maxSize () {
58 static const auto size = UByteArray().max_size();
59 return size;
60 }
61 };
62}
63
64
65#endif
static std::size_t maxSize()
Definition ByteArray.h:44
ByteArray(const std::string &data)
Definition ByteArray.h:23
std::string asString() const
Definition ByteArray.h:25
std::string asString(size_t maxsize_r) const
Definition ByteArray.h:30
ByteArray(const char *data, const int len=-1)
Definition ByteArray.h:24
static std::size_t maxSize()
Definition ByteArray.h:57
UByteArray(const char *data, const int len=-1)
Definition ByteArray.h:55
Easy-to use interface to the ZYPP dependency resolver.