修订版 | 74e12673ed60642aaf23cd47a38bfc0d0c9de5a8 (tree) |
---|---|
时间 | 2021-11-26 23:47:42 |
作者 | Takashi Sawanaka <sdottaka@user...> |
Commiter | Takashi Sawanaka |
Fix issue #1057: Source Files Comparison doesn't seem to Work properly
@@ -56,6 +56,8 @@ using Poco::Exception; | ||
56 | 56 | |
57 | 57 | extern int recursive; |
58 | 58 | |
59 | +extern "C" int is_blank_line(char const* pch, char const* limit); | |
60 | + | |
59 | 61 | static void CopyTextStats(const file_data * inf, FileTextStats * myTextStats); |
60 | 62 | static void CopyDiffutilTextStats(file_data *inf, DiffFileData * diffData); |
61 | 63 |
@@ -291,37 +293,39 @@ static unsigned GetCommentsFilteredText(unsigned dwCookie, int startLine, int en | ||
291 | 293 | } |
292 | 294 | |
293 | 295 | /** |
294 | - * @brief Replace spaces in a string | |
295 | - * @param [in] str - String to search | |
296 | - * @param [in] rep - String to replace | |
296 | + * @brief Replace a string inside a string with another string. | |
297 | + * This function searches for a string inside another string an if found, | |
298 | + * replaces it with another string. Function can replace several instances | |
299 | + * of the string inside one string. | |
300 | + * @param [in,out] target A string containing another string to replace. | |
301 | + * @param [in] find A string to search and replace with another (@p replace). | |
302 | + * @param [in] replace A string used to replace original (@p find). | |
297 | 303 | */ |
298 | -static void ReplaceSpaces(std::string & str, const char *rep) | |
304 | +void Replace(std::string &target, const std::string &find, const std::string &replace) | |
299 | 305 | { |
306 | + const std::string::size_type find_len = find.length(); | |
307 | + const std::string::size_type replace_len = replace.length(); | |
300 | 308 | std::string::size_type pos = 0; |
301 | - size_t replen = strlen(rep); | |
302 | - while ((pos = str.find_first_of(" \t", pos)) != std::string::npos) | |
309 | + while ((pos = target.find(find, pos)) != std::string::npos) | |
303 | 310 | { |
304 | - std::string::size_type posend = str.find_first_not_of(" \t", pos); | |
305 | - if (posend != String::npos) | |
306 | - str.replace(pos, posend - pos, rep); | |
307 | - else | |
308 | - str.replace(pos, str.length() - pos, rep); | |
309 | - pos += replen; | |
311 | + target.replace(pos, find_len, replace); | |
312 | + pos += replace_len; | |
310 | 313 | } |
311 | 314 | } |
312 | 315 | |
313 | 316 | /** |
314 | - * @brief Replace spaces in a string | |
315 | - * @param [in] str - String to search | |
317 | + * @brief Replace the characters that matche characters specified in its arguments | |
318 | + * @param [in,out] str - A string containing another string to replace. | |
319 | + * @param [in] chars - characters to search for | |
316 | 320 | * @param [in] rep - String to replace |
317 | 321 | */ |
318 | -static void ReplaceNumbers(std::string& str, const char* rep) | |
322 | +static void ReplaceChars(std::string & str, const char* chars, const char *rep) | |
319 | 323 | { |
320 | 324 | std::string::size_type pos = 0; |
321 | 325 | size_t replen = strlen(rep); |
322 | - while ((pos = str.find_first_of("0123456789", pos)) != std::string::npos) | |
326 | + while ((pos = str.find_first_of(chars, pos)) != std::string::npos) | |
323 | 327 | { |
324 | - std::string::size_type posend = str.find_first_not_of("0123456789", pos); | |
328 | + std::string::size_type posend = str.find_first_not_of(chars, pos); | |
325 | 329 | if (posend != String::npos) |
326 | 330 | str.replace(pos, posend - pos, rep); |
327 | 331 | else |
@@ -331,6 +335,26 @@ static void ReplaceNumbers(std::string& str, const char* rep) | ||
331 | 335 | } |
332 | 336 | |
333 | 337 | /** |
338 | + * @brief Remove blank lines | |
339 | + */ | |
340 | +void RemoveBlankLines(std::string &str) | |
341 | +{ | |
342 | + size_t pos = 0; | |
343 | + while (pos < str.length()) | |
344 | + { | |
345 | + size_t posend = str.find_first_of("\r\n", pos); | |
346 | + if (posend != std::string::npos) | |
347 | + posend = str.find_first_not_of("\r\n", posend); | |
348 | + if (posend == std::string::npos) | |
349 | + posend = str.length(); | |
350 | + if (is_blank_line(str.data() + pos, str.data() + posend)) | |
351 | + str.erase(pos, posend - pos); | |
352 | + else | |
353 | + pos = posend; | |
354 | + } | |
355 | +} | |
356 | + | |
357 | +/** | |
334 | 358 | @brief The main entry for post filtering. Performs post-filtering, by setting comment blocks to trivial |
335 | 359 | @param [in] LineNumberLeft - First line number to read from left file |
336 | 360 | @param [in] QtyLinesLeft - Number of lines in the block for left file |
@@ -380,23 +404,22 @@ void CDiffWrapper::PostFilter(PostFilterContext& ctxt, int LineNumberLeft, int Q | ||
380 | 404 | if (m_options.m_ignoreWhitespace == WHITESPACE_IGNORE_ALL) |
381 | 405 | { |
382 | 406 | //Ignore character case |
383 | - ReplaceSpaces(LineDataLeft, ""); | |
384 | - ReplaceSpaces(LineDataRight, ""); | |
407 | + ReplaceChars(LineDataLeft, " \t", ""); | |
408 | + ReplaceChars(LineDataRight, " \t", ""); | |
385 | 409 | } |
386 | 410 | else if (m_options.m_ignoreWhitespace == WHITESPACE_IGNORE_CHANGE) |
387 | 411 | { |
388 | 412 | //Ignore change in whitespace char count |
389 | - ReplaceSpaces(LineDataLeft, " "); | |
390 | - ReplaceSpaces(LineDataRight, " "); | |
413 | + ReplaceChars(LineDataLeft, " \t", " "); | |
414 | + ReplaceChars(LineDataRight, " \t", " "); | |
391 | 415 | } |
392 | 416 | |
393 | 417 | if (m_options.m_bIgnoreNumbers ) |
394 | 418 | { |
395 | 419 | //Ignore number character case |
396 | - ReplaceNumbers(LineDataLeft, ""); | |
397 | - ReplaceNumbers(LineDataRight, ""); | |
420 | + ReplaceChars(LineDataLeft, "0123456789", ""); | |
421 | + ReplaceChars(LineDataRight, "0123456789", ""); | |
398 | 422 | } |
399 | - | |
400 | 423 | if (m_options.m_bIgnoreCase) |
401 | 424 | { |
402 | 425 | //ignore case |
@@ -407,6 +430,18 @@ void CDiffWrapper::PostFilter(PostFilterContext& ctxt, int LineNumberLeft, int Q | ||
407 | 430 | for (std::string::iterator pb = LineDataRight.begin(), pe = LineDataRight.end(); pb != pe; ++pb) |
408 | 431 | *pb = static_cast<char>(::toupper(*pb)); |
409 | 432 | } |
433 | + if (m_options.m_bIgnoreEOLDifference) | |
434 | + { | |
435 | + Replace(LineDataLeft, "\r\n", "\n"); | |
436 | + Replace(LineDataLeft, "\r", "\n"); | |
437 | + Replace(LineDataRight, "\r\n", "\n"); | |
438 | + Replace(LineDataRight, "\r", "\n"); | |
439 | + } | |
440 | + if (m_options.m_bIgnoreBlankLines) | |
441 | + { | |
442 | + RemoveBlankLines(LineDataLeft); | |
443 | + RemoveBlankLines(LineDataRight); | |
444 | + } | |
410 | 445 | if (LineDataLeft != LineDataRight) |
411 | 446 | return; |
412 | 447 | //only difference is trival |