public void Test_StringPool_Add_Misc() { var pool = new StringPool(); string hello = nameof(hello), helloworld = nameof(helloworld), windowsCommunityToolkit = nameof(windowsCommunityToolkit); Assert.IsFalse(pool.TryGet(hello.AsSpan(), out _)); Assert.IsFalse(pool.TryGet(helloworld.AsSpan(), out _)); Assert.IsFalse(pool.TryGet(windowsCommunityToolkit.AsSpan(), out _)); pool.Add(hello); pool.Add(helloworld); pool.Add(windowsCommunityToolkit); Assert.IsTrue(pool.TryGet(hello.AsSpan(), out string?hello2)); Assert.IsTrue(pool.TryGet(helloworld.AsSpan(), out string?world2)); Assert.IsTrue(pool.TryGet(windowsCommunityToolkit.AsSpan(), out string?windowsCommunityToolkit2)); Assert.AreSame(hello, hello2); Assert.AreSame(helloworld, world2); Assert.AreSame(windowsCommunityToolkit, windowsCommunityToolkit2); }
/// <summary> /// Make sure string is added to pool /// </summary> internal static void Prepare(StringPool pool, string value, int resourceId = -1) { if (value != null) { pool.Add(value, resourceId); } }
public override void Write(Stream destination) { destination.Seek(16, SeekOrigin.Begin); var stringPool1 = new StringPool(); stringPool1.GroupAlignment = 0x10; long aetFileEntriesPosition = destination.Position; foreach (var entry in fileEntries) { DataStream.WriteUInt32(destination, entry.Id); stringPool1.Add(destination, entry.Name); stringPool1.Add(destination, entry.FileName); DataStream.WriteUInt32(destination, entry.Index); DataStream.WriteUInt32(destination, entry.SpriteId); } DataStream.Pad(destination, 0x10); var stringPool2 = new StringPool(); stringPool2.GroupAlignment = 0x4; long aetEntriesPosition = destination.Position; foreach (var entry in entries) { DataStream.WriteUInt32(destination, entry.Id); stringPool2.Add(destination, entry.Name, entry.GroupId); DataStream.WriteUInt16(destination, entry.Index); DataStream.WriteUInt16(destination, entry.GroupId); } stringPool1.Write(destination); stringPool2.Write(destination); destination.Seek(0, SeekOrigin.Begin); DataStream.WriteUInt32(destination, (uint)fileEntries.Count); DataStream.WriteUInt32(destination, (uint)aetFileEntriesPosition); DataStream.WriteUInt32(destination, (uint)entries.Count); DataStream.WriteUInt32(destination, (uint)aetEntriesPosition); }
public override void Write(Stream destination) { var stringPool = new StringPool(); stringPool.StringAlignment = 0x2; foreach (string value in strings) { stringPool.Add(destination, value); } DataStream.Pad(destination, 0x10); stringPool.Write(destination); }
public void Test_StringPool_Add_Single() { var pool = new StringPool(); string hello = nameof(hello); Assert.IsFalse(pool.TryGet(hello.AsSpan(), out _)); pool.Add(hello); Assert.IsTrue(pool.TryGet(hello.AsSpan(), out string?hello2)); Assert.AreSame(hello, hello2); }
public void Test_StringPool_Add_Overwrite() { var pool = new StringPool(); var today = DateTime.Today; var text1 = ToStringNoInlining(today); pool.Add(text1); Assert.IsTrue(pool.TryGet(text1.AsSpan(), out string?result)); Assert.AreSame(text1, result); var text2 = ToStringNoInlining(today); pool.Add(text2); Assert.IsTrue(pool.TryGet(text2.AsSpan(), out result)); Assert.AreNotSame(text1, result); Assert.AreSame(text2, result); }
public void Test_StringPool_GetOrAdd_Encoding_Misc() { var pool = new StringPool(); string helloworld = nameof(helloworld); pool.Add(helloworld); Span <byte> span = Encoding.UTF8.GetBytes(nameof(helloworld)); string helloworld2 = pool.GetOrAdd(span, Encoding.UTF8); Assert.AreSame(helloworld, helloworld2); string windowsCommunityToolkit = nameof(windowsCommunityToolkit); Span <byte> span2 = Encoding.ASCII.GetBytes(windowsCommunityToolkit); string windowsCommunityToolkit2 = pool.GetOrAdd(span2, Encoding.ASCII), windowsCommunityToolkit3 = pool.GetOrAdd(windowsCommunityToolkit); Assert.AreSame(windowsCommunityToolkit2, windowsCommunityToolkit3); }
/// <summary> /// 注意要和SystemLib中的String的对象格式相同 /// </summary> /// <param name="value"></param> /// <returns>绝对地址</returns> public uint AddConstantString(string value) { if (!StringPool.TryGetValue(value, out HeapData data)) { // 分配byte数组 HeapData stringData = MallocCharArray(Encoding.UTF8.GetByteCount(value)); Encoding.UTF8.GetBytes(value, new Span <byte>(stringData.Data, HeapData.ArrayLengthSize + HeapData.MiscDataSize, stringData.Data.Length - HeapData.ArrayLengthSize - HeapData.MiscDataSize)); // String对象 byte[] vs = new byte[HeapData.MiscDataSize + HeapData.StringLengthSize + HeapData.StringDataSize]; // 头部信息可以不填,因为MethodArea是内存的边界,GC不会继续walk // 长度信息 BitConverter.TryWriteBytes(new Span <byte>(vs, HeapData.MiscDataSize, HeapData.StringLengthSize), value.Length); // Data信息 BitConverter.TryWriteBytes(new Span <byte>(vs, HeapData.MiscDataSize + HeapData.StringLengthSize, HeapData.StringDataSize), MemoryMap.MapToAbsolute(stringData.Offset, MemoryTag.METHOD)); // 字符串 data = Malloc(vs); StringPool.Add(value, data); } return(MemoryMap.MapToAbsolute(data.Offset, MemoryTag.METHOD)); }
public Module(string name) { ModuleNameIndex = StringPool.Add(name); }