namespace XFEToolBox.Client.Core.Search; /// A filesystem name captured by the index. Searching this model never accesses the disk. public sealed record FileSearchEntry { private readonly string fullPath; private readonly int directoryEnd; private string? directoryPath; private string? searchDirectoryPath; public FileSearchEntry(string FullPath, bool IsDirectory) { fullPath = FullPath ?? throw new ArgumentNullException(nameof(FullPath)); this.IsDirectory = IsDirectory; var end = fullPath.Length; while (end > 0 && IsSeparator(fullPath[end - 1])) end--; var separator = -1; for (var index = end - 1; index >= 0; index--) { if (!IsSeparator(fullPath[index])) continue; separator = index; break; } Name = fullPath[(separator + 1)..end]; // Retain the separator in a drive root (C:\) and in the Unix root (/). directoryEnd = separator == 0 || (separator == 2 && fullPath[1] == ':') ? separator + 1 : separator; SearchPath = NormalizePath(fullPath); } public string FullPath => fullPath; public bool IsDirectory { get; } public string Name { get; } public string DirectoryPath => directoryPath ??= directoryEnd > 0 ? fullPath[..directoryEnd] : string.Empty; internal string SearchPath { get; } internal string SearchDirectoryPath => searchDirectoryPath ??= SearchDirectorySpan.ToString(); internal ReadOnlySpan SearchDirectorySpan { get { var end = Math.Max(0, directoryEnd); while (end > 1 && SearchPath[end - 1] == '\\' && !(end == 3 && SearchPath[1] == ':')) end--; return SearchPath.AsSpan(0, end); } } /// /// Scanner-only factory for a direct child of an already normalized directory. Reuses the /// parent's strings rather than allocating and then discarding one parent path per file. /// The public constructor remains the validating, platform-independent entry point. /// internal static FileSearchEntry FromDirectory(string fullPath, bool isDirectory, string directoryPath, string normalizedDirectoryPath) { var nameStart = directoryPath.Length; if (nameStart < fullPath.Length && IsSeparator(fullPath[nameStart])) nameStart++; return new FileSearchEntry(fullPath, isDirectory, fullPath[nameStart..], directoryPath, normalizedDirectoryPath); } private FileSearchEntry(string fullPath, bool isDirectory, string name, string directoryPath, string normalizedDirectoryPath) { this.fullPath = fullPath; IsDirectory = isDirectory; Name = name; directoryEnd = directoryPath.Length; this.directoryPath = directoryPath; searchDirectoryPath = normalizedDirectoryPath; SearchPath = NormalizePath(fullPath); } private FileSearchEntry(FileSearchEntry source, string directoryPath, string searchDirectoryPath) { fullPath = source.fullPath; directoryEnd = source.directoryEnd; IsDirectory = source.IsDirectory; Name = source.Name; this.directoryPath = directoryPath; SearchPath = source.SearchPath; this.searchDirectoryPath = searchDirectoryPath; } internal FileSearchEntry ShareDirectory(string directoryPath, string searchDirectoryPath) { // Preserve the exact spelling of the path while sharing the overwhelmingly common // identical parent strings. Do not globally intern filenames or user-supplied paths. if (!fullPath.AsSpan(0, Math.Max(0, directoryEnd)).SequenceEqual(directoryPath)) directoryPath = DirectoryPath; if (!SearchDirectorySpan.SequenceEqual(searchDirectoryPath)) searchDirectoryPath = SearchDirectoryPath; return ReferenceEquals(this.directoryPath, directoryPath) && ReferenceEquals(this.searchDirectoryPath, searchDirectoryPath) ? this : new(this, directoryPath, searchDirectoryPath); } // Directory strings are lazily materialized and then shared by the index. Cache state must // not change this immutable value's equality or hash code after a consumer has read it. public bool Equals(FileSearchEntry? other) => other is not null && IsDirectory == other.IsDirectory && fullPath.Equals(other.fullPath, StringComparison.Ordinal); public override int GetHashCode() => HashCode.Combine(fullPath, IsDirectory); public void Deconstruct(out string FullPath, out bool IsDirectory) { FullPath = fullPath; IsDirectory = this.IsDirectory; } internal static bool IsSeparator(char value) => value is '/' or '\\'; internal static string NormalizePath(string value) { value = value.Replace('/', '\\'); var end = value.Length; while (end > 1 && value[end - 1] == '\\' && !(end == 3 && value[1] == ':')) end--; return end == value.Length ? value : value[..end]; } } public sealed record FileSearchMatch(FileSearchEntry Entry, int Score, bool ContentMatch = false, string? Snippet = null); public sealed record FileSearchResults(IReadOnlyList Items, int TotalMatches, TimeSpan Elapsed);