return n == std::string::npos ? path : path.substr(n + 1);
}
-bool
-is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos)
-{
-#ifdef _WIN32
- const char delim[] = "/\\";
-#else
- const char delim[] = "/";
-#endif
- split_pos = path.find_first_of(delim);
- if (split_pos != std::string::npos) {
-#ifdef _WIN32
- // -I/C:/foo and -I/c/foo will already be handled by delim_pos
- // correctly resulting in -I and /C:/foo or /c/foo respectively.
- // -IC:/foo will not as we would get -IC: and /foo
- if (split_pos > 0 && path[split_pos - 1] == ':') {
- split_pos = split_pos - 2;
- }
-#endif
- // this is not redundant on some platforms so nothing to simplify
- // NOLINTNEXTLINE(readability-simplify-boolean-expr)
- return true;
- }
- return false;
-}
-
std::string
change_extension(string_view path, string_view new_ext)
{
#endif
}
+nonstd::optional<size_t>
+is_absolute_path_with_prefix(nonstd::string_view path)
+{
+#ifdef _WIN32
+ const char delim[] = "/\\";
+#else
+ const char delim[] = "/";
+#endif
+ auto split_pos = path.find_first_of(delim);
+ if (split_pos != std::string::npos) {
+#ifdef _WIN32
+ // -I/C:/foo and -I/c/foo will already be handled by delim_pos correctly
+ // resulting in -I and /C:/foo or /c/foo respectively. -IC:/foo will not as
+ // we would get -IC: and /foo.
+ if (split_pos > 0 && path[split_pos - 1] == ':') {
+ split_pos = split_pos - 2;
+ }
+#endif
+ // This is not redundant on some platforms, so nothing to simplify.
+ // NOLINTNEXTLINE(readability-simplify-boolean-expr)
+ return split_pos;
+ }
+ return nonstd::nullopt;
+}
+
#if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
int
is_nfs_fd(int fd, bool* is_nfs)
// Get base name of path.
nonstd::string_view base_name(nonstd::string_view path);
-// Determine if `path` is an absolute path with prefix, returning the split
-// point
-bool is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos);
-
// Get an integer value from bytes in big endian order.
//
// Parameters:
buffer[0] = value;
}
+// Determine if `path` is an absolute path with prefix, returning the split
+// point.
+nonstd::optional<size_t> is_absolute_path_with_prefix(nonstd::string_view path);
+
// Test if a file is on nfs.
//
// Sets is_nfs to the result if fstatfs is available and no error occurred.
// Potentially rewrite concatenated absolute path argument to relative.
if (args[i][0] == '-') {
- size_t slash_pos = 0;
- if (Util::is_absolute_path_with_prefix(args[i], slash_pos)) {
- std::string option = args[i].substr(0, slash_pos);
+ const auto slash_pos = Util::is_absolute_path_with_prefix(args[i]);
+ if (slash_pos) {
+ std::string option = args[i].substr(0, *slash_pos);
if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) {
- auto relpath =
- Util::make_relative_path(ctx, string_view(args[i]).substr(slash_pos));
+ auto relpath = Util::make_relative_path(
+ ctx, string_view(args[i]).substr(*slash_pos));
std::string new_option = option + relpath;
if (compopt_affects_cpp_output(option)) {
state.cpp_args.push_back(new_option);
-// Copyright (C) 2019-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt");
}
-TEST_CASE("Util::is_absolute_path_with_prefix")
-{
- size_t delim_pos = 0;
- CHECK(Util::is_absolute_path_with_prefix("-I/c/foo", delim_pos));
- CHECK(delim_pos == 2);
- CHECK(Util::is_absolute_path_with_prefix("-W,path/c/foo", delim_pos));
- CHECK(delim_pos == 7);
- CHECK(!Util::is_absolute_path_with_prefix("-DMACRO", delim_pos));
-#ifdef _WIN32
- CHECK(Util::is_absolute_path_with_prefix("-I/C:/foo", delim_pos));
- CHECK(delim_pos == 2);
- CHECK(Util::is_absolute_path_with_prefix("-IC:/foo", delim_pos));
- CHECK(delim_pos == 2);
- CHECK(Util::is_absolute_path_with_prefix("-W,path/c:/foo", delim_pos));
- CHECK(delim_pos == 7);
- CHECK(Util::is_absolute_path_with_prefix("-W,pathc:/foo", delim_pos));
- CHECK(delim_pos == 7);
- CHECK(!Util::is_absolute_path_with_prefix("-opt:value", delim_pos));
-#endif
-}
-
TEST_CASE("Util::big_endian_to_int")
{
uint8_t bytes[8] = {0x70, 0x9e, 0x9a, 0xbc, 0xd6, 0x54, 0x4b, 0xca};
CHECK(bytes[7] == 0xca);
}
+TEST_CASE("Util::is_absolute_path_with_prefix")
+{
+ CHECK(*Util::is_absolute_path_with_prefix("-I/c/foo") == 2);
+ CHECK(*Util::is_absolute_path_with_prefix("-W,path/c/foo") == 7);
+ CHECK(!Util::is_absolute_path_with_prefix("-DMACRO"));
+#ifdef _WIN32
+ CHECK(*Util::is_absolute_path_with_prefix("-I/C:/foo") == 2);
+ CHECK(*Util::is_absolute_path_with_prefix("-IC:/foo") == 2);
+ CHECK(*Util::is_absolute_path_with_prefix("-W,path/c:/foo") == 7);
+ CHECK(*Util::is_absolute_path_with_prefix("-W,pathc:/foo") == 7);
+ CHECK(!Util::is_absolute_path_with_prefix("-opt:value"));
+#endif
+}
+
TEST_CASE("Util::is_dir_separator")
{
CHECK(!Util::is_dir_separator('x'));