Skip to content

Commit a5950f4

Browse files
static std::string & initializers (#1335)
move the static inline const std::string& references to a helper function call --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent db70329 commit a5950f4

5 files changed

Lines changed: 66 additions & 14 deletions

File tree

azure-pipelines.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ jobs:
148148
containerImage: gcc:7
149149
cli11.std: 14
150150
cli11.options: -DCMAKE_CXX_FLAGS="-Wconversion"
151+
gcc8:
152+
containerImage: gcc:8
153+
cli11.std: 17
151154
gcc4.8:
152155
containerImage: helics/buildenv:gcc4-8-builder
153156
cli11.std: 11

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ set_property(
218218
TEST prefix_command PROPERTY PASS_REGULAR_EXPRESSION
219219
[=[Prefix: 3 : 2 : 1.*Remaining commands: other one two 3]=])
220220

221+
#just test a minimal build
222+
add_cli_exe(minimal minimal.cpp)
223+
add_test(NAME minimal COMMAND minimal)
224+
221225
add_cli_exe(arg_capture arg_capture.cpp)
222226
add_test(NAME arg_capture COMMAND arg_capture -v 27 --sub -v 13 --val prefix)
223227
set_property(TEST arg_capture PROPERTY PASS_REGULAR_EXPRESSION "value=27")

examples/minimal.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2+
// under NSF AWARD 1414736 and by the respective contributors.
3+
// All rights reserved.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
// Code modified from https://github.com/CLIUtils/CLI11/issues/1334
8+
9+
#include <CLI/CLI.hpp>
10+
11+
/** This example is a minimal example of using CLI11. It does not do anything, but it compiles and runs, and can be used
12+
* as a starting point for new projects.
13+
*/
14+
int main(int argc, char **argv) {
15+
16+
CLI::App app{"App description"};
17+
CLI11_PARSE(app, argc, argv);
18+
return 0;
19+
}

include/CLI/TypeTools.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,16 @@ bool lexical_assign(const std::string &input, AssignTo &output) {
14571457
/* on some older clang compilers */
14581458
#pragma clang diagnostic push
14591459
#pragma clang diagnostic ignored "-Wsign-conversion"
1460+
#elif defined(__GNUC__) && (__GNUC__ == 8)
1461+
/* gcc 8 warns on intentional assignments such as std::atomic<unsigned long> = int */
1462+
#pragma GCC diagnostic push
1463+
#pragma GCC diagnostic ignored "-Wsign-conversion"
14601464
#endif
14611465
output = val;
14621466
#if defined(__clang__)
14631467
#pragma clang diagnostic pop
1468+
#elif defined(__GNUC__) && (__GNUC__ == 8)
1469+
#pragma GCC diagnostic pop
14641470
#endif
14651471
return true;
14661472
}

include/CLI/impl/StringTools_inl.hpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,43 @@ find_member(std::string name, const std::vector<std::string> names, bool ignore_
185185
return (it != std::end(names)) ? (it - std::begin(names)) : (-1);
186186
}
187187

188-
CLI11_MODULE_INLINE const std::string &escapedChars("\b\t\n\f\r\"\\");
189-
CLI11_MODULE_INLINE const std::string &escapedCharsCode("btnfr\"\\");
188+
CLI11_MODULE_INLINE const std::string &escapedChars() {
189+
static const std::string s{"\b\t\n\f\r\"\\"};
190+
return s;
191+
}
192+
CLI11_MODULE_INLINE const std::string &escapedCharsCode() {
193+
static const std::string s{"btnfr\"\\"};
194+
return s;
195+
}
196+
CLI11_MODULE_INLINE const std::string &bracketChars() {
197+
static const std::string s{"\"'`[(<{"};
198+
return s;
199+
}
200+
CLI11_MODULE_INLINE const std::string &matchBracketChars() {
201+
static const std::string s{"\"'`])>}"};
202+
return s;
203+
}
204+
205+
// CLI11_MODULE_INLINE constexpr char escapedChars[]="\b\t\n\f\r\"\\";
206+
// CLI11_MODULE_INLINE constexpr char escapedCharsCode[]="btnfr\"\\";
207+
/*
208+
const std::string &escapedChars("\b\t\n\f\r\"\\");
209+
190210
CLI11_MODULE_INLINE const std::string &bracketChars("\"'`[(<{");
191211
CLI11_MODULE_INLINE const std::string &matchBracketChars("\"'`])>}");
192-
212+
*/
193213
CLI11_INLINE bool has_escapable_character(const std::string &str) {
194-
return (str.find_first_of(escapedChars) != std::string::npos);
214+
return (str.find_first_of(escapedChars()) != std::string::npos);
195215
}
196216

197217
CLI11_INLINE std::string add_escaped_characters(const std::string &str) {
198218
std::string out;
199219
out.reserve(str.size() + 4);
200220
for(char s : str) {
201-
auto sloc = escapedChars.find_first_of(s);
221+
auto sloc = escapedChars().find_first_of(s);
202222
if(sloc != std::string::npos) {
203223
out.push_back('\\');
204-
out.push_back(escapedCharsCode[sloc]);
224+
out.push_back(escapedCharsCode()[sloc]);
205225
} else {
206226
out.push_back(s);
207227
}
@@ -258,9 +278,9 @@ CLI11_INLINE std::string remove_escaped_characters(const std::string &str) {
258278
if(str.end() - loc < 2) {
259279
throw std::invalid_argument("invalid escape sequence " + str);
260280
}
261-
auto ecloc = escapedCharsCode.find_first_of(*(loc + 1));
281+
auto ecloc = escapedCharsCode().find_first_of(*(loc + 1));
262282
if(ecloc != std::string::npos) {
263-
out.push_back(escapedChars[ecloc]);
283+
out.push_back(escapedChars()[ecloc]);
264284
++loc;
265285
} else if(*(loc + 1) == 'u') {
266286
// must have 4 hex characters
@@ -330,7 +350,7 @@ CLI11_INLINE std::size_t close_literal_quote(const std::string &str, std::size_t
330350

331351
CLI11_INLINE std::size_t close_sequence(const std::string &str, std::size_t start, char closure_char) {
332352

333-
auto bracket_loc = matchBracketChars.find(closure_char);
353+
auto bracket_loc = matchBracketChars().find(closure_char);
334354
switch(bracket_loc) {
335355
case 0:
336356
return close_string_quote(str, start, closure_char);
@@ -356,7 +376,7 @@ CLI11_INLINE std::size_t close_sequence(const std::string &str, std::size_t star
356376
return loc;
357377
}
358378
}
359-
bracket_loc = bracketChars.find(str[loc]);
379+
bracket_loc = bracketChars().find(str[loc]);
360380
if(bracket_loc != std::string::npos) {
361381
switch(bracket_loc) {
362382
case 0:
@@ -367,7 +387,7 @@ CLI11_INLINE std::size_t close_sequence(const std::string &str, std::size_t star
367387
loc = close_literal_quote(str, loc, str[loc]);
368388
break;
369389
default:
370-
closures.push_back(matchBracketChars[bracket_loc]);
390+
closures.push_back(matchBracketChars()[bracket_loc]);
371391
break;
372392
}
373393
}
@@ -388,9 +408,9 @@ CLI11_INLINE std::vector<std::string> split_up(std::string str, char delimiter)
388408

389409
std::vector<std::string> output;
390410
while(!str.empty()) {
391-
if(bracketChars.find_first_of(str[0]) != std::string::npos) {
392-
auto bracketLoc = bracketChars.find_first_of(str[0]);
393-
auto end = close_sequence(str, 0, matchBracketChars[bracketLoc]);
411+
if(bracketChars().find_first_of(str[0]) != std::string::npos) {
412+
auto bracketLoc = bracketChars().find_first_of(str[0]);
413+
auto end = close_sequence(str, 0, matchBracketChars()[bracketLoc]);
394414
if(end >= str.size()) {
395415
output.push_back(std::move(str));
396416
str.clear();

0 commit comments

Comments
 (0)