summaryrefslogtreecommitdiff
path: root/src/core/common/Utils.hpp
blob: 5332b5094fa54145eb631791276bbbf65fb49430 (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
/*
    Ousía
    Copyright (C) 2014, 2015  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/>.
*/

#ifndef _OUSIA_UTILS_H_
#define _OUSIA_UTILS_H_

#include <sstream>
#include <string>

namespace ousia {

class Utils {
public:
	/**
	 * Returns true if the given character is in [A-Za-z]
	 */
	static bool isAlphabetic(const char c)
	{
		return ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'));
	}

	/**
	 * Returns true if the given character is in [0-9]
	 */
	static bool isNumeric(const char c) { return (c >= '0') && (c <= '9'); }

	/**
	 * Returns true if the given character is in [0-9A-Fa-f]
	 */
	static bool isHexadecimal(const char c)
	{
		return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) ||
		       ((c >= 'a') && (c <= 'f'));
	}

	/**
	 * Returns true if the given character is in [A-Za-z0-9]
	 */
	static bool isAlphanumeric(const char c)
	{
		return isAlphabetic(c) || isNumeric(c);
	}

	/**
	 * Returns true if the given character is in [A-Za-z_][A-Za-z0-9_-]*
	 */
	static bool isIdentifier(const std::string &name);

	/**
	 * Returns true if the given character is a whitespace character.
	 */
	static bool isWhitespace(const char c)
	{
		return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
	}

	/**
	 * Removes whitespace at the beginning and the end of the given string.
	 */
	static std::string trim(const std::string &s);

	/**
	 * Turns the elements of a collection into a string separated by the
	 * given delimiter.
	 *
	 * @param es is an iterable container of elements that can be appended to an
	 * output stream (the << operator must be implemented).
	 * @param delim is the delimiter that should be used to separate the items.
	 * @param start is a character sequence that should be prepended to the
	 * result.
	 * @param end is a character sequence that should be appended to the result.
	 */
	template <class T>
	static std::string join(T es, const std::string &delim,
	                        const std::string &start = "",
	                        const std::string &end = "")
	{
		std::stringstream res;
		bool first = true;
		res << start;
		for (const auto &e : es) {
			if (!first) {
				res << delim;
			}
			res << e;
			first = false;
		}
		res << end;
		return res.str();
	}
};
}

#endif /* _OUSIA_UTILS_H_ */