public unsafe void ExpandAfterShrinkPool_AllocatesDefault() { LazyUTF8String.ResetPool(new MockTracer(), DefaultIndexEntryCount); string fileAndFolderNames = "folderSDKfile.txtDKfolders"; UseASCIIBytePointer( fileAndFolderNames, bufferPtr => { int initialBytePoolSize = LazyUTF8String.BytePoolSize(); int initialStringPoolSize = LazyUTF8String.StringPoolSize(); LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String.FromByteArray(bufferPtr + 17, 2); LazyUTF8String.ShrinkPool(); CheckPoolSizes(expectedBytePoolSize: 6, expectedStringPoolSize: 2); LazyUTF8String.FreePool(); CheckPoolSizes(expectedBytePoolSize: 6, expectedStringPoolSize: 2); LazyUTF8String.ShrinkPool(); CheckPoolSizes(expectedBytePoolSize: 0, expectedStringPoolSize: 0); LazyUTF8String.FromByteArray(bufferPtr + 6, 3); CheckPoolSizes(expectedBytePoolSize: initialBytePoolSize, expectedStringPoolSize: initialStringPoolSize); LazyUTF8String.ShrinkPool(); CheckPoolSizes(expectedBytePoolSize: 3, expectedStringPoolSize: 1); }); }
private static unsafe LazyUTF8String ConstructLazyUTF8String(string name) { byte[] buffer = Encoding.ASCII.GetBytes(name); fixed(byte *bufferPtr = buffer) { return(LazyUTF8String.FromByteArray(bufferPtr, name.Length)); } }
public unsafe void CaseInsensitiveEquals_SameNameDifferentCase2_EqualsTrue() { UseASCIIBytePointer( "FOlderonefile.txtFolder", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 0, 6); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 6); firstFolder.CaseInsensitiveEquals(secondFolder).ShouldBeTrue(nameof(firstFolder.CaseInsensitiveEquals)); }); }
public unsafe void NonASCIICharacters_Compare() { UseUTF8BytePointer( "folderSDKfile.txtريلٌأكتوبرDKfolders", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 20); firstFolder.CaseInsensitiveCompare(secondFolder).ShouldBeAtMost(-1, nameof(firstFolder.CaseInsensitiveCompare)); }); }
public unsafe void CaseInsensitiveCompare_EqualsGreaterThanZero2() { UseASCIIBytePointer( "folderSDKfile.txtDKfolders", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 2); firstFolder.CaseInsensitiveCompare(secondFolder).ShouldBeAtLeast(1, nameof(firstFolder.CaseInsensitiveCompare)); }); }
public unsafe void CaseInsensitiveCompare_EqualsLessThanZero() { UseASCIIBytePointer( "folderonefile.txtfolders", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 0, 6); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 7); firstFolder.CaseInsensitiveCompare(secondFolder).ShouldBeAtMost(-1, nameof(firstFolder.CaseInsensitiveCompare)); }); }
public unsafe void CaseInsensitiveEquals_OneNameShorterEqualsFalse() { UseASCIIBytePointer( "folderonefile.txtFold", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 0, 6); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 4); firstFolder.CaseInsensitiveEquals(secondFolder).ShouldBeFalse(nameof(firstFolder.CaseInsensitiveEquals)); }); }
public unsafe void PoolSizeCheck() { UseASCIIBytePointer( "folderSDKfile.txtDKfolders", bufferPtr => { int bytePoolSizeBeforeFreePool = LazyUTF8String.BytePoolSize(); int stringPoolSizeBeforeFreePool = LazyUTF8String.StringPoolSize(); LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 2); CheckPoolSizes(bytePoolSizeBeforeFreePool, stringPoolSizeBeforeFreePool); }); }
public unsafe void GetString() { UseASCIIBytePointer( "folderonefile.txt", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 0, 6); firstFolder.GetString().ShouldEqual("folder"); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); secondFolder.GetString().ShouldEqual("one"); LazyUTF8String file = LazyUTF8String.FromByteArray(bufferPtr + 9, 8); file.GetString().ShouldEqual("file.txt"); }); }
public unsafe void ShrinkPool_DecreasesPoolSize() { LazyUTF8String.ResetPool(new MockTracer(), DefaultIndexEntryCount); string fileAndFolderNames = "folderSDKfile.txtDKfolders"; UseASCIIBytePointer( fileAndFolderNames, bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 17, 2); LazyUTF8String.ShrinkPool(); CheckPoolSizes(expectedBytePoolSize: 6, expectedStringPoolSize: 2); }); }
public unsafe void GetString_NonASCII() { UseUTF8BytePointer( "folderoneريلٌأكتوبرfile.txt", bufferPtr => { LazyUTF8String firstFolder = LazyUTF8String.FromByteArray(bufferPtr + 0, 6); firstFolder.GetString().ShouldEqual("folder"); LazyUTF8String secondFolder = LazyUTF8String.FromByteArray(bufferPtr + 6, 3); secondFolder.GetString().ShouldEqual("one"); LazyUTF8String utf8 = LazyUTF8String.FromByteArray(bufferPtr + 9, 20); utf8.GetString().ShouldEqual("ريلٌأكتوبر"); LazyUTF8String file = LazyUTF8String.FromByteArray(bufferPtr + 29, 8); file.GetString().ShouldEqual("file.txt"); }); }
public unsafe void PoolSizeIncreasesAfterShrinking() { LazyUTF8String.ResetPool(); string fileAndFolderNames = "folderSDKfile.txtDKfolders"; UseASCIIBytePointer( fileAndFolderNames, bufferPtr => { int initialBytePoolSize = LazyUTF8String.BytePoolSize(); int initialStringPoolSize = LazyUTF8String.StringPoolSize(); LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String.FromByteArray(bufferPtr + 17, 2); LazyUTF8String.ShrinkPool(); CheckPoolSizes(expectedBytePoolSize: 6, expectedStringPoolSize: 2); LazyUTF8String.FromByteArray(bufferPtr + 6, 3); LazyUTF8String.FromByteArray(bufferPtr + 17, 2); LazyUTF8String.FromByteArray(bufferPtr, 6); CheckPoolSizes(expectedBytePoolSize: 6 + initialBytePoolSize, expectedStringPoolSize: 2 + initialStringPoolSize); }); }
public unsafe void ParsePath() { this.PathBuffer[this.PathLength] = 0; // The index of that path part that is after the path separator int currentPartStartIndex = 0; // The index to start looking for the next path separator // Because the previous final separator is stored and we know where the previous path will be replaced // the code can use the previous final separator to start looking from that point instead of having to // run through the entire path to break it apart /* Example: * Previous path = folder/where/previous/separator/is/used/file.txt * This path = folder/where/previous/separator/is/used/file2.txt * ^ ^ * this.previousFinalSeparatorIndex | * this.ReplaceIndex * * folder/where/previous/separator/is/used/file2.txt * ^^ * currentPartStartIndex| * forLoopStartIndex */ int forLoopStartIndex = 0; fixed(byte *pathPtr = this.PathBuffer) { if (this.previousFinalSeparatorIndex < this.ReplaceIndex && !this.RangeContains(pathPtr + this.ReplaceIndex, this.PathLength - this.ReplaceIndex, PathSeparatorCode)) { // Only need to parse the last part, because the rest of the string is unchanged // The logical thing to do would be to start the for loop at previousFinalSeparatorIndex+1, but two // repeated / characters would make an invalid path, so we'll assume that git would not have stored that path forLoopStartIndex = this.previousFinalSeparatorIndex + 2; // we still do need to start the current part's index at the correct spot, so subtract one for that currentPartStartIndex = forLoopStartIndex - 1; this.NumParts--; this.HasSameParentAsLastEntry = true; } else { this.NumParts = 0; this.ClearLastParent(); } int partIndex = this.NumParts; byte *forLoopPtr = pathPtr + forLoopStartIndex; for (int i = forLoopStartIndex; i < this.PathLength + 1; i++) { if (*forLoopPtr == PathSeparatorCode) { this.PathParts[partIndex] = LazyUTF8String.FromByteArray(pathPtr + currentPartStartIndex, i - currentPartStartIndex); partIndex++; currentPartStartIndex = i + 1; this.NumParts++; this.previousFinalSeparatorIndex = i; } ++forLoopPtr; } // We unrolled the final part calculation to after the loop, to avoid having to do a 0-byte check inside the for loop this.PathParts[partIndex] = LazyUTF8String.FromByteArray(pathPtr + currentPartStartIndex, this.PathLength - currentPartStartIndex); this.NumParts++; } }