OSDN Git Service

Improve plugin system (#797) (6)
[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         dlbreak, 
27         dlinsert,
28 };
29 /**
30  * @brief kind of synchronaction
31  */
32 enum
33 {
34         synbegin1 = 0, 
35         synbegin2,
36         synend1, 
37         synend2 
38 };
39
40 struct wdiff;
41
42 /**
43  * @brief Class to hold together data needed to implement strdiff::ComputeWordDiffs
44  */
45 class stringdiffs
46 {
47 public:
48         stringdiffs(const String & str1, const String & str2,
49                 bool case_sensitive, bool eol_sensitive, int whitespace, int breakType,
50                 std::vector<wdiff> * pDiffs);
51
52         ~stringdiffs();
53
54         void BuildWordDiffList();
55         void wordLevelToByteLevel();
56         void PopulateDiffs();
57
58 // Implementation types
59 private:
60         struct word {
61                 int start; // index of first character of word in original string
62                 int end;   // index of last character of word in original string
63                 int hash;
64                 int bBreak; // Is it a isWordBreak 0 = word -1= whitespace -2 = empty 1 = breakWord
65                 word(int s = 0, int e = 0, int b = 0, int h = 0) : start(s), end(e), bBreak(b),hash(h) { }
66                 int length() const { return end+1-start; }
67         };
68
69 // Implementation methods
70 private:
71
72         void ComputeByteDiff(const String& str1, const String& str2,
73                         bool casitive, int xwhite, 
74                         int begin[2], int end[2], bool equal);
75         std::vector<word> BuildWordsArray(const String & str);
76         unsigned Hash(const String & str, int begin, int end, unsigned h ) const;
77         bool AreWordsSame(const word & word1, const word & word2) const;
78         bool IsWord(const word & word1) const;
79         /**
80          * @brief Is this block an space or whitespace one?
81          */
82         inline bool IsSpace(const word & word1) const
83         {
84                 return (word1.bBreak == dlspace);
85         }
86         /**
87          * @brief Is this block a break?
88          */
89         inline bool IsBreak(const word & word1) const
90         {
91                 return (word1.bBreak == dlbreak || word1.bBreak == dlspace);
92         }
93         /**
94          * @brief Is this block an empty (insert) one?
95          */
96         inline bool IsInsert(const word & word1) const
97         {
98                 return (word1.bBreak == dlinsert);
99         }
100         bool caseMatch(TCHAR ch1, TCHAR ch2) const;
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, bool exchanged);
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         bool m_case_sensitive;
114         bool m_eol_sensitive;
115         int m_whitespace;
116         int m_breakType;
117         bool m_matchblock;
118         std::vector<wdiff> * m_pDiffs;
119         std::vector<word> m_words1;
120         std::vector<word> m_words2;
121         std::vector<wdiff> m_wdiffs;
122 };
123
124 }