// 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
#include
#include
#include
//////////////////////////////////////////////////////////////////////////
// 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
inline _ST& operator