mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
Return a copy of the ArrayPoolBufferWriter buffer in Ascii85, AsciiHex and RunLength filters and fix #964
This commit is contained in:
@@ -22,7 +22,11 @@ O<DJ+*.@<*K0@<6L(Df-\0Ec5e;DffZ(EZee.Bl.9pF""AGXBPCsi + DGm >@3BB / F * &OCAfu2
|
||||
|
||||
var result = filter.Decode(bytes, dictionary, 0);
|
||||
|
||||
var text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, " +
|
||||
"that by a perseverance of delight in the continued and indefatigable generation of knowledge, " +
|
||||
@@ -37,7 +41,11 @@ O<DJ+*.@<*K0@<6L(Df-\0Ec5e;DffZ(EZee.Bl.9pF""AGXBPCsi + DGm >@3BB / F * &OCAfu2
|
||||
|
||||
var result = filter.Decode(bytes, dictionary, 1);
|
||||
|
||||
var text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal("Man \0\0\0\0is d", text);
|
||||
}
|
||||
@@ -104,8 +112,12 @@ Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>";
|
||||
|
||||
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
|
||||
|
||||
var text = Encoding.ASCII.GetString(result.ToArray());
|
||||
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
|
||||
}
|
||||
|
||||
@@ -124,11 +136,57 @@ Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&";
|
||||
|
||||
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
|
||||
|
||||
var text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecodeParallel()
|
||||
{
|
||||
Parallel.For(0, 100_000, i =>
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
var bytes = Encoding.ASCII.GetBytes("9jqo^zBlbD-");
|
||||
|
||||
var result = filter.Decode(bytes, dictionary, 1);
|
||||
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal("Man \0\0\0\0is d", text);
|
||||
}
|
||||
else
|
||||
{
|
||||
const string input =
|
||||
@"0d&.mDdmGg4?O`>9P&*SFD)dS2E2gC4pl@QEb/Zr$8N_r$:7]!01IZ=0eskNAdU47<+?7h+B3Ol2_m!C+?)#1+B1
|
||||
`9>:<KhASu!rA7]9oF*)G6@;U'.@ps6t@V$[&ART*lARTXoCj@HP2DlU*/0HBI+B1r?0H_r%1a#ac$<nof.3LB""+=MAS+D58'ATD3qCj@.
|
||||
F@;@;70ea^uAKYi.Eb-A7E+*6f+EV:*DBN1?0ek+_+B1r?<%9""=ASu!rA7]9oF*)G6@;U'<.3MT)$8<SS1,pCU6jd-H;e7
|
||||
C#1,U1&Ft""Og2'=;YEa`c,ASu!rA8,po+Dk\3BQ%F&+CT;%+CQ]A1,'h!Ft""Oh2'=;UBl%3eCh4`'DBMbD7O]H>0H_br.:""&q8d[6p/M
|
||||
T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p.!0AMEb031ATMF#F<G%,DIIR2+Cno
|
||||
&@3B9%+CT.1.3LK*+=KNS6V0ilAoD^,@<=+N>p**=$</Jt-rY&$AKYo'+EV:.+Cf>,E,oN2F(oQ1+D#G#De*R""B-;&&FD,T'F!+n3AKY4b
|
||||
F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+=
|
||||
Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>";
|
||||
|
||||
var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);
|
||||
|
||||
#if !NET
|
||||
string text = Encoding.ASCII.GetString(result.ToArray());
|
||||
#else
|
||||
string text = Encoding.ASCII.GetString(result.Span);
|
||||
#endif
|
||||
|
||||
Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
/// <inheritdoc />
|
||||
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
|
||||
{
|
||||
var asciiBuffer = new byte[5];
|
||||
Span<byte> asciiBuffer = stackalloc byte[5];
|
||||
|
||||
var index = 0;
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
WriteData(asciiBuffer, index, writer);
|
||||
}
|
||||
|
||||
return writer.WrittenMemory;
|
||||
return writer.WrittenMemory.ToArray();
|
||||
}
|
||||
|
||||
private static void WriteData(Span<byte> ascii, int index, ArrayPoolBufferWriter<byte> writer)
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
WriteHexToByte(pair, writer);
|
||||
}
|
||||
|
||||
return writer.WrittenMemory;
|
||||
return writer.WrittenMemory.ToArray();
|
||||
}
|
||||
|
||||
private static void WriteHexToByte(ReadOnlySpan<byte> hexBytes, ArrayPoolBufferWriter<byte> writer)
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
return output.WrittenMemory;
|
||||
return output.WrittenMemory.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user