Przeglądaj źródła

core: do not insert separator if not necessary

Daniele Bartolini 6 lat temu
rodzic
commit
783a1ccaf4
2 zmienionych plików z 35 dodań i 17 usunięć
  1. 2 1
      src/core/filesystem/path.cpp
  2. 33 16
      src/core/unit_tests.cpp

+ 2 - 1
src/core/filesystem/path.cpp

@@ -59,7 +59,8 @@ namespace path
 		const u32 lb = strlen32(path_b);
 		path.reserve(la + lb + 1);
 		path  = path_a;
-		path += PATH_SEPARATOR;
+		if (la != 0 && lb != 0)
+			path += PATH_SEPARATOR;
 		path += path_b;
 	}
 

+ 33 - 16
src/core/unit_tests.cpp

@@ -1215,14 +1215,6 @@ static void test_path()
 		const bool b = path::is_root("/home");
 		ENSURE(b == false);
 	}
-	{
-		TempAllocator128 ta;
-		DynamicString path(ta);
-		path::join(path, "/home", "foo");
-		ENSURE(path == "/home/foo");
-		path::join(path, "/home", "bar");
-		ENSURE(path == "/home/bar");
-	}
 	{
 		ENSURE(path::has_trailing_separator("/home/foo/"));
 		ENSURE(!path::has_trailing_separator("/home/foo"));
@@ -1258,14 +1250,6 @@ static void test_path()
 		const bool b = path::is_root("E:\\Users");
 		ENSURE(b == false);
 	}
-	{
-		TempAllocator128 ta;
-		DynamicString path(ta);
-		path::join(path, "C:\\Users", "foo");
-		ENSURE(path == "C:\\Users\\foo");
-		path::join(path, "C:\\Users", "bar");
-		ENSURE(path == "C:\\Users\\bar");
-	}
 	{
 		ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
 		ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
@@ -1303,6 +1287,39 @@ static void test_path()
 		const char* r = path::extension("boot.bar.config");
 		ENSURE(strcmp(r, "config") == 0);
 	}
+	{
+		TempAllocator128 ta;
+		DynamicString path(ta);
+		path::join(path, "", "");
+		ENSURE(path == "");
+	}
+	{
+		TempAllocator128 ta;
+		DynamicString path(ta);
+		path::join(path, "foo", "");
+		ENSURE(path == "foo");
+	}
+	{
+		TempAllocator128 ta;
+		DynamicString path(ta);
+		path::join(path, "", "bar");
+		ENSURE(path == "bar");
+	}
+#if CROWN_PLATFORM_POSIX
+	{
+		TempAllocator128 ta;
+		DynamicString path(ta);
+		path::join(path, "foo", "bar");
+		ENSURE(path == "foo/bar");
+	}
+#else
+	{
+		TempAllocator128 ta;
+		DynamicString path(ta);
+		path::join(path, "C:\\foo", "bar");
+		ENSURE(path == "C:\\foo\\bar");
+	}
+#endif // CROWN_PLATFORM_POSIX
 }
 
 static void test_command_line()