| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Reflection.Internal; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 7 | | { |
| | | 8 | | internal readonly struct UserStringHeap |
| | | 9 | | { |
| | | 10 | | internal readonly MemoryBlock Block; |
| | | 11 | | |
| | | 12 | | public UserStringHeap(MemoryBlock block) |
| | 0 | 13 | | { |
| | 0 | 14 | | this.Block = block; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | internal string GetString(UserStringHandle handle) |
| | 0 | 18 | | { |
| | | 19 | | int offset, size; |
| | 0 | 20 | | if (!Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out offset, out size)) |
| | 0 | 21 | | { |
| | 0 | 22 | | return string.Empty; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | // Spec: Furthermore, there is an additional terminal byte (so all byte counts are odd, not even). |
| | | 26 | | // The size in the blob header is the length of the string in bytes + 1. |
| | 0 | 27 | | return Block.PeekUtf16(offset, size & ~1); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | internal UserStringHandle GetNextHandle(UserStringHandle handle) |
| | 0 | 31 | | { |
| | | 32 | | int offset, size; |
| | 0 | 33 | | if (!Block.PeekHeapValueOffsetAndSize(handle.GetHeapOffset(), out offset, out size)) |
| | 0 | 34 | | { |
| | 0 | 35 | | return default(UserStringHandle); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | int nextIndex = offset + size; |
| | 0 | 39 | | if (nextIndex >= Block.Length) |
| | 0 | 40 | | { |
| | 0 | 41 | | return default(UserStringHandle); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return UserStringHandle.FromOffset(nextIndex); |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |