From a4bd5caf05075ba870df93a1b1ba1413ac9dbaf7 Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 10 Apr 2017 22:18:41 +0400 Subject: [PATCH] added static class implementing string extension methods. methods check if file name ends with specified extensions. --- ArcFormats/ArcCommon.cs | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ArcFormats/ArcCommon.cs b/ArcFormats/ArcCommon.cs index ad096345..8cd66218 100644 --- a/ArcFormats/ArcCommon.cs +++ b/ArcFormats/ArcCommon.cs @@ -93,6 +93,66 @@ namespace GameRes.Formats } } + public static class StringExtensions + { + /// + /// Check if has specified extension . + /// + public static bool HasExtension (this string filename, string ext) + { + bool ext_is_empty = string.IsNullOrEmpty (ext); + if (!ext_is_empty && '.' == ext[0]) + return filename.EndsWith (ext, StringComparison.InvariantCultureIgnoreCase); + int ext_start = GetExtensionIndex (filename); + // filename extension length + int l_ext_length = filename.Length - ext_start; + if (ext_is_empty) + return 0 == l_ext_length; + return (l_ext_length == ext.Length + && filename.EndsWith (ext, StringComparison.InvariantCultureIgnoreCase)); + } + + /// + /// Check if filename ends with any of the extensions from the . + /// + public static bool HasAnyOfExtensions (this string filename, params string[] ext_list) + { + int ext_start = GetExtensionIndex (filename); + int l_ext_length = filename.Length - ext_start; + foreach (string ext in ext_list) + { + if (string.IsNullOrEmpty (ext) || "." == ext) + { + if (0 == l_ext_length) + return true; + } + else if ('.' == ext[0] || l_ext_length == ext.Length) + { + if (filename.EndsWith (ext, StringComparison.InvariantCultureIgnoreCase)) + return true; + } + } + return false; + } + + internal static int GetExtensionIndex (string filename) + { + int name_start = filename.LastIndexOfAny (VFS.PathSeparatorChars); + if (-1 == name_start) + name_start = 0; + else + name_start++; + if (filename.Length == name_start) // path ends with '\' + return name_start; + + int ext_start = filename.LastIndexOf ('.', filename.Length-1, filename.Length - name_start); + if (-1 == ext_start) + return filename.Length; + else + return ext_start + 1; + } + } + public class HuffmanDecoder { byte[] m_src;