Make "/C:/<source root>" source-relative on Windows

MakeAbsolutePathRelativeIfPossible() handles Windows paths written as
"C:/foo" and "/C:/foo" in separate branches. The "/C:/foo" one offsets
its indices by one for the leading slash, but since
https://gn-review.googlesource.com/c/gn/+/5680 its check for a path
equal to the source root applies the offset to the wrong length, so it
never matches: "/C:/src" used as a directory in a build file resolved
to "/C:/src/" instead of "//" with source root "C:/src".

Fix it by dropping the leading slash of "/C:/foo" paths first, so that
they line up with the source root like "C:/foo" paths and need no
offset. Both forms then go through the same code, including the
equality check that already worked for "C:/foo". Also share the rest of
the function with POSIX, which leaves only the prefix comparison
platform-specific.

Change-Id: I22c1122a5c0b8e7de2e7235ac8667590d548b32e
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/28960
Commit-Queue: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
diff --git a/src/gn/filesystem_utils.cc b/src/gn/filesystem_utils.cc
index a95a41a..ae4dfb4 100644
--- a/src/gn/filesystem_utils.cc
+++ b/src/gn/filesystem_utils.cc
@@ -367,6 +367,18 @@
 
   dest->clear();
 
+#if defined(OS_WIN)
+  // Handle "/C:/foo" like "C:/foo".
+  if (path.starts_with('/') && DoesBeginWindowsDriveLetter(path.substr(1)))
+    path.remove_prefix(1);
+
+  // Only paths with a drive letter can be inside the source root, which must
+  // have one too.
+  if (!DoesBeginWindowsDriveLetter(path))
+    return false;
+  DCHECK(DoesBeginWindowsDriveLetter(source_root));
+#endif
+
   // There is no specification of how many slashes may be at the end of
   // source_root or path. Trim them off for easier string manipulation.
   size_t path_len = AbsPathLenWithNoTrailingSlash(path);
@@ -374,88 +386,35 @@
 
   if (source_root_len > path_len)
     return false;  // The source root is longer: the path can never be inside.
+
 #if defined(OS_WIN)
-  // Source root should be canonical on Windows. Note that the initial slash
-  // must be forward slash, but that the other ones can be either forward or
-  // backward.
-  DCHECK(source_root.size() > 2 && source_root[0] != '/' &&
-         source_root[1] == ':' && IsSlash(source_root[2]));
-
-  size_t after_common_index = std::string::npos;
-  if (DoesBeginWindowsDriveLetter(path)) {
-    // Handle "C:\foo"
-    if (AreAbsoluteWindowsPathsEqual(source_root.substr(0, source_root_len),
-                                     path.substr(0, source_root_len))) {
-      after_common_index = source_root_len;
-      if (path_len == source_root_len) {
-        *dest = "//";
-        return true;
-      }
-    } else {
-      return false;
-    }
-  } else if (path[0] == '/' && source_root_len <= path_len - 1 &&
-             DoesBeginWindowsDriveLetter(path.substr(1))) {
-    // Handle "/C:/foo"
-    if (AreAbsoluteWindowsPathsEqual(source_root.substr(0, source_root_len),
-                                     path.substr(1, source_root_len))) {
-      after_common_index = source_root_len + 1;
-      if (path_len + 1 == source_root_len) {
-        *dest = "//";
-        return true;
-      }
-    } else {
-      return false;
-    }
-  } else {
+  if (!AreAbsoluteWindowsPathsEqual(source_root.substr(0, source_root_len),
+                                    path.substr(0, source_root_len)))
     return false;
+#else
+  if (!path.starts_with(source_root.substr(0, source_root_len)))
+    return false;
+#endif
+
+  if (path_len == source_root_len) {
+    // path is equivalent to source_root.
+    *dest = "//";
+    return true;
   }
-
-  // If we get here, there's a match and after_common_index identifies the
-  // part after it.
-
-  if (!IsSlash(path[after_common_index])) {
+  if (!IsSlash(path[source_root_len])) {
     // path is ${source-root}SUFFIX/...
     return false;
   }
-  // A source-root relative path, The input may have an unknown number of
+
+  // A source-root relative path. The input may have an unknown number of
   // slashes after the previous match. Skip over them.
-  size_t first_after_slash = after_common_index + 1;
+  size_t first_after_slash = source_root_len + 1;
   while (first_after_slash < path_len && IsSlash(path[first_after_slash]))
     first_after_slash++;
+
   dest->assign("//");  // Result is source root relative.
-  dest->append(&path.data()[first_after_slash],
-               path.size() - first_after_slash);
+  dest->append(path.substr(first_after_slash));
   return true;
-
-#else
-
-  // On non-Windows this is easy. Since we know both are absolute, just do a
-  // prefix check.
-
-  if (path.substr(0, source_root_len) ==
-      source_root.substr(0, source_root_len)) {
-    if (path_len == source_root_len) {
-      // path is equivalent to source_root.
-      *dest = "//";
-      return true;
-    } else if (!IsSlash(path[source_root_len])) {
-      // path is ${source-root}SUFFIX/...
-      return false;
-    }
-    // A source-root relative path, The input may have an unknown number of
-    // slashes after the previous match. Skip over them.
-    size_t first_after_slash = source_root_len + 1;
-    while (first_after_slash < path_len && IsSlash(path[first_after_slash]))
-      first_after_slash++;
-
-    dest->assign("//");  // Result is source root relative.
-    dest->append(&path.data()[first_after_slash],
-                 path.size() - first_after_slash);
-    return true;
-  }
-  return false;
-#endif
 }
 
 base::FilePath MakeAbsoluteFilePathRelativeIfPossible(
diff --git a/src/gn/filesystem_utils_unittest.cc b/src/gn/filesystem_utils_unittest.cc
index f533bde..7c4754e 100644
--- a/src/gn/filesystem_utils_unittest.cc
+++ b/src/gn/filesystem_utils_unittest.cc
@@ -149,15 +149,49 @@
       MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\foo", &dest));
   EXPECT_EQ("//foo", dest);
   EXPECT_TRUE(
-      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/foo", &dest));
-  EXPECT_EQ("//foo", dest);
-  EXPECT_TRUE(
       MakeAbsolutePathRelativeIfPossible("c:\\base", "C:\\base\\foo\\", &dest));
   EXPECT_EQ("//foo\\", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\\\foo", &dest));
+  EXPECT_EQ("//foo", dest);
 
   EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\ba", &dest));
   EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base",
-                                                  "C:\\/notbase/foo", &dest));
+                                                  "C:\\notbase\\foo", &dest));
+  EXPECT_FALSE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\basefoo", &dest));
+  EXPECT_FALSE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "D:\\base\\foo", &dest));
+
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/foo", &dest));
+  EXPECT_EQ("//foo", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("c:\\base", "/c:/base/foo/", &dest));
+  EXPECT_EQ("//foo/", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base//foo", &dest));
+  EXPECT_EQ("//foo", dest);
+
+  EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/ba", &dest));
+  EXPECT_FALSE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/notbase/foo", &dest));
+  EXPECT_FALSE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/basefoo", &dest));
+  EXPECT_FALSE(
+      MakeAbsolutePathRelativeIfPossible("C:\\base", "/D:/base/foo", &dest));
 #else
 
   EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/foo/", &dest));
@@ -167,10 +201,17 @@
   EXPECT_TRUE(
       MakeAbsolutePathRelativeIfPossible("/base/", "/base/foo/", &dest));
   EXPECT_EQ("//foo/", dest);
+  EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/", &dest));
+  EXPECT_EQ("//", dest);
+  EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base//foo", &dest));
+  EXPECT_EQ("//foo", dest);
 
   EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest));
   EXPECT_FALSE(
       MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo", &dest));
+  EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/basefoo", &dest));
 #endif
 }