summaryrefslogtreecommitdiff
path: root/src/core/common/WhitespaceHandler.hpp
blob: 1935c2487453d4ed0d2a1a3ab0503edc758dc1f2 (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
    Ousía
    Copyright (C) 2014  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file WhitespaceHandler.hpp
 *
 * Contains the WhitespaceHandler classes which are used in multiple places to
 * trim, compact or preserve whitespaces while at the same time maintaining the
 * position information associated with the input strings.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_WHITESPACE_HANDLER_HPP_
#define _OUSIA_WHITESPACE_HANDLER_HPP_

#include <string>
#include <vector>

#include "WhitespaceHandler.hpp"

namespace ousia {

/**
 * WhitespaceHandler is a based class that can be used to collect text on a
 * character-by-character basis. Note that this class and its descendants are
 * hoped to be inlined by the compiler (and used in conjunction with templates),
 * thus they are fully defined inside this header.
 */
class WhitespaceHandler {
public:
	/**
	 * Start position of the extracted text.
	 */
	size_t textStart;

	/**
	 * End position of the extracted text.
	 */
	size_t textEnd;

	/**
	 * Buffer containing the extracted text.
	 */
	std::vector<char> textBuf;

	/**
	 * Constructor of the TextHandlerBase base class. Initializes the start and
	 * end position with zeros.
	 */
	WhitespaceHandler() : textStart(0), textEnd(0) {}

	/**
	 * Returns true if this whitespace handler has found any text and a text
	 * token could be emitted.
	 *
	 * @return true if the internal data buffer is non-empty.
	 */
	bool hasText() { return !textBuf.empty(); }

	/**
	 * Returns the content of the WhitespaceHandler as string.
	 */
	std::string toString()
	{
		return std::string(textBuf.data(), textBuf.size());
	}
};

/**
 * The PreservingWhitespaceHandler class preserves all characters unmodified,
 * including whitepace characters.
 */
class PreservingWhitespaceHandler : public WhitespaceHandler {
public:
	/**
	 * Appends the given character to the internal text buffer, does not
	 * eliminate whitespace.
	 *
	 * @param c is the character that should be appended to the internal buffer.
	 * @param start is the start byte offset of the given character.
	 * @param end is the end byte offset of the given character.
	 */
	void append(char c, size_t start, size_t end)
	{
		if (textBuf.empty()) {
			textStart = start;
		}
		textEnd = end;
		textBuf.push_back(c);
	}
};

/**
 * The TrimmingTextHandler class trims all whitespace characters at the begin
 * and the end of a text section but leaves all other characters unmodified,
 * including whitepace characters.
 */
class TrimmingWhitespaceHandler : public WhitespaceHandler {
public:
	/**
	 * Buffer used internally to temporarily store all whitespace characters.
	 * They are only added to the output buffer if another non-whitespace
	 * character is reached.
	 */
	std::vector<char> whitespaceBuf;

	/**
	 * Appends the given character to the internal text buffer, eliminates
	 * whitespace characters at the begin and end of the text.
	 *
	 * @param c is the character that should be appended to the internal buffer.
	 * @param start is the start byte offset of the given character.
	 * @param end is the end byte offset of the given character.
	 */
	void append(char c, size_t start, size_t end)
	{
		// Handle whitespace characters
		if (Utils::isWhitespace(c)) {
			if (!textBuf.empty()) {
				whitespaceBuf.push_back(c);
			}
			return;
		}

		// Set the start and end offset correctly
		if (textBuf.empty()) {
			textStart = start;
		}
		textEnd = end;

		// Store the character
		if (!whitespaceBuf.empty()) {
			textBuf.insert(textBuf.end(), whitespaceBuf.begin(),
			               whitespaceBuf.end());
			whitespaceBuf.clear();
		}
		textBuf.push_back(c);
	}
};

/**
 * The CollapsingTextHandler trims characters at the beginning and end of the
 * text and reduced multiple whitespace characters to a single blank.
 */
class CollapsingWhitespaceHandler : public WhitespaceHandler {
public:
	/**
	 * Flag set to true if a whitespace character was reached.
	 */
	bool hasWhitespace = false;

	/**
	 * Appends the given character to the internal text buffer, eliminates
	 * redundant whitespace characters.
	 *
	 * @param c is the character that should be appended to the internal buffer.
	 * @param start is the start byte offset of the given character.
	 * @param end is the end byte offset of the given character.
	 */
	void append(char c, size_t start, size_t end)
	{
		// Handle whitespace characters
		if (Utils::isWhitespace(c)) {
			if (!textBuf.empty()) {
				hasWhitespace = true;
			}
			return;
		}

		// Set the start and end offset correctly
		if (textBuf.empty()) {
			textStart = start;
		}
		textEnd = end;

		// Store the character
		if (hasWhitespace) {
			textBuf.push_back(' ');
			hasWhitespace = false;
		}
		textBuf.push_back(c);
	}
};

/**
 * Function that can be used to append the given buffer (e.g. a string or a
 * vector) to the whitespace handler.
 *
 * @tparam WhitespaceHandler is one of the WhitespaceHandler classes.
 * @tparam Buffer is an iterable type.
 * @param handler is the handler to which the characters of the Buffer should be
 * appended.
 * @param buf is the buffer from which the characters should be read.
 * @param start is the start byte offset. Each character is counted as one byte.
 */
template <typename WhitespaceHandler, typename Buffer>
inline void appendToWhitespaceHandler(WhitespaceHandler &handler, Buffer buf,
                                      size_t start)
{
	for (auto elem : buf) {
		handler.append(elem, start++);
	}
}
}

#endif /* _OUSIA_WHITESPACE_HANDLER_HPP_ */