From af6a9370336a94147c5f93eef0978c34ab504482 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sun, 6 Sep 2020 04:38:41 +0200 Subject: Go: avoid non-deterministic output The code has even turned out simpler. --- pdf/pdf.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/pdf/pdf.go b/pdf/pdf.go index f871c70..4f1f5e1 100644 --- a/pdf/pdf.go +++ b/pdf/pdf.go @@ -853,30 +853,19 @@ func (u *Updater) FlushUpdates() { return updated[i] < updated[j] }) - groups := make(map[uint]uint) - for i := 0; i < len(updated); { - start, count := updated[i], uint(1) - for i++; i != len(updated) && updated[i] == start+count; i++ { - count++ - } - groups[start] = count - } - - // Taking literally "Each cross-reference section begins with a line - // containing the keyword xref. Following this line are one or more - // cross-reference subsections." from 3.4.3 in PDF Reference. - if len(groups) == 0 { - groups[0] = 0 - } - buf := bytes.NewBuffer(u.Document) startXref := buf.Len() + 1 buf.WriteString("\nxref\n") - for start, count := range groups { - fmt.Fprintf(buf, "%d %d\n", start, count) - for i := uint(0); i < count; i++ { - ref := u.xref[start+uint(i)] + for i := 0; i < len(updated); { + start, stop := updated[i], updated[i]+1 + for i++; i < len(updated) && updated[i] == stop; i++ { + stop++ + } + + fmt.Fprintf(buf, "%d %d\n", start, stop-start) + for ; start < stop; start++ { + ref := u.xref[start] if ref.nonfree { fmt.Fprintf(buf, "%010d %05d n \n", ref.offset, ref.generation) } else { @@ -885,6 +874,13 @@ func (u *Updater) FlushUpdates() { } } + // Taking literally "Each cross-reference section begins with a line + // containing the keyword xref. Following this line are one or more + // cross-reference subsections." from 3.4.3 in PDF Reference. + if len(updated) == 0 { + fmt.Fprintf(buf, "%d %d\n", 0, 0) + } + u.Trailer["Size"] = NewNumeric(float64(u.xrefSize)) trailer := NewDict(u.Trailer) -- cgit v1.2.3-70-g09d2