Skip to content

Commit f108429

Browse files
committed
improved isAbsolutePath() implementation
1 parent 28bf747 commit f108429

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

simplecpp.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,11 +2417,19 @@ namespace simplecpp {
24172417
bool isAbsolutePath(const std::string &path)
24182418
{
24192419
#ifdef SIMPLECPP_WINDOWS
2420-
if (path.length() >= 3 && path[0] > 0 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
2420+
// C:\\path\\file
2421+
// C:/path/file
2422+
if (path.length() >= 3 && std::isalpha(path[0]) && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
24212423
return true;
2422-
return path.length() > 1U && (path[0] == '/' || path[0] == '\\');
2424+
2425+
// \\host\path\file
2426+
// //host/path/file
2427+
if (path.length() >= 2 && (path[0] == '\\' || path[0] == '/') && (path[1] == '\\' || path[1] == '/'))
2428+
return true;
2429+
2430+
return false;
24232431
#else
2424-
return path.length() > 1U && path[0] == '/';
2432+
return !path.empty() && path[0] == '/';
24252433
#endif
24262434
}
24272435
}

test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,14 +3246,14 @@ static void isAbsolutePath() {
32463246
//ASSERT_EQUALS(true, simplecpp::isAbsolutePath("\\")); // TODO
32473247
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:\\foo\\bar"));
32483248
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("0:/foo/bar"));
3249-
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar")); // TODO
3249+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\foo\\bar"));
32503250
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("\\\\")); // TODO
32513251
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("//")); // TODO
3252-
//ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar")); // TODO
3252+
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/foo/bar"));
32533253
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("/"));
32543254
#else
32553255
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/foo/bar"));
3256-
//ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/")); // TODO
3256+
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("/"));
32573257
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("//host/foo/bar"));
32583258

32593259
ASSERT_EQUALS(false, simplecpp::isAbsolutePath("foo/bar"));

0 commit comments

Comments
 (0)