-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpptoken.h
More file actions
76 lines (63 loc) · 1.61 KB
/
Copy pathcpptoken.h
File metadata and controls
76 lines (63 loc) · 1.61 KB
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
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef CBCC354D_B949_4767_8FE6_6EA7C16BDF99
#define CBCC354D_B949_4767_8FE6_6EA7C16BDF99
#include <cstring>
#include <memory>
#include <string>
#include <vector>
//////////////////////////////////////////////////////////////////////////
// For holding tokens
struct CppToken
{
const char* sz;
size_t len;
bool operator==(const CppToken& rhs) const
{
if (len != rhs.len)
return false;
return (sz == rhs.sz) || (std::strncmp(sz, rhs.sz, len) == 0);
}
bool operator!=(const CppToken& rhs) const
{
if (len != rhs.len)
return true;
return std::strncmp(sz, rhs.sz, len) != 0;
}
operator std::string() const
{
return toString();
}
std::string toString() const
{
return sz ? std::string(sz, len) : std::string();
}
};
/**
* Since CppToken cannot have ctor (because it is intended to be used inside union).
*/
inline CppToken MakeCppToken(const char* sz, size_t len)
{
CppToken tkn = {sz, len};
return tkn;
}
inline CppToken MakeCppToken(const char* beg, const char* end)
{
return MakeCppToken(beg, end - beg);
}
inline CppToken MergeCppToken(const CppToken& token1, const CppToken& token2)
{
if (token1.sz == nullptr)
return token2;
else if (token2.sz == nullptr)
return token1;
return MakeCppToken(token1.sz, token2.sz + token2.len - token1.sz);
}
template <typename _ST>
inline _ST& operator<<(_ST& stm, const CppToken& token)
{
for (size_t i = 0; i < token.len; ++i)
stm << token.sz[i];
return stm;
}
#endif /* CBCC354D_B949_4767_8FE6_6EA7C16BDF99 */