File: chartools.h

package info (click to toggle)
kdevelop 4%3A4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,360 kB
  • ctags: 11,484
  • sloc: cpp: 105,371; python: 522; ansic: 488; lex: 422; sh: 139; ruby: 120; makefile: 51; xml: 42; php: 12
file content (121 lines) | stat: -rw-r--r-- 3,951 bytes parent folder | download
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
/*
  Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef CHARTOOLS
#define CHARTOOLS

#include <cctype>

#include "cppparserexport.h"

template<class T>
class QVector;
class QString;
class QByteArray;
typedef QVector<unsigned int> PreprocessedContents;

/**
 * Attempt to fix bug 328285
 *
 * This is more or less a work-around to make rpp a bit more robust against
 * non-7bit ASCII chars.
 *
 * See explanation at: http://stackoverflow.com/a/6693878
 */
inline bool isSpace(char c) {
#ifdef _MSC_VER
  return isspace(static_cast<uchar>(c));
#else
  return std::isspace(static_cast<uchar>(c));
#endif
}

inline bool isLetter(char c) {
  return std::isalpha(static_cast<uchar>(c));
}

inline bool isLetterOrNumber(char c) {
  return std::isalnum(static_cast<uchar>(c));
}

inline bool isNumber(char c) {
  return std::isdigit(static_cast<uchar>(c));
}

//Takes an index as delt with during preprocessing, and determines whether it is a fake-index that represents
//a character. If the 0xffff0000 bits are set, it is a custom character.
inline bool isCharacter(uint index) {
  return (index & 0xffff0000) == 0xffff0000;
}

//Creates an index that represents the given character
inline uint indexFromCharacter(char character) {
  return (uint)character | 0xffff0000;
}

//Extracts the character that is represented by the index
inline char characterFromIndex(uint index) {
  return (char)(index & 0xffff);
}

inline bool isSpace(unsigned int c) {
  return isCharacter(c) && isSpace(characterFromIndex(c));
}

inline bool isLetter(unsigned int c) {
  return isCharacter(c) && isLetter(characterFromIndex(c));
}

inline bool isLetterOrNumber(unsigned int c) {
  return isCharacter(c) && isLetterOrNumber(characterFromIndex(c));
}

inline bool isNumber(unsigned int c) {
  return isCharacter(c) && isNumber(characterFromIndex(c));
}

/**
 * Whether input @p c is a valid macro identifier character
 *
 * In theory the standard only allows 'a-z', 'A-Z', '0-9', and '_',
 * however, GCC + MSVC accept '$' as well
 *
 * See http://stackoverflow.com/a/369524
 */
inline bool isValidMacroIdentifierToken(char c) {
  return isLetterOrNumber(c) || c == '_' || c == '$';
}

///Opposite of convertFromByteArray
KDEVCPPRPP_EXPORT QByteArray stringFromContents(const PreprocessedContents& contents, int offset = 0, int count = 0);

///Opposite of convertFromByteArray
KDEVCPPRPP_EXPORT QByteArray stringFromContents(const uint* contents, int count);

///Returns a string that has a gap inserted between the tokens(for debugging)
KDEVCPPRPP_EXPORT QByteArray stringFromContentsWithGaps(const PreprocessedContents& contents, int offset = 0, int count = 0);

///Converts the byte array to a vector of fake-indices containing the text
KDEVCPPRPP_EXPORT PreprocessedContents convertFromByteArray(const QByteArray& array);

///Converts the byte array to a vector of fake-indices containing the text
///This also tokenizes the given array when possible
KDEVCPPRPP_EXPORT PreprocessedContents tokenizeFromByteArray(const QByteArray& array);
#endif