diff --git a/src/HexManiac.Core/Models/Runs/ArrayRunElementSegment.cs b/src/HexManiac.Core/Models/Runs/ArrayRunElementSegment.cs index 8c2395e9..9d240aef 100644 --- a/src/HexManiac.Core/Models/Runs/ArrayRunElementSegment.cs +++ b/src/HexManiac.Core/Models/Runs/ArrayRunElementSegment.cs @@ -509,9 +509,17 @@ namespace HavenSoft.HexManiac.Core.Models.Runs { var result = new StringBuilder("-"); var options = rawData.GetBitOptions(SourceArrayName); + // handle edge cases: no options, or no options for some set bits + var highestByteSet = Length - 1; + while (rawData[offset + highestByteSet] == 0 && highestByteSet > 0) { + highestByteSet -= 1; + } + var highestBitSet = (int)Math.Log(rawData[offset + highestByteSet], 2); + var unknownBitsAreSet = options == null || (highestByteSet * 8) + highestBitSet >= options.Count; + for (int i = 0; i < Length; i++) { var bits = rawData[offset + i]; - if (options == null) { + if (unknownBitsAreSet) { result.Append(bits.ToString("X2")); } else { var optionOffset = i << 3; diff --git a/src/HexManiac.Tests/Before_Baseclass/ArrayRunTests.cs b/src/HexManiac.Tests/Before_Baseclass/ArrayRunTests.cs index 84ca3961..fdb72679 100644 --- a/src/HexManiac.Tests/Before_Baseclass/ArrayRunTests.cs +++ b/src/HexManiac.Tests/Before_Baseclass/ArrayRunTests.cs @@ -1091,6 +1091,21 @@ namespace HavenSoft.HexManiac.Tests { Assert.True(match); } + [Fact] + public void BitFieldWithUnknownBitSet_CopyPaste_CorrectBitsSet() { + var fileSystem = new StubFileSystem(); + Model.SetList(new ModelDelta(), "bits", "adam", "bob", "carl", "dave"); + ViewPort.Edit("^table[a|b[]bits]3 00 10 "); + + ViewPort.SelectionStart = new Point(1, 0); + ViewPort.Copy.Execute(fileSystem); + + ViewPort.SelectionStart = new Point(2, 0); + ViewPort.Edit(fileSystem.CopyText); + + Assert.Equal(0x10, Model[2]); + } + private static void StandardSetup(out byte[] data, out PokemonModel model, out ViewPort viewPort) { data = new byte[0x200]; model = new PokemonModel(data);