rsc / pdf

PDF reader
BSD 3-Clause "New" or "Revised" License
510 stars 327 forks source link

Reader.Page() - Is it 0-indexed or 1-indexed? #18

Closed Northern-Lights closed 6 years ago

Northern-Lights commented 6 years ago

https://github.com/rsc/pdf/blob/master/page.go#L22

The GoDoc for this function says that it's 1-indexed, but the comment on L22 says it's 0-indexed. When calling the method as such:

r.Page(0)

we land in an infinite loop, because the initial num-- adjustment puts num at -1, and therefore we never find a page. Maybe an error condition should be returned in case a 0 is passed as an argument?

rsc commented 6 years ago

Thanks - fixed infinite loop. To answer your question, in this code:

// Page returns the page for the given page number.
// Page numbers are indexed starting at 1, not 0.
// If the page is not found, Page returns a Page with p.V.IsNull().
func (r *Reader) Page(num int) Page {
    num-- // now 0-indexed

On entry num is 1-indexed. After num--, num is 1 less than before and is now 0-indexed.