draconware-dev / SpanExtensions.Net

SpanExtensions.Net aims to make ReadOnlySpan<T> and Span<T> more accessible and overall better integrated into the .Net Ecosystem, by providing alternatives for many missing Extension Methods for Span<T>, ranging from string.Split() over Enumerable.Skip() and Enumerable.Take() to an improved ReadOnlySpan<T>.IndexOf().
MIT License
17 stars 5 forks source link

Doesn't work with Path.PathSeparator as delimeter, but works with '\\' #4

Closed VafArt closed 6 months ago

VafArt commented 6 months ago

Describe the bug
Doesnt work with Path.PathSeparator as delimeter, but works with '\'

To Reproduce
using SpanExtensions; var path = @"C:\Users\ArthurPC\source\repos\a.vafin\PhotoService\products"; foreach(var e in ReadOnlySpanExtensions.Split(path, Path.PathSeparator)) // doesn't split, but works with '\' in windows { Console.WriteLine(e.ToString()); }

Expected behavior
Console Input: C: Users ArthurPC source repos a.vafin PhotoService products

dragon7307 commented 6 months ago

Hey, Thank you for reporting this bug. However, this "bug", does not appear to be a bug of the library at all, but instead it seems to me that there is a misunderstanding concerning .Net terminology here. You are trying to delimit a path delimited by a "\". Path.PathSeparator is defined as a semicolon on windows, as can be seen here. As the name suggests, Path.PathSeparator is used to separate actual paths, like yours from another, for example in environment variables. So essentially you have been trying to delimit your path with a semicolon as a delimiter, and you got the correct output, since your path didn't include any semicolons. What you probably actually wanted to do, is use Path.DirectorySeparatorChar, which returns the platform specific delimiter for directories and files, not entire paths. This should fix your problem. If not, feel free to open another issue.

I do not know whether it was done in this case for clarity or not, but the library is designed in such a way, that there is no need, to call ReadOnlySpanExtensions.Split(path, separator), but instead, it is way easier, to consume your path through an extension method path.Split(separator). If your path is not a ReadonlySpan, you could just chain .AsSpan(). This however is of course up to you.