|
| 1 | +// Copyright 2020-2021 ONIXLabs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.ComponentModel; |
| 17 | + |
| 18 | +namespace OnixLabs.Core |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Provides extension methods for strings. |
| 22 | + /// </summary> |
| 23 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 24 | + public static class StringExtensions |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// The value of an index not found. |
| 28 | + /// </summary> |
| 29 | + private const int IndexNotFound = -1; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Returns a substring before the first occurrence of the specified delimiter. |
| 33 | + /// </summary> |
| 34 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 35 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 36 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 37 | + /// <returns>Returns a substring before the first occurrence of the specified delimiter.</returns> |
| 38 | + public static string SubstringBefore( |
| 39 | + this string value, |
| 40 | + char delimiter, |
| 41 | + string? defaultValue = null) |
| 42 | + { |
| 43 | + int index = value.IndexOf(delimiter); |
| 44 | + |
| 45 | + return index switch |
| 46 | + { |
| 47 | + IndexNotFound => defaultValue ?? value, |
| 48 | + _ => value[..index] |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Returns a substring before the first occurrence of the specified delimiter. |
| 54 | + /// </summary> |
| 55 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 56 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 57 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 58 | + /// <param name="comparison">Specifies the string comparison rule for the search.</param> |
| 59 | + /// <returns>Returns a substring before the first occurrence of the specified delimiter.</returns> |
| 60 | + public static string SubstringBefore( |
| 61 | + this string value, |
| 62 | + string delimiter, |
| 63 | + string? defaultValue = null, |
| 64 | + StringComparison comparison = StringComparison.Ordinal) |
| 65 | + { |
| 66 | + int index = value.IndexOf(delimiter, comparison); |
| 67 | + |
| 68 | + return index switch |
| 69 | + { |
| 70 | + IndexNotFound => defaultValue ?? value, |
| 71 | + _ => value[..index] |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Returns a substring before the last occurrence of the specified delimiter. |
| 77 | + /// </summary> |
| 78 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 79 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 80 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 81 | + /// <returns>Returns a substring before the last occurrence of the specified delimiter.</returns> |
| 82 | + public static string SubstringBeforeLast( |
| 83 | + this string value, |
| 84 | + char delimiter, |
| 85 | + string? defaultValue = null) |
| 86 | + { |
| 87 | + int index = value.LastIndexOf(delimiter); |
| 88 | + |
| 89 | + return index switch |
| 90 | + { |
| 91 | + IndexNotFound => defaultValue ?? value, |
| 92 | + _ => value[..index] |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Returns a substring before the last occurrence of the specified delimiter. |
| 98 | + /// </summary> |
| 99 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 100 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 101 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 102 | + /// <param name="comparison">Specifies the string comparison rule for the search.</param> |
| 103 | + /// <returns>Returns a substring before the last occurrence of the specified delimiter.</returns> |
| 104 | + public static string SubstringBeforeLast( |
| 105 | + this string value, |
| 106 | + string delimiter, |
| 107 | + string? defaultValue = null, |
| 108 | + StringComparison comparison = StringComparison.Ordinal) |
| 109 | + { |
| 110 | + int index = value.LastIndexOf(delimiter, comparison); |
| 111 | + |
| 112 | + return index switch |
| 113 | + { |
| 114 | + IndexNotFound => defaultValue ?? value, |
| 115 | + _ => value[..index] |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Returns a substring after the first occurrence of the specified delimiter. |
| 121 | + /// </summary> |
| 122 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 123 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 124 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 125 | + /// <returns>Returns a substring after the first occurrence of the specified delimiter.</returns> |
| 126 | + public static string SubstringAfter( |
| 127 | + this string value, |
| 128 | + char delimiter, |
| 129 | + string? defaultValue = null) |
| 130 | + { |
| 131 | + int index = value.IndexOf(delimiter); |
| 132 | + |
| 133 | + return index switch |
| 134 | + { |
| 135 | + IndexNotFound => defaultValue ?? value, |
| 136 | + _ => value[(index + 1)..value.Length] |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Returns a substring after the first occurrence of the specified delimiter. |
| 142 | + /// </summary> |
| 143 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 144 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 145 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 146 | + /// <param name="comparison">Specifies the string comparison rule for the search.</param> |
| 147 | + /// <returns>Returns a substring after the first occurrence of the specified delimiter.</returns> |
| 148 | + public static string SubstringAfter( |
| 149 | + this string value, |
| 150 | + string delimiter, |
| 151 | + string? defaultValue = null, |
| 152 | + StringComparison comparison = StringComparison.Ordinal) |
| 153 | + { |
| 154 | + int index = value.IndexOf(delimiter, comparison); |
| 155 | + |
| 156 | + return index switch |
| 157 | + { |
| 158 | + IndexNotFound => defaultValue ?? value, |
| 159 | + _ => value[(index + delimiter.Length)..value.Length] |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Returns a substring after the last occurrence of the specified delimiter. |
| 165 | + /// </summary> |
| 166 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 167 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 168 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 169 | + /// <returns>Returns a substring after the last occurrence of the specified delimiter.</returns> |
| 170 | + public static string SubstringAfterLast( |
| 171 | + this string value, |
| 172 | + char delimiter, |
| 173 | + string? defaultValue = null) |
| 174 | + { |
| 175 | + int index = value.LastIndexOf(delimiter); |
| 176 | + |
| 177 | + return index switch |
| 178 | + { |
| 179 | + IndexNotFound => defaultValue ?? value, |
| 180 | + _ => value[(index + 1)..value.Length] |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Returns a substring after the last occurrence of the specified delimiter. |
| 186 | + /// </summary> |
| 187 | + /// <param name="value">The original <see cref="string"/> from which to obtain a sub-string.</param> |
| 188 | + /// <param name="delimiter">The delimiter to find within the original string.</param> |
| 189 | + /// <param name="defaultValue">The value to return if the delimiter is not found, which defaults to the original string.</param> |
| 190 | + /// <param name="comparison">Specifies the string comparison rule for the search.</param> |
| 191 | + /// <returns>Returns a substring after the last occurrence of the specified delimiter.</returns> |
| 192 | + public static string SubstringAfterLast( |
| 193 | + this string value, |
| 194 | + string delimiter, |
| 195 | + string? defaultValue = null, |
| 196 | + StringComparison comparison = StringComparison.Ordinal) |
| 197 | + { |
| 198 | + int index = value.LastIndexOf(delimiter, comparison); |
| 199 | + |
| 200 | + return index switch |
| 201 | + { |
| 202 | + IndexNotFound => defaultValue ?? value, |
| 203 | + _ => value[(index + delimiter.Length)..value.Length] |
| 204 | + }; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments