aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/StringExtensions.cs
blob: 0f59235121d605cd64adf452e4448694e56b42d4 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

/// <exclude/>
/// <summary>
/// A collection of <see cref="String"/> extension methods.
/// </summary>
internal static class StringExtensions
{
    private static Regex titleRegEx;

    /// <summary>
    /// Initializes the <see cref="StringExtensions"/> class.
    /// </summary>
    static StringExtensions()
    {
        titleRegEx = new Regex(@"
                (?<=[A-Z])(?=[A-Z][a-z]) |
                 (?<=[^A-Z])(?=[A-Z]) |
                 (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
    }

    /// <summary>
    /// Returns true if the string is not null or contains white spaces.
    /// </summary>
    /// <param name="str">The string.</param>
    /// <returns></returns>
    internal static bool IsValid(this String str)
    {
        return !String.IsNullOrWhiteSpace(str);
    }

    /// <summary>
    /// Determines whether the string contains an existing file path.
    /// </summary>
    /// <param name="path">The path.</param>
    /// <returns></returns>
    internal static bool IsFileExists(this String path)
    {
        return System.IO.File.Exists(path);
    }

    /// <summary>
    /// Determines whether the string is valid as a file system path.
    /// </summary>
    /// <param name="path">The path.</param>
    /// <returns></returns>
    internal static bool IsPathValid(this String path)
    {
        return path.IsValid() && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) == -1;
    }

    /// <summary>
    /// Returns a friendly file size of the file path.
    /// </summary>
    /// <param name="filePath">The file path.</param>
    /// <returns></returns>
    internal static String GetFileSizeString(this String filePath)
    {
        FileInfo f = new FileInfo(filePath);
        double length = f.Length;

        string[] sizes = { "B", "KB", "MB", "GB" };
        double len = length;
        int order = 0;
        while (len >= 1024 && order + 1 < sizes.Length)
        {
            order++;
            len = len / 1024;
        }

        // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
        // show a single decimal place, and no space.
        return String.Format("{0:0.##} {1}", len, sizes[order]);
    }

    /// <summary>
    /// If string is a path, returns the file name.
    /// </summary>
    /// <param name="filePath">The file path.</param>
    /// <returns></returns>
    internal static String GetFileName(this String filePath)
    {
        return Path.GetFileName(filePath);
    }

    /// <summary>
    /// If string is a path, returns the file extension.
    /// </summary>
    /// <param name="filePath">The file path.</param>
    /// <returns></returns>
    internal static String GetFileExtension(this String filePath)
    {
        return Path.GetExtension(filePath);
    }

    internal static String ToTitle(this String str)
    {
        return titleRegEx.Replace(str, " ");
    }
}