Nexus-Mods / NexusMods.Paths

Custom path library used by NexusMods.App.
https://nexus-mods.github.io/NexusMods.Paths/
GNU General Public License v3.0
6 stars 0 forks source link

Bug + Perf: RelativePath.GetHashCode is Case Sensitive #25

Closed Sewer56 closed 11 months ago

Sewer56 commented 1 year ago

Bug Report

Summary

RelativePath.GetHashCode uses a custom vectorised function for calculating the HashCode of a string that I once made for my Virtual FileSystem. This function is case-sensitive (as it hashes the raw bytes), however the current specification suggests that paths should be compared case insensitive.

More precisely, our GetHashCode is case sensitive, while our Equals is case insensitive, which breaks the contract.

Steps to reproduce

Hash two RelativePath(s) with different casing, and note the hash differs.

What is the expected behaviour?

For any two strings which are equal with Equals, their GetHashCode must match too.

Design

Apply the following steps:

Note:

Use TextInfo.ChangeCase<ToUpperConversion> for changing the case. It contains a backported vectorised implementation from .NET 8. We can switch to regular ToLower Span extension once we start targeting .NET 8. If you want, can use #if !NET7_0_OR_GREATER define to include conditional, so we don't forget to replace it.

Other information

When the Path library was initially rewritten by me, the requirements and implementation were a bit different. Back then we converted RelativePath(s) into lowercase. However, circumstances have changed, and we stopped doing this a while back.

Sewer56 commented 11 months ago

Suggested for prioritization as for https://github.com/Nexus-Mods/NexusMods.Paths/issues/26 , some tests needed disabling due to this bug.