PageRenderTime 4ms CodeModel.GetById 81ms app.highlight 59ms RepoModel.GetById 62ms app.codeStats 0ms

/GitCommands/System.cs

https://github.com/vbjay/gitextensions
C# | 211 lines | 142 code | 35 blank | 34 comment | 26 complexity | 0f7333187ecaa8a79dced65aec27d626 MD5 | raw file
  1using System.Collections.Generic;
  2using System.Linq;
  3using System.Text;
  4using JetBrains.Annotations;
  5
  6namespace System
  7{
  8    public static class StringExtensions
  9    {
 10        /// <summary>'\n'</summary>
 11        static readonly char[] NewLineSeparator = new char[] { '\n' };
 12
 13        public static string SkipStr(this string str, string toSkip)
 14        {
 15            if (str == null)
 16                return null;
 17
 18            int idx;
 19            idx = str.IndexOf(toSkip);
 20            if (idx != -1)
 21                return str.Substring(idx + toSkip.Length);
 22            else
 23                return null;
 24        }
 25
 26        public static String TakeUntilStr(this string str, String untilStr)
 27        {
 28            if (str == null)
 29                return null;
 30
 31            int idx;
 32            idx = str.IndexOf(untilStr);
 33            if (idx != -1)
 34                return str.Substring(0, idx);
 35            else
 36                return str;
 37        }
 38
 39        public static string CommonPrefix(this string s, string other)
 40        {
 41            if (s.IsNullOrEmpty() || other.IsNullOrEmpty())
 42                return string.Empty;
 43
 44            int prefixLength = 0;
 45
 46            foreach (char c in other)
 47            {
 48                if (s.Length <= prefixLength || s[prefixLength] != c)
 49                    return s.Substring(0, prefixLength);
 50
 51                prefixLength++;
 52            }
 53
 54            return s;
 55        }
 56
 57        public static bool IsNullOrEmpty(this string s)
 58        {
 59            return string.IsNullOrEmpty(s);
 60        }
 61
 62
 63        public static string Combine(this string left, string sep, string right)
 64        {
 65            if (left.IsNullOrEmpty())
 66                return right;
 67            else if (right.IsNullOrEmpty())
 68                return left;
 69            else
 70                return left + sep + right;
 71        }
 72
 73        public static string Quote(this string s)
 74        {
 75            return s.Quote("\"");
 76        }
 77
 78        public static string Quote(this string s, string quotationMark)
 79        {
 80            if (s == null)
 81                return string.Empty;
 82
 83            return quotationMark + s + quotationMark;
 84        }
 85
 86        /// <summary>
 87        /// Quotes string if it is not null and not empty
 88        /// </summary>
 89        /// <param name="s"></param>
 90        /// <returns></returns>
 91        public static string QuoteNE(this string s)
 92        {
 93            return s.IsNullOrEmpty() ? s : s.Quote("\"");
 94        }
 95
 96        /// <summary>
 97        /// Adds parentheses if string is not null and not empty
 98        /// </summary>
 99        /// <param name="s"></param>
100        /// <returns></returns>
101        public static string AddParenthesesNE(this string s)
102        {
103            return s.IsNullOrEmpty() ? s : "(" + s + ")";
104        }
105
106        /// <summary>
107        /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
108        /// </summary>
109        /// <param name="value">The string to test.</param>
110        /// <remarks>
111        /// This method is copied from .Net Framework 4.0 and should be deleted after leaving 3.5.
112        /// </remarks>
113        /// <returns>
114        /// true if the value parameter is null or <see cref="string.Empty"/>, or if value consists exclusively of white-space characters.
115        /// </returns>
116        [Pure]
117        public static bool IsNullOrWhiteSpace([CanBeNull] this string value)
118        {
119            return string.IsNullOrWhiteSpace(value);
120        }
121
122        /// <summary>Indicates whether the specified string is neither null, nor empty, nor has only whitespace.</summary>
123        public static bool IsNotNullOrWhitespace([CanBeNull] this string value)
124        {
125            return !value.IsNullOrWhiteSpace();
126        }
127
128        /// <summary>
129        /// Determines whether the beginning of this instance matches any of the specified strings.
130        /// </summary>
131        /// <param name="value"></param>
132        /// <param name="starts">array of strings to compare</param>
133        /// <returns>true if any starts element matches the beginning of this string; otherwise, false.</returns>
134        public static bool StartsWithAny([CanBeNull] this string value, string[] starts)
135        {
136            return value != null && starts.Any(s => value.StartsWith(s));
137        }
138
139        public static string RemoveLines(this string value, Func<string, bool> shouldRemoveLine)
140        {
141            if (value.IsNullOrEmpty())
142                return value;
143
144            if (value[value.Length - 1] == '\n')
145                value = value.Substring(0, value.Length - 1);
146
147            StringBuilder sb = new StringBuilder();
148            string[] lines = value.Split('\n');
149
150            foreach (string line in lines)
151                if (!shouldRemoveLine(line))
152                    sb.Append(line + '\n');
153
154            return sb.ToString();
155        }
156
157        /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
158        public static IEnumerable<string> SplitThenTrim(this string value, params string[] separator)
159        {
160            return value
161                .Split(separator, StringSplitOptions.RemoveEmptyEntries)
162                .Select(s => s.Trim());
163        }
164
165        /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
166        public static IEnumerable<string> SplitThenTrim(this string value, params char[] separator)
167        {
168            return value
169                .Split(separator, StringSplitOptions.RemoveEmptyEntries)
170                .Select(s => s.Trim());
171        }
172
173        /// <summary>Split a string, delimited by line-breaks, excluding empty entries.</summary>
174        public static string[] SplitLines(this string value)
175        {
176            return value.Split(NewLineSeparator);
177        }
178
179        /// <summary>Split a string, delimited by line-breaks, excluding empty entries; then trim whitespace.</summary>
180        public static IEnumerable<string> SplitLinesThenTrim(this string value)
181        {
182            return value.SplitThenTrim(NewLineSeparator);
183        }
184
185        /// <summary>Gets the text after the last separator.
186        /// If NO separator OR ends with separator, returns the original value.</summary>
187        public static string SubstringAfterLastSafe(this string value, string separator)
188        {// ex: "origin/master" -> "master"
189            if (value.EndsWith(separator) || !value.Contains(separator))
190            {// "origin/master/" OR "master" -> return original
191                return value;
192            }
193            return value.Substring(1 + value.LastIndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
194        }
195        public static string SubstringAfterFirst(this string value, string separator)
196        {
197            return value.Substring(1 + value.IndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
198        }
199
200    }
201
202    public static class BoolExtensions
203    {
204
205        public static string AsForce(this bool force)
206        {
207            return force ? " -f " : string.Empty;
208        }
209
210    }
211}