Fail2abuseipdb
A simple application for converting fail2ban's jail output to an abuseipdb-compatible CSV
include
string_splitter.hpp
Go to the documentation of this file.
1
/**
2
* @file string_splitter.hpp
3
* @author Simon Cahill (simon@simonc.eu)
4
* @brief Contains the full implementation of an iterator-based string splitter based on an answer provided by StackOverflow user Calmarius.
5
* @version 0.1
6
* @date 2022-10-14
7
*
8
* @copyright Copyright (c) 2022 Simon Cahill and Contributors
9
*/
10
11
#
ifndef
FAIL2ABUSEIPDB_INCLUDE_STRING_SPLITTER_HPP
12
#
define
FAIL2ABUSEIPDB_INCLUDE_STRING_SPLITTER_HPP
13
14
#
include
<
string
>
15
16
using
std::string;
17
using
std::string_view;
18
19
/**
20
* @brief Splits a string into tokens, usable in an iterator loop, based on a delimiter.
21
*/
22
struct
StringSplit
{
23
24
/**
25
* @brief Simple implementation of an iterator usable in a iterative for-loop.
26
*/
27
struct
Iterator
{
28
private
:
29
size_t
m_tokenStart
= 0;
30
size_t
m_tokenEnd
= 0;
31
string
m_input
;
32
string_view
m_inputView
;
33
string
m_delimiter
;
34
bool
m_done
=
false
;
35
36
public
:
37
/**
38
* @brief Constructs an empty iterator which indicates the end of an iterator.
39
*/
40
Iterator
() {
m_done
=
true
; }
41
42
/**
43
* @brief Constructs a new instance of this iterator.
44
*
45
* @param input The input string.
46
* @param delimiter The delimiter to split the string by.
47
*/
48
Iterator
(
const
string& input,
const
string& delimiter):
49
m_input
(std::move(input)),
m_inputView
(
m_input
),
m_delimiter
(std::move(delimiter)) {
50
m_tokenEnd = m_inputView.find(m_delimiter, m_tokenStart);
51
}
52
53
/**
54
* @brief Dereference operator overload.
55
*
56
* @return string_view Returns a substring of the input marked by the current iterator position.
57
*/
58
string_view
operator
*() {
return
m_inputView.substr(m_tokenStart, m_tokenEnd - m_tokenStart); }
59
60
/**
61
* @brief Increments the iterator's position by 1.
62
*
63
* @return Iterator& A reference to the current instance.
64
*/
65
Iterator
&
operator
++() {
66
if
(
m_tokenEnd
== string::npos) {
67
m_done
=
true
;
68
return
*
this
;
69
}
70
71
m_tokenStart
=
m_tokenEnd
+
m_delimiter
.size();
72
m_tokenEnd = m_inputView.find(m_delimiter, m_tokenStart);
73
return
*
this
;
74
}
75
76
/**
77
* @brief Not-equal operator overload. Determines whether two @see Iterator instances are inequal.
78
*
79
* @param other A const reference to a different @see Iterator.
80
*
81
* @return true If the two instances are inequal.
82
* @return false Otherwise.
83
*
84
* @remarks This only checks if both iterators point to the end of the iterator!
85
*/
86
bool
operator
!=(
const
Iterator
& other) {
return
m_done
&& other
.
m_done
; }
87
};
88
89
private
:
90
Iterator
m_iteratorBeginning
;
//!< The beginning iterator
91
92
public
:
93
/**
94
* @brief Constructs a new instance of @see StringSplit
95
*
96
* @param input The input string to be split into tokens.
97
* @param delim The delimiter by which to split the strings.
98
*/
99
StringSplit
(
const
string& input,
const
string& delim):
m_iteratorBeginning
{
std
::
move
(
input
),
std
::
move
(
delim
)} { }
100
101
/**
102
* @brief Gets the beginning of the iterator.
103
*
104
* @return Iterator The beginning iterator (position)
105
*/
106
Iterator
begin
() {
return
m_iteratorBeginning; }
107
108
/**
109
* @brief Returns the end of the iterator.
110
*
111
* @return Iterator The end iterator.
112
*/
113
Iterator
end
() {
return
Iterator
{
}
; }
114
};
115
116
#
endif
// FAIL2ABUSEIPDB_INCLUDE_STRING_SPLITTER_HPP
Generated by
1.9.1