Skip to content

Commit c8acd02

Browse files
committed
added simplecpp::View to be able to write portable simplecpp::TokenList code and added tests for all constructors
1 parent 43f68be commit c8acd02

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

simplecpp.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,44 @@ namespace simplecpp {
6969
enum cppstd_t : std::int8_t { CPPUnknown=-1, CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26 };
7070

7171
using TokenString = std::string;
72+
73+
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
74+
using View = std::string_view;
75+
#else
76+
struct View
77+
{
78+
// cppcheck-suppress noExplicitConstructor
79+
View(const char* data)
80+
: data_(data)
81+
, size_(strlen(data))
82+
{}
83+
84+
#if !defined(__cpp_lib_span)
85+
View(const char* data, std::size_t size)
86+
: data_(data)
87+
, size_(size)
88+
{}
89+
90+
// cppcheck-suppress noExplicitConstructor
91+
View(const std::string& str)
92+
: data_(str.data())
93+
, size_(str.size())
94+
{}
95+
#endif
96+
97+
const char* data() const {
98+
return data_;
99+
}
100+
101+
std::size_t size() const {
102+
return size_;
103+
}
104+
105+
const char* data_;
106+
std::size_t size_;
107+
};
108+
#endif
109+
72110
class Macro;
73111

74112
/**
@@ -238,12 +276,10 @@ namespace simplecpp {
238276
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
239277
{}
240278
#endif
241-
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
242279
/** generates a token list from the given buffer */
243-
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
280+
TokenList(View data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
244281
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
245282
{}
246-
#endif
247283
#ifdef __cpp_lib_span
248284
/** generates a token list from the given buffer */
249285
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)

test.cpp

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,11 +3299,80 @@ static void preprocess_files()
32993299
}
33003300
}
33013301

3302-
static void safe_api()
3302+
static void tokenlist_api()
33033303
{
3304+
std::vector<std::string> filenames;
3305+
# if !defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
3306+
// buffer
3307+
{
3308+
char input[] = "code"; // NOLINT(misc-const-correctness)
3309+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3310+
}
3311+
{
3312+
const char input[] = "code";
3313+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3314+
}
3315+
{
3316+
unsigned char input[] = "code"; // NOLINT(misc-const-correctness)
3317+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3318+
}
3319+
{
3320+
const unsigned char input[] = "code";
3321+
simplecpp::TokenList(input,sizeof(input),filenames,"");
3322+
}
3323+
#endif
3324+
// buffer via View
3325+
{
3326+
const char * const input = "code";
3327+
simplecpp::TokenList({input},filenames,"");
3328+
}
3329+
// buffer via View
3330+
{
3331+
char input[] = "code"; // NOLINT(misc-const-correctness)
3332+
simplecpp::TokenList(simplecpp::View{input},filenames,"");
3333+
}
3334+
{
3335+
const char input[] = "code";
3336+
simplecpp::TokenList(simplecpp::View{input},filenames,"");
3337+
}
3338+
// sized buffer via View
3339+
{
3340+
char input[] = "code"; // NOLINT(misc-const-correctness)
3341+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3342+
}
3343+
{
3344+
const char input[] = "code";
3345+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3346+
}
3347+
// array
3348+
{
3349+
char input[] = "code"; // NOLINT(misc-const-correctness)
3350+
simplecpp::TokenList(input,filenames,"");
3351+
}
3352+
{
3353+
const char input[] = "code";
3354+
simplecpp::TokenList(input,filenames,"");
3355+
}
3356+
{
3357+
unsigned char input[] = "code"; // NOLINT(misc-const-correctness)
3358+
simplecpp::TokenList(input,filenames,"");
3359+
}
3360+
{
3361+
const unsigned char input[] = "code";
3362+
simplecpp::TokenList(input,filenames,"");
3363+
}
3364+
// std::string via View
3365+
{
3366+
std::string input = "code"; // NOLINT(misc-const-correctness)
3367+
simplecpp::TokenList(input,filenames,"");
3368+
}
3369+
{
3370+
const std::string input = "code";
3371+
simplecpp::TokenList(input,filenames,"");
3372+
}
3373+
33043374
// this test is to make sure the safe APIs are compiling
33053375
#if defined(__cpp_lib_string_view) || defined(__cpp_lib_span)
3306-
std::vector<std::string> filenames;
33073376
# if defined(__cpp_lib_string_view)
33083377
{
33093378
const char input[] = "code";
@@ -3660,7 +3729,7 @@ int main(int argc, char **argv)
36603729

36613730
TEST_CASE(preprocess_files);
36623731

3663-
TEST_CASE(safe_api);
3732+
TEST_CASE(tokenlist_api);
36643733

36653734
TEST_CASE(isAbsolutePath);
36663735

0 commit comments

Comments
 (0)