From 77b4ddce898e0504013f9636f2e3a262b713c44e Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Mon, 21 Feb 2022 16:56:09 -0300 Subject: [PATCH] Fix `GetExtension` and add `HasExtension` to `Path` --- BeefLibs/corlib/src/IO/Path.bf | 51 +++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/BeefLibs/corlib/src/IO/Path.bf b/BeefLibs/corlib/src/IO/Path.bf index 698bd682..5b7abecd 100644 --- a/BeefLibs/corlib/src/IO/Path.bf +++ b/BeefLibs/corlib/src/IO/Path.bf @@ -303,10 +303,53 @@ namespace System.IO public static Result GetExtension(StringView inPath, String outExt) { - int i; - if ((i = inPath.LastIndexOf('.')) != -1) - outExt.Append(inPath, i); - return .Ok; + if (inPath.IsEmpty) + return .Err; + + CheckInvalidPathChars(inPath); + + for (int i = inPath.Length; --i >= 0;) + { + char8 ch = inPath[i]; + if (ch == '.') + { + if (i != inPath.Length - 1) + { + outExt.Append(inPath.Substring(i, inPath.Length - i)); + return .Ok; + } + else + break; + } + if (ch == DirectorySeparatorChar || ch == AltDirectorySeparatorChar || ch == VolumeSeparatorChar) + break; + } + + return .Err; + } + + public static bool HasExtension(StringView path) + { + if (path.IsEmpty) + return false; + + CheckInvalidPathChars(path); + + for (int i = path.Length; --i >= 0;) + { + char8 ch = path[i]; + if (ch == '.') + { + if (i != path.Length - 1) + return true; + else + return false; + } + if (ch == DirectorySeparatorChar || ch == AltDirectorySeparatorChar || ch == VolumeSeparatorChar) + break; + } + + return false; } public static Result GetTempPath(String outPath)