Skip to content

Commit 3ea626f

Browse files
committed
added simplecpp::View to be able to write portable simplecpp::TokenList code and added tests for all constructors
1 parent a45afce commit 3ea626f

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

simplecpp.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@ 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)
74+
using View = std::string_view;
75+
#else
76+
struct View
77+
{
78+
View(const char* data)
79+
: data_(data)
80+
, size_(strlen(data))
81+
{}
82+
83+
View(const char* data, std::size_t size)
84+
: data_(data)
85+
, size_(size)
86+
{}
87+
88+
View(const std::string& str)
89+
: data_(str.data())
90+
, size_(str.size())
91+
{}
92+
93+
const char* data() const {
94+
return data_;
95+
}
96+
97+
std::size_t size() const {
98+
return size_;
99+
}
100+
101+
const char* data_;
102+
std::size_t size_;
103+
};
104+
#endif
105+
72106
class Macro;
73107

74108
/**
@@ -238,9 +272,9 @@ namespace simplecpp {
238272
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
239273
{}
240274
#endif
241-
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
275+
#if !defined(__cpp_lib_span)
242276
/** 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)
277+
TokenList(View data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
244278
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
245279
{}
246280
#endif

test.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,11 +3299,66 @@ 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+
// View
3325+
{
3326+
char input[] = "code"; // NOLINT(misc-const-correctness)
3327+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3328+
}
3329+
{
3330+
const char input[] = "code";
3331+
simplecpp::TokenList({input,sizeof(input)},filenames,"");
3332+
}
3333+
// array
3334+
{
3335+
char input[] = "code"; // NOLINT(misc-const-correctness)
3336+
simplecpp::TokenList(input,filenames,"");
3337+
}
3338+
{
3339+
const char input[] = "code";
3340+
simplecpp::TokenList(input,filenames,"");
3341+
}
3342+
{
3343+
unsigned char input[] = "code"; // NOLINT(misc-const-correctness)
3344+
simplecpp::TokenList(input,filenames,"");
3345+
}
3346+
{
3347+
const unsigned char input[] = "code";
3348+
simplecpp::TokenList(input,filenames,"");
3349+
}
3350+
// std::string via View
3351+
{
3352+
std::string input = "code"; // NOLINT(misc-const-correctness)
3353+
simplecpp::TokenList(input,filenames,"");
3354+
}
3355+
{
3356+
const std::string input = "code";
3357+
simplecpp::TokenList(input,filenames,"");
3358+
}
3359+
33043360
// this test is to make sure the safe APIs are compiling
33053361
#if defined(__cpp_lib_string_view) || defined(__cpp_lib_span)
3306-
std::vector<std::string> filenames;
33073362
# if defined(__cpp_lib_string_view)
33083363
{
33093364
const char input[] = "code";
@@ -3660,7 +3715,7 @@ int main(int argc, char **argv)
36603715

36613716
TEST_CASE(preprocess_files);
36623717

3663-
TEST_CASE(safe_api);
3718+
TEST_CASE(tokenlist_api);
36643719

36653720
TEST_CASE(isAbsolutePath);
36663721

0 commit comments

Comments
 (0)