@@ -51,50 +51,69 @@ export function applyPatch(source, uniDiff, options = {}) {
51
51
return true ;
52
52
}
53
53
54
- // Search best fit offsets for each hunk based on the previous ones
55
- for ( let i = 0 ; i < hunks . length ; i ++ ) {
56
- let hunk = hunks [ i ] ,
54
+ function distanceIterator ( toPos , minLine , maxLine ) {
55
+ let wantForward = true ,
57
56
backwardExhausted = false ,
58
57
forwardExhausted = false ,
59
- localOffset = 0 ,
60
- toPos = offset + hunk . oldStart - 1 ,
61
- maxLine = lines . length - hunk . oldLines ,
62
- toCheck ;
58
+ localOffset = 1 ;
59
+
60
+ return function iterator ( ) {
61
+ if ( wantForward && ! forwardExhausted ) {
62
+ if ( backwardExhausted ) {
63
+ localOffset ++ ;
64
+ } else {
65
+ wantForward = false ;
66
+ }
67
+
68
+ // Check if trying to fit beyond text length, and if not, check it fits
69
+ // after offset location (or desired location on first iteration)
70
+ if ( toPos + localOffset <= maxLine ) {
71
+ return localOffset ;
72
+ }
63
73
64
- for ( ; ; ) {
65
- // Check if trying to fit beyond text length, and if not, check it fits
66
- // after offset location (or desired location on first iteration)
67
- toCheck = toPos + localOffset ;
68
- if ( maxLine < toCheck ) {
69
74
forwardExhausted = true ;
70
- } else if ( hunkFits ( hunk , toCheck ) ) {
71
- hunk . offset = offset += localOffset ;
72
- break ;
73
75
}
74
76
75
- // If we tried to fit hunk before text beginning and beyond text lenght,
76
- // then hunk can't be fit on the text so we raise an error
77
- if ( backwardExhausted && forwardExhausted ) {
78
- return false ;
79
- }
77
+ if ( ! backwardExhausted ) {
78
+ if ( ! forwardExhausted ) {
79
+ wantForward = true ;
80
+ }
80
81
81
- // Reset checks of trying to fit outside text limits and increase offset
82
- // of the current hunk relative to its desired location
83
- backwardExhausted = false ;
84
- forwardExhausted = false ;
85
- localOffset ++ ;
82
+ // Check if trying to fit before text beginning, and if not, check it fits
83
+ // before offset location
84
+ if ( minLine <= toPos - localOffset ) {
85
+ return - localOffset ++ ;
86
+ }
86
87
87
- // Check if trying to fit before text beginning, and if not, check it fits
88
- // before offset location
89
- toCheck = toPos - localOffset ;
90
- if ( toCheck < minLine ) {
91
88
backwardExhausted = true ;
92
- } else if ( hunkFits ( hunk , toCheck ) ) {
93
- hunk . offset = offset -= localOffset ;
89
+ return iterator ( ) ;
90
+ }
91
+
92
+ // We tried to fit hunk before text beginning and beyond text lenght, then
93
+ // hunk can't fit on the text. Return undefined
94
+ } ;
95
+ }
96
+
97
+ // Search best fit offsets for each hunk based on the previous ones
98
+ for ( let i = 0 ; i < hunks . length ; i ++ ) {
99
+ let hunk = hunks [ i ] ,
100
+ maxLine = lines . length - hunk . oldLines ,
101
+ localOffset = 0 ,
102
+ toPos = offset + hunk . oldStart - 1 ;
103
+
104
+ let iterator = distanceIterator ( toPos , minLine , maxLine ) ;
105
+
106
+ for ( ; localOffset != undefined ; localOffset = iterator ( ) ) {
107
+ if ( hunkFits ( hunk , toPos + localOffset ) ) {
108
+ hunk . offset = offset += localOffset ;
94
109
break ;
95
110
}
96
111
}
97
112
113
+ if ( localOffset == undefined ) {
114
+ return false ;
115
+ }
116
+
98
117
// Set lower text limit to end of the current hunk, so next ones don't try
99
118
// to fit over already patched text
100
119
minLine = hunk . offset + hunk . oldStart + hunk . oldLines ;
0 commit comments