summaryrefslogtreecommitdiff
path: root/support/highlight/src/core/stringtools.h
blob: 861ae85822c57f8884d87890f338797a2d555c59 (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
/***************************************************************************
                          stringtools.h  -  description
                             -------------------
    begin                : Mon Dec 10 2001
    copyright            : (C) 2001 by Andre Simon
    email                : andre.simon1@gmx.de
 ***************************************************************************/


/*
This file is part of Highlight.

Highlight 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.

Highlight 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 Highlight.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef STRINGTOOLS_H
#define STRINGTOOLS_H

#include <string>
#include <vector>
#include <sstream>

using namespace std;

/// Contains methods for string manipulation

namespace StringTools
{

	/** Change Keyword case */
	enum KeywordCase
	{
		CASE_UNCHANGED, ///< do not alter case
		CASE_LOWER,     ///< convert to lower case
		CASE_UPPER,     ///< convert to upper case
		CASE_CAPITALIZE ///< convert first character to upper case
	};

	/** Change character case of strings
	    \param s input string
	    \param kcase case modification indicator
	    \return modified string
	*/
	string change_case ( const string & s,
	                     const KeywordCase kcase = CASE_LOWER ) throw();

	/** Trim string (remove whitespace)
	   \param value String
	   \return string trimmed on the left
	*/
	string trimRight ( const string &value );

	/** Parse data within parantheses
	    \param s String, containing a opening and a closing paranthesis
	    \return value between "(", ")" */
	string getParantheseVal ( const string &s );

	/** Split string and return items separated by a delimiter
	    \param s string containing tokens
	    \param delim Token delimiter
	    \return vector containing found tokens */
	vector <string> splitString ( const string& s, unsigned char delim );

	/** Convert string to a numeric value of the given type
	    \param val variable of specified type which will contain the numeric value
	    \param s string containing a number
	    \param f format specifier function (IO manipulator)
	    \return true if successfull */
	template <class T>
	bool str2num ( T &val, const std::string& s, std::ios_base& ( *f ) ( std::ios_base& ) )
	{
		std::istringstream iss ( s );
		return ! ( iss >> f >> val ).fail();
	}

}

#endif