Skip to content

Commit

Permalink
More elegant solution for pure2-main-args.cpp2 (hsutter#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarzec authored Jan 18, 2024
1 parent f77affa commit 3bdb706
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 5 additions & 6 deletions regression-tests/pure2-main-args.cpp2
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
main: (args) = {
exe: std::string = args.argv[0];
pos:= exe.find_last_of("/\\");
if pos != std::string::npos {
pos++;
exe = exe.substr(pos, exe.size() - pos);
}
// Explicit call to string is necessary
// std::filesystem::path is base on and implicitly convertible to
// - std::string (on POSIX systems)
// - std::wstring (on Windows)
exe : _ = std::filesystem::path(args.argv[0]).filename().string();
std::cout
<< "args.argc is (args.argc)$\n"
<< "args.argv[0] is (exe)$\n"
Expand Down
13 changes: 6 additions & 7 deletions regression-tests/test-results/pure2-main-args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ auto main(int const argc_, char** argv_) -> int;
#line 1 "pure2-main-args.cpp2"
auto main(int const argc_, char** argv_) -> int{
auto const args = cpp2::make_args(argc_, argv_);
#line 2 "pure2-main-args.cpp2"
std::string exe {CPP2_ASSERT_IN_BOUNDS_LITERAL(args.argv, 0)};
auto pos {CPP2_UFCS(find_last_of)(exe, "/\\")};
if (pos != std::string::npos) {
++pos;
exe = CPP2_UFCS(substr)(exe, pos, CPP2_UFCS(size)(exe) - std::move(pos));
}
// Explicit call to string is necessary
// std::filesystem::path is base on and implicitly convertible to
// - std::string (on POSIX systems)
// - std::wstring (on Windows)
#line 6 "pure2-main-args.cpp2"
auto exe {CPP2_UFCS(string)(CPP2_UFCS(filename)(std::filesystem::path(CPP2_ASSERT_IN_BOUNDS_LITERAL(args.argv, 0))))};
std::cout
<< ("args.argc is " + cpp2::to_string(args.argc) + "\n")
<< ("args.argv[0] is " + cpp2::to_string(std::move(exe)) + "\n");
Expand Down

0 comments on commit 3bdb706

Please sign in to comment.