Debian Codegraph @

inst/include/sourcetools/core/util.h

raw
1 #ifndef SOURCETOOLS_CORE_UTIL_H
2 #define SOURCETOOLS_CORE_UTIL_H
3
4 #include <string>
5 #include <memory>
6 #include <cctype>
7 #include <cstdlib>
8
9 namespace sourcetools {
10 namespace detail {
11
12 class noncopyable
13 {
14 protected:
15 noncopyable() {}
16 ~noncopyable() {}
17
18 private:
19 noncopyable(const noncopyable&);
20 noncopyable& operator=(const noncopyable&);
21 };
22
23 } // namespace detail
24 typedef detail::noncopyable noncopyable;
25
26 template <typename T>
27 class scoped_ptr : noncopyable
28 {
29 public:
30 explicit scoped_ptr(T* pData) : pData_(pData) {}
31 T& operator*() const { return *pData_; }
32 T* operator->() const { return pData_; }
33 operator T*() const { return pData_; }
34 ~scoped_ptr() { delete pData_; }
35 private:
36 T* pData_;
37 };
38
39 template <typename T>
40 class scoped_array : noncopyable
41 {
42 public:
43 explicit scoped_array(T* pData) : pData_(pData) {}
44 T& operator*() const { return *pData_; }
45 T* operator->() const { return pData_; }
46 operator T*() const { return pData_; }
47 ~scoped_array() { delete[] pData_; }
48 private:
49 T* pData_;
50 };
51
52 namespace utils {
53
54 inline bool isWhitespace(char ch)
55 {
56 return
57 ch == ' ' ||
58 ch == '\f' ||
59 ch == '\r' ||
60 ch == '\n' ||
61 ch == '\t' ||
62 ch == '\v';
63 }
64
65 template <typename T>
66 inline bool countWhitespaceBytes(const char* data,
67 T* pBytes)
68 {
69 T bytes = 0;
70 while (isWhitespace(*data)) {
71 ++data;
72 ++bytes;
73 }
74
75 *pBytes = bytes;
76 return bytes != 0;
77 }
78
79 inline bool isDigit(char ch)
80 {
81 return
82 (ch >= '0' && ch <= '9');
83 }
84
85 inline bool isAlphabetic(char ch)
86 {
87 return
88 (ch >= 'a' && ch <= 'z') ||
89 (ch >= 'A' && ch <= 'Z');
90 }
91
92 inline bool isAlphaNumeric(char ch)
93 {
94 return
95 (ch >= 'a' && ch <= 'z') ||
96 (ch >= 'A' && ch <= 'Z') ||
97 (ch >= '0' && ch <= '9');
98 }
99
100 inline bool isHexDigit(char ch)
101 {
102 return
103 (ch >= '0' && ch <= '9') ||
104 (ch >= 'a' && ch <= 'f') ||
105 (ch >= 'A' && ch <= 'F');
106 }
107
108 inline bool isValidForStartOfRSymbol(char ch)
109 {
110 return
111 isAlphabetic(ch) ||
112 ch == '.' ||
113 static_cast<signed char>(ch) < 0;
114 }
115
116 inline bool isValidForRSymbol(char ch)
117 {
118 return
119 isAlphaNumeric(ch) ||
120 ch == '.' ||
121 ch == '_' ||
122 static_cast<signed char>(ch) < 0;
123 }
124
125 inline std::string escape(char ch)
126 {
127 switch (ch) {
128 case '\r':
129 return "\\r";
130 case '\n':
131 return "\\n";
132 case '\t':
133 return "\\t";
134 default:
135 return std::string(1, ch);
136 }
137 }
138
139 } // namespace utils
140 } // namespace sourcetools
141
142 #endif /* SOURCETOOLS_CORE_UTIL_H */