@@ -53,6 +53,9 @@ func applyError(err error, args ...interface{}) error {
5353
5454 e , ok := err .(* ApplyError )
5555 if ! ok {
56+ if err == io .EOF {
57+ err = io .ErrUnexpectedEOF
58+ }
5659 e = & ApplyError {err : err }
5760 }
5861 for _ , arg := range args {
@@ -115,8 +118,8 @@ func (f *TextFragment) ApplyStrict(dst io.Writer, src LineReader) error {
115118 // line numbers are zero-indexed, positions are one-indexed
116119 limit := f .OldPosition - 1
117120
118- // an EOF is allowed here: the fragment applies to the last line of the
119- // source but it does not have a newline character
121+ // io. EOF is acceptable here: the first line of the patch is the last of
122+ // the source and it has no newline character
120123 nextLine , n , err := copyLines (dst , src , limit )
121124 if err != nil && err != io .EOF {
122125 return applyError (err , lineNum (n ))
@@ -127,16 +130,16 @@ func (f *TextFragment) ApplyStrict(dst io.Writer, src LineReader) error {
127130 if err := applyTextLine (dst , nextLine , line ); err != nil {
128131 return applyError (err , lineNum (n ), fragLineNum (i ))
129132 }
130- if fromSrc ( line ) {
133+ if line . Old ( ) {
131134 used ++
132135 }
133136 // advance reader if the next fragment line appears in src and we're behind
134- if i < len (f .Lines )- 1 && fromSrc ( f .Lines [i + 1 ]) && int64 (n )- limit < used {
137+ if i < len (f .Lines )- 1 && f .Lines [i + 1 ]. Old ( ) && int64 (n )- limit < used {
135138 nextLine , n , err = src .ReadLine ()
136- if err != nil {
137- if err == io .EOF {
138- err = io . ErrUnexpectedEOF
139- }
139+ switch {
140+ case err == io .EOF && f . Lines [ i + 1 ]. NoEOL ():
141+ continue
142+ case err != nil :
140143 return applyError (err , lineNum (n ), fragLineNum (i + 1 )) // report for _next_ line in fragment
141144 }
142145 }
@@ -159,14 +162,10 @@ func applyTextLine(dst io.Writer, src string, line Line) (err error) {
159162 return
160163}
161164
162- func fromSrc (line Line ) bool {
163- return line .Op != OpAdd
164- }
165-
166165// copyLines copies from src to dst until the line at limit, exclusive. Returns
167- // the line at limit and the line number. The line number may not equal the
168- // limit if and only if a non-EOF error occurs . A negative limit means the
169- // first read should return io.EOF and no data .
166+ // the line at limit and the line number. If the error is nil or io.EOF, the
167+ // line number equals limit . A negative limit checks that the source has no
168+ // more lines to read .
170169func copyLines (dst io.Writer , src LineReader , limit int64 ) (string , int , error ) {
171170 // TODO(bkeyes): fix int vs int64 for limit and return value
172171 for {
0 commit comments