OSDN Git Service

refactor
[winmerge-jp/winmerge-jp.git] / Src / stringdiffsi.h
1 /** 
2  * @file  stringdiffsi.h
3  *
4  * @brief Declaration file for class stringdiffs
5  *
6  */
7 #pragma once
8
9 #include <vector>
10 #include "utils/icu.hpp"
11
12 // Uncomment this to see stringdiff log messages
13 // We don't use _DEBUG since stringdiff logging is verbose and slows down WinMerge
14 //#define STRINGDIFF_LOGGING
15
16 namespace strdiff
17 {
18
19 /**
20  * @brief kind of diff blocks.
21  */
22 enum
23 {
24         dlword = 0,
25         dlspace,
26         dleol,
27         dlbreak, 
28         dlnumber,
29 };
30 /**
31  * @brief kind of synchronaction
32  */
33 enum
34 {
35         synbegin1 = 0, 
36         synbegin2,
37         synend1, 
38         synend2 
39 };
40
41 struct wdiff;
42
43 /**
44  * @brief Class to hold together data needed to implement strdiff::ComputeWordDiffs
45  */
46 class stringdiffs
47 {
48 public:
49         stringdiffs(const String & str1, const String & str2,
50                 bool case_sensitive, bool eol_sensitive, int whitespace, bool ignore_numbers, int breakType,
51                 std::vector<wdiff> * pDiffs);
52
53         ~stringdiffs();
54
55         void BuildWordDiffList();
56         void wordLevelToByteLevel();
57         void PopulateDiffs();
58
59 // Implementation types
60 private:
61         struct word {
62                 int start; // index of first character of word in original string
63                 int end;   // index of last character of word in original string
64                 int hash;
65                 int bBreak; // Is it a isWordBreak 0 = word -1= whitespace -2 = empty 1 = breakWord
66                 word(int s = 0, int e = 0, int b = 0, int h = 0) : start(s), end(e), bBreak(b),hash(h) { }
67                 inline int length() const { return end+1-start; }
68         };
69
70 // Implementation methods
71 private:
72
73         void ComputeByteDiff(const String& str1, const String& str2,
74                         bool casitive, int xwhite, 
75                         int begin[2], int end[2], bool equal);
76         std::vector<word> BuildWordsArray(const String & str) const;
77         unsigned Hash(const String & str, int begin, int end, unsigned h ) const;
78         bool AreWordsSame(const word & word1, const word & word2) const;
79         bool IsWord(const word & word1) const;
80         /**
81          * @brief Is this block an space or whitespace one?
82          */
83         inline bool IsSpace(const word & word1) const
84         {
85                 return (word1.bBreak == dlspace);
86         }
87         /**
88          * @brief Is this block a number one?
89          */
90         inline bool IsNumber(const word& word1) const
91         {
92                 return (word1.bBreak == dlnumber);
93         }
94         /**
95          * @brief Is this block an EOL?
96          */
97         inline bool IsEOL(const word & word1) const
98         {
99                 return (word1.bBreak == dleol);
100         }
101         bool BuildWordDiffList_DP();
102         int dp(std::vector<char> & edscript);
103         int onp(std::vector<char> & edscript);
104         int snake(int k, int y, int M, int N, bool exchanged) const;
105 #ifdef STRINGDIFF_LOGGING
106         void debugoutput();
107 #endif
108
109 // Implementation data
110 private:
111         const String & m_str1;
112         const String & m_str2;
113         int m_whitespace;
114         int m_breakType;
115         bool m_case_sensitive;
116         bool m_eol_sensitive;
117         bool m_ignore_numbers = false;
118         bool m_matchblock;
119         std::vector<wdiff> * m_pDiffs;
120         std::vector<word> m_words1;
121         std::vector<word> m_words2;
122         std::vector<wdiff> m_wdiffs;
123 };
124
125 }