blob: 7bbe659568ad4d693ecf2225523a10b18d0065e0 (
plain)
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
|
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <algorithm>
namespace InferenceEngine {
/**
* @Brief iterator for accesing standard c-style null terminated strings withing c++ algorithms
* @tparam Char
*/
template<typename Char>
struct null_terminated_range_iterator : public std::iterator<std::forward_iterator_tag, Char> {
public:
null_terminated_range_iterator() = delete;
// make a non-end iterator (well, unless you pass nullptr ;)
explicit null_terminated_range_iterator(Char *ptr) : ptr(ptr) {}
bool operator != (null_terminated_range_iterator const &that) const {
// iterators are equal if they point to the same location
return !(operator==(that));
}
bool operator == (null_terminated_range_iterator const &that) const {
// iterators are equal if they point to the same location
return ptr == that.ptr
// or if they are both end iterators
|| (is_end() && that.is_end());
}
null_terminated_range_iterator<Char> &operator++() {
get_accessor()++;
return *this;
}
null_terminated_range_iterator<Char> &operator++(int) {
return this->operator++();
}
Char &operator*() {
return *get_accessor();
}
protected:
Char *& get_accessor() {
if (ptr == nullptr) {
throw std::logic_error("null_terminated_range_iterator dereference: pointer is zero");
}
return ptr;
}
bool is_end() const {
// end iterators can be created by the default ctor
return !ptr
// or by advancing until a null character
|| !*ptr;
}
Char *ptr;
};
template<typename Char>
struct null_terminated_range_iterator_end : public null_terminated_range_iterator<Char> {
public:
// make an end iterator
null_terminated_range_iterator_end() : null_terminated_range_iterator<Char>(nullptr) {
null_terminated_range_iterator<Char>::ptr = nullptr;
}
};
inline null_terminated_range_iterator<const char> null_terminated_string(const char *a) {
return null_terminated_range_iterator<const char>(a);
}
inline null_terminated_range_iterator<const char> null_terminated_string_end() {
return null_terminated_range_iterator_end<const char>();
}
} // namespace InferenceEngine
|