| | 1 | | #if NET8_0_OR_GREATER |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace PropertyGridHelpers.Support |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Provides logic to extract base names from resource names, removing a specified assembly prefix and |
| | 10 | | /// the standard <c>.resources</c> extension. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// This implementation is useful when working with resource files that are embedded with fully qualified names, |
| | 14 | | /// allowing you to obtain simpler base names for further processing. |
| | 15 | | /// </remarks> |
| | 16 | | /// <seealso cref="IResourceBaseNameExtractor"/> |
| | 17 | | internal class RangeBasedBaseNameExtractor : IResourceBaseNameExtractor |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Extracts base names from the specified resource names, removing the standard <c>.resources</c> |
| | 21 | | /// extension and stripping the provided assembly prefix if present. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="assemblyPrefix"> |
| | 24 | | /// The assembly prefix to remove from the beginning of resource names (e.g., the default namespace |
| | 25 | | /// of the assembly). If the base name starts with this prefix, it will be removed along with any |
| | 26 | | /// subsequent dot separator. |
| | 27 | | /// </param> |
| | 28 | | /// <param name="resourceNames"> |
| | 29 | | /// The full resource names to process, typically retrieved via <see cref="System.Reflection.Assembly.GetManifes |
| | 30 | | /// </param> |
| | 31 | | /// <returns> |
| | 32 | | /// An ordered list of distinct base names with the <c>.resources</c> extension and the assembly prefix removed. |
| | 33 | | /// </returns> |
| | 34 | | public IList<string> ExtractBaseNames(string assemblyPrefix, string[] resourceNames) |
| 16 | 35 | | { |
| | 36 | | const string resourceExtension = ".resources"; |
| 16 | 37 | | var resourceExtensionLength = resourceExtension.Length; |
| 16 | 38 | | var baseNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 39 | |
|
| 176 | 40 | | foreach (var name in resourceNames) |
| 64 | 41 | | { |
| 64 | 42 | | if (name.EndsWith(resourceExtension, StringComparison.OrdinalIgnoreCase)) |
| 48 | 43 | | { |
| 48 | 44 | | var baseName = name[..^resourceExtensionLength]; |
| | 45 | |
|
| 48 | 46 | | if (baseName.StartsWith(assemblyPrefix, StringComparison.OrdinalIgnoreCase)) |
| 48 | 47 | | baseName = baseName[assemblyPrefix.Length..].TrimStart('.'); |
| | 48 | |
|
| 48 | 49 | | _ = baseNames.Add(baseName); |
| 48 | 50 | | } |
| 64 | 51 | | } |
| | 52 | |
|
| 64 | 53 | | return [.. baseNames.OrderBy(x => x, StringComparer.OrdinalIgnoreCase)]; |
| 16 | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| | 57 | | #endif |