Index: source/main.cpp =================================================================== --- source/main.cpp +++ source/main.cpp @@ -108,8 +108,8 @@ // Request the high performance GPU on Windows by default if no system override is specified. // See: // - https://github.com/supertuxkart/stk-code/pull/4693/commits/0a99c667ef513b2ce0f5755729a6e05df8aac48a -// - https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm -// - https://gpuopen.com/learn/amdpowerxpressrequesthighperformance/ +// - https://docs.nvidia.com/gameworks/content/technologies/desktop/optimus.htm +// - https://gpuopen.com/learn/amdpowerxpressrequesthighperformance/ extern "C" { __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; @@ -496,9 +496,9 @@ // moved into a helper function to ensure args is destroyed before // exit(), which may result in a memory leak. -static void RunGameOrAtlas(int argc, const char* argv[]) +static void RunGameOrAtlas(const PS::span argv) { - CmdLineArgs args(argc, argv); + CmdLineArgs args(argv); g_CmdLineArgs = args; @@ -736,7 +736,8 @@ EarlyInit(); // must come at beginning of main - RunGameOrAtlas(argc, const_cast(argv)); + // static_cast is ok, argc is required to be non-negative. + RunGameOrAtlas({argv, static_cast(argc)}); // Shut down profiler initialised by EarlyInit g_Profiler2.Shutdown(); Index: source/ps/GameSetup/CmdLineArgs.h =================================================================== --- source/ps/GameSetup/CmdLineArgs.h +++ source/ps/GameSetup/CmdLineArgs.h @@ -18,8 +18,9 @@ #ifndef INCLUDED_CMDLINEARGS #define INCLUDED_CMDLINEARGS -#include "ps/CStr.h" #include "lib/os_path.h" +#include "ps/containers/Span.h" +#include "ps/CStr.h" #include @@ -33,10 +34,9 @@ * All arguments are required to be of the form -name or * -name=value - anything else is ignored. * - * @param argc size of argv array - * @param argv array of arguments; argv[0] should be the program's name + * @param argv span of arguments; argv[0] should be the program's name */ - CmdLineArgs(int argc, const char* argv[]); + CmdLineArgs(PS::span argv); /** * Test whether the given name was specified, as either -name or Index: source/ps/GameSetup/CmdLineArgs.cpp =================================================================== --- source/ps/GameSetup/CmdLineArgs.cpp +++ source/ps/GameSetup/CmdLineArgs.cpp @@ -20,6 +20,8 @@ #include "lib/sysdep/sysdep.h" +#include + CmdLineArgs g_CmdLineArgs; namespace @@ -42,41 +44,40 @@ } // namespace -CmdLineArgs::CmdLineArgs(int argc, const char* argv[]) +CmdLineArgs::CmdLineArgs(const PS::span argv) { - if (argc >= 1) - { - std::string arg0(argv[0]); - // avoid OsPath complaining about mixing both types of separators, - // which happens when running in the VC2010 debugger - std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP); - m_Arg0 = arg0; - } + if (argv.empty()) + return; + + std::string arg0(argv[0]); + // avoid OsPath complaining about mixing both types of separators, + // which happens when running in the VC2010 debugger + std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP); + m_Arg0 = arg0; - for (int i = 1; i < argc; ++i) + // Implicit conversion from const char* to std::string_view. + for (const std::string_view argN : argv.subspan(1)) { // Only accept arguments that start with '-' - if (argv[i][0] != '-') + if (argN.front() != '-') { - m_ArgsWithoutName.emplace_back(argv[i]); + m_ArgsWithoutName.emplace_back(argN.begin(), argN.end()); continue; } // Allow -arg and --arg - char offset = argv[i][1] == '-' ? 2 : 1; - CStr name, value; + const std::string_view afterDashes = argN.substr(argN[1] == '-' ? 2 : 1); // Check for "-arg=value" - const char* eq = strchr(argv[i], '='); - if (eq) - { - name = CStr(argv[i]+offset, eq-argv[i]-offset); - value = CStr(eq+1); - } - else - name = CStr(argv[i]+offset); - - m_Args.emplace_back(std::move(name), std::move(value)); + const std::string_view::iterator eq = std::find(afterDashes.begin(), + afterDashes.end(), '='); + + m_Args.emplace_back + ( + std::piecewise_construct, + std::tuple{afterDashes.begin(), eq}, + std::tuple{std::min(eq + 1, afterDashes.end()), afterDashes.end()} + ); } } Index: source/ps/GameSetup/tests/test_CmdLineArgs.h =================================================================== --- source/ps/GameSetup/tests/test_CmdLineArgs.h +++ source/ps/GameSetup/tests/test_CmdLineArgs.h @@ -24,8 +24,8 @@ public: void test_has() { - const char* argv[] = { "program", "-test2" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = { "program", "-test2" }; + CmdLineArgs c(argv); TS_ASSERT(!c.Has("test1")); TS_ASSERT(c.Has("test2")); TS_ASSERT(!c.Has("test3")); @@ -34,8 +34,8 @@ void test_get() { - const char* argv[] = { "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = { "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" }; + CmdLineArgs c(argv); TS_ASSERT(!c.Has("program")); TS_ASSERT_STR_EQUALS(c.Get("test0"), ""); TS_ASSERT_STR_EQUALS(c.Get("test1"), ""); @@ -46,8 +46,11 @@ void test_multiple() { - const char* argv[] = { "program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = + { + "program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" + }; + CmdLineArgs c(argv); TS_ASSERT_STR_EQUALS(c.Get("test1"), "one"); TS_ASSERT_STR_EQUALS(c.Get("test2"), "none"); @@ -66,10 +69,10 @@ void test_get_invalid() { - const char* argv[] = { + constexpr std::array argv = { "-test1", "--test2", "test3-", " -test4", "--", "-==" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + CmdLineArgs c(argv); TS_ASSERT(!c.Has("test1")); TS_ASSERT(c.Has("test2")); @@ -79,26 +82,32 @@ void test_arg0() { - const char* argv[] = { "program" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = { "program" }; + CmdLineArgs c(argv); TS_ASSERT_WSTR_EQUALS(c.GetArg0().string(), L"program"); - CmdLineArgs c2(0, NULL); + CmdLineArgs c2(PS::span{}); TS_ASSERT_WSTR_EQUALS(c2.GetArg0().string(), L""); - const char* argv3[] = { "ab/cd/ef/gh/../ij" }; - CmdLineArgs c3(ARRAY_SIZE(argv3), argv3); -#if OS_WIN - TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab\\cd\\ef\\gh\\..\\ij"); -#else - TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab/cd/ef/gh/../ij"); -#endif + const std::array argv3 = { "ab/cd/ef/gh/../ij" }; + CmdLineArgs c3(argv3); + if constexpr(OS_WIN) + { + TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab\\cd\\ef\\gh\\..\\ij"); + } + else + { + TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab/cd/ef/gh/../ij"); + } } void test_get_without_names() { - const char* argv[] = { "program", "test0", "-test1", "test2", "test3", "--test4=test5" }; - CmdLineArgs c(ARRAY_SIZE(argv), argv); + constexpr std::array argv = + { + "program", "test0", "-test1", "test2", "test3", "--test4=test5" + }; + CmdLineArgs c(argv); TS_ASSERT(c.Has("test1")); TS_ASSERT_STR_EQUALS(c.Get("test4"), "test5"); CStr expected_args[] = { "test0", "test2", "test3" }; Index: source/ps/containers/Span.h =================================================================== --- source/ps/containers/Span.h +++ source/ps/containers/Span.h @@ -52,6 +52,10 @@ constexpr span(iterator first, iterator last) : m_Pointer(first), m_Extent(static_cast(last - first)) {} + template + constexpr span(const std::array& arr) + : m_Pointer(arr.data()), m_Extent(arr.size()) {} + constexpr span(const span& other) = default; constexpr span& operator=(const span& other) = default; @@ -66,11 +70,16 @@ constexpr iterator begin() const { return m_Pointer; } constexpr iterator end() const { return m_Pointer + m_Extent; } + constexpr span subspan(size_type offset) const { return {m_Pointer + offset, m_Extent - offset}; } + private: pointer m_Pointer; size_type m_Extent; }; +template +span(const std::array&) -> span; + } // namespace PS #endif // INCLUDED_PS_SPAN