@@ -1854,6 +1854,8 @@ static int match_fragment(struct image *img,
1854
1854
{
1855
1855
int i ;
1856
1856
char * fixed_buf , * buf , * orig , * target ;
1857
+ struct strbuf fixed ;
1858
+ size_t fixed_len ;
1857
1859
int preimage_limit ;
1858
1860
1859
1861
if (preimage -> nr + try_lno <= img -> nr ) {
@@ -1977,12 +1979,12 @@ static int match_fragment(struct image *img,
1977
1979
* use the whitespace from the preimage.
1978
1980
*/
1979
1981
extra_chars = preimage_end - preimage_eof ;
1980
- fixed_buf = xmalloc ( imgoff + extra_chars );
1981
- memcpy ( fixed_buf , img -> buf + try , imgoff );
1982
- memcpy ( fixed_buf + imgoff , preimage_eof , extra_chars );
1983
- imgoff += extra_chars ;
1982
+ strbuf_init ( & fixed , imgoff + extra_chars );
1983
+ strbuf_add ( & fixed , img -> buf + try , imgoff );
1984
+ strbuf_add ( & fixed , preimage_eof , extra_chars );
1985
+ fixed_buf = strbuf_detach ( & fixed , & fixed_len ) ;
1984
1986
update_pre_post_images (preimage , postimage ,
1985
- fixed_buf , imgoff , postlen );
1987
+ fixed_buf , fixed_len , postlen );
1986
1988
return 1 ;
1987
1989
}
1988
1990
@@ -1999,27 +2001,22 @@ static int match_fragment(struct image *img,
1999
2001
* but in this loop we will only handle the part of the
2000
2002
* preimage that falls within the file.
2001
2003
*/
2002
- fixed_buf = xmalloc (preimage -> len + 1 );
2003
- buf = fixed_buf ;
2004
+ strbuf_init (& fixed , preimage -> len + 1 );
2004
2005
orig = preimage -> buf ;
2005
2006
target = img -> buf + try ;
2006
2007
for (i = 0 ; i < preimage_limit ; i ++ ) {
2007
- size_t fixlen ; /* length after fixing the preimage */
2008
2008
size_t oldlen = preimage -> line [i ].len ;
2009
2009
size_t tgtlen = img -> line [try_lno + i ].len ;
2010
- size_t tgtfixlen ; /* length after fixing the target line */
2011
- char tgtfixbuf [ 1024 ], * tgtfix ;
2010
+ size_t fixstart = fixed . len ;
2011
+ struct strbuf tgtfix ;
2012
2012
int match ;
2013
2013
2014
2014
/* Try fixing the line in the preimage */
2015
- fixlen = ws_fix_copy (buf , orig , oldlen , ws_rule , NULL );
2015
+ ws_fix_copy (& fixed , orig , oldlen , ws_rule , NULL );
2016
2016
2017
2017
/* Try fixing the line in the target */
2018
- if (sizeof (tgtfixbuf ) > tgtlen )
2019
- tgtfix = tgtfixbuf ;
2020
- else
2021
- tgtfix = xmalloc (tgtlen );
2022
- tgtfixlen = ws_fix_copy (tgtfix , target , tgtlen , ws_rule , NULL );
2018
+ strbuf_init (& tgtfix , tgtlen );
2019
+ ws_fix_copy (& tgtfix , target , tgtlen , ws_rule , NULL );
2023
2020
2024
2021
/*
2025
2022
* If they match, either the preimage was based on
@@ -2031,15 +2028,15 @@ static int match_fragment(struct image *img,
2031
2028
* so we might as well take the fix together with their
2032
2029
* real change.
2033
2030
*/
2034
- match = (tgtfixlen == fixlen && !memcmp (tgtfix , buf , fixlen ));
2031
+ match = (tgtfix .len == fixed .len - fixstart &&
2032
+ !memcmp (tgtfix .buf , fixed .buf + fixstart ,
2033
+ fixed .len - fixstart ));
2035
2034
2036
- if (tgtfix != tgtfixbuf )
2037
- free (tgtfix );
2035
+ strbuf_release (& tgtfix );
2038
2036
if (!match )
2039
2037
goto unmatch_exit ;
2040
2038
2041
2039
orig += oldlen ;
2042
- buf += fixlen ;
2043
2040
target += tgtlen ;
2044
2041
}
2045
2042
@@ -2051,32 +2048,32 @@ static int match_fragment(struct image *img,
2051
2048
* false).
2052
2049
*/
2053
2050
for ( ; i < preimage -> nr ; i ++ ) {
2054
- size_t fixlen ; /* length after fixing the preimage */
2051
+ size_t fixstart = fixed . len ; /* start of the fixed preimage */
2055
2052
size_t oldlen = preimage -> line [i ].len ;
2056
2053
int j ;
2057
2054
2058
2055
/* Try fixing the line in the preimage */
2059
- fixlen = ws_fix_copy (buf , orig , oldlen , ws_rule , NULL );
2056
+ ws_fix_copy (& fixed , orig , oldlen , ws_rule , NULL );
2060
2057
2061
- for (j = 0 ; j < fixlen ; j ++ )
2062
- if (!isspace (buf [j ]))
2058
+ for (j = fixstart ; j < fixed . len ; j ++ )
2059
+ if (!isspace (fixed . buf [j ]))
2063
2060
goto unmatch_exit ;
2064
2061
2065
2062
orig += oldlen ;
2066
- buf += fixlen ;
2067
2063
}
2068
2064
2069
2065
/*
2070
2066
* Yes, the preimage is based on an older version that still
2071
2067
* has whitespace breakages unfixed, and fixing them makes the
2072
2068
* hunk match. Update the context lines in the postimage.
2073
2069
*/
2070
+ fixed_buf = strbuf_detach (& fixed , & fixed_len );
2074
2071
update_pre_post_images (preimage , postimage ,
2075
- fixed_buf , buf - fixed_buf , 0 );
2072
+ fixed_buf , fixed_len , 0 );
2076
2073
return 1 ;
2077
2074
2078
2075
unmatch_exit :
2079
- free ( fixed_buf );
2076
+ strbuf_release ( & fixed );
2080
2077
return 0 ;
2081
2078
}
2082
2079
@@ -2244,7 +2241,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2244
2241
int match_beginning , match_end ;
2245
2242
const char * patch = frag -> patch ;
2246
2243
int size = frag -> size ;
2247
- char * old , * new , * oldlines , * newlines ;
2244
+ char * old , * oldlines ;
2245
+ struct strbuf newlines ;
2248
2246
int new_blank_lines_at_end = 0 ;
2249
2247
unsigned long leading , trailing ;
2250
2248
int pos , applied_pos ;
@@ -2254,16 +2252,16 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2254
2252
memset (& preimage , 0 , sizeof (preimage ));
2255
2253
memset (& postimage , 0 , sizeof (postimage ));
2256
2254
oldlines = xmalloc (size );
2257
- newlines = xmalloc ( size );
2255
+ strbuf_init ( & newlines , size );
2258
2256
2259
2257
old = oldlines ;
2260
- new = newlines ;
2261
2258
while (size > 0 ) {
2262
2259
char first ;
2263
2260
int len = linelen (patch , size );
2264
- int plen , added ;
2261
+ int plen ;
2265
2262
int added_blank_line = 0 ;
2266
2263
int is_blank_context = 0 ;
2264
+ size_t start ;
2267
2265
2268
2266
if (!len )
2269
2267
break ;
@@ -2293,7 +2291,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2293
2291
/* ... followed by '\No newline'; nothing */
2294
2292
break ;
2295
2293
* old ++ = '\n' ;
2296
- * new ++ = '\n' ;
2294
+ strbuf_addch ( & newlines , '\n' ) ;
2297
2295
add_line_info (& preimage , "\n" , 1 , LINE_COMMON );
2298
2296
add_line_info (& postimage , "\n" , 1 , LINE_COMMON );
2299
2297
is_blank_context = 1 ;
@@ -2315,18 +2313,17 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2315
2313
if (first == '+' && no_add )
2316
2314
break ;
2317
2315
2316
+ start = newlines .len ;
2318
2317
if (first != '+' ||
2319
2318
!whitespace_error ||
2320
2319
ws_error_action != correct_ws_error ) {
2321
- memcpy (new , patch + 1 , plen );
2322
- added = plen ;
2320
+ strbuf_add (& newlines , patch + 1 , plen );
2323
2321
}
2324
2322
else {
2325
- added = ws_fix_copy (new , patch + 1 , plen , ws_rule , & applied_after_fixing_ws );
2323
+ ws_fix_copy (& newlines , patch + 1 , plen , ws_rule , & applied_after_fixing_ws );
2326
2324
}
2327
- add_line_info (& postimage , new , added ,
2325
+ add_line_info (& postimage , newlines . buf + start , newlines . len - start ,
2328
2326
(first == '+' ? 0 : LINE_COMMON ));
2329
- new += added ;
2330
2327
if (first == '+' &&
2331
2328
(ws_rule & WS_BLANK_AT_EOF ) &&
2332
2329
ws_blank_line (patch + 1 , plen , ws_rule ))
@@ -2351,9 +2348,9 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2351
2348
}
2352
2349
if (inaccurate_eof &&
2353
2350
old > oldlines && old [-1 ] == '\n' &&
2354
- new > newlines && new [ - 1 ] == '\n' ) {
2351
+ newlines . len > 0 && newlines . buf [ newlines . len - 1 ] == '\n' ) {
2355
2352
old -- ;
2356
- new -- ;
2353
+ strbuf_setlen ( & newlines , newlines . len - 1 ) ;
2357
2354
}
2358
2355
2359
2356
leading = frag -> leading ;
@@ -2385,8 +2382,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2385
2382
pos = frag -> newpos ? (frag -> newpos - 1 ) : 0 ;
2386
2383
preimage .buf = oldlines ;
2387
2384
preimage .len = old - oldlines ;
2388
- postimage .buf = newlines ;
2389
- postimage .len = new - newlines ;
2385
+ postimage .buf = newlines . buf ;
2386
+ postimage .len = newlines . len ;
2390
2387
preimage .line = preimage .line_allocated ;
2391
2388
postimage .line = postimage .line_allocated ;
2392
2389
@@ -2462,7 +2459,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2462
2459
}
2463
2460
2464
2461
free (oldlines );
2465
- free ( newlines );
2462
+ strbuf_release ( & newlines );
2466
2463
free (preimage .line_allocated );
2467
2464
free (postimage .line_allocated );
2468
2465
0 commit comments