Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PathExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 223b8c16ccbd60d82dafec9fa3856cb0a04a15de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Collections.Generic;
using System.IO;

namespace UVtools.Core.Extensions
{
    public static class PathExtensions
    {
        public static string GetFileNameStripAllExtensions(string path)
        {
            path = Path.GetFileName(path);
            if(string.IsNullOrEmpty(path)) return string.Empty;
            var splitPath = path.Split('.', 2, StringSplitOptions.TrimEntries);
            return splitPath.Length == 0 ? string.Empty : splitPath[0];
        }

        public static string GetFileNameStripExtensions(string path, List<string> extensions, out string strippedExtension)
        {
            strippedExtension = string.Empty;
            path = Path.GetFileName(path);
            if (string.IsNullOrEmpty(path)) return string.Empty;
            foreach (var extension in extensions)
            {
                var dotExtension = $".{extension}";
                if (!path.EndsWith(dotExtension, StringComparison.OrdinalIgnoreCase)) continue;
                strippedExtension = extension;
                return path.Remove(path.Length - dotExtension.Length);
            }

            return path;
        }
    }
}