OSDN Git Service

Cleanup: Fixup 1: some size_t should be ptrdiff_t
authorGreyMerlin <GreyMerlin7@gmail.com>
Sun, 8 Oct 2017 22:28:58 +0000 (15:28 -0700)
committerGreyMerlin <GreyMerlin7@gmail.com>
Mon, 9 Oct 2017 18:43:17 +0000 (11:43 -0700)
 * Some recent type changes to size_t should have been to ptrdiff_t
(which is signed).
 * In particular, str_pos(), get_coding() and fget_coding() in
  cs2cs.cpp use the value -2 as a return flag.  This was being
improperly handled via size_t.
 * Finding this change led to areas with similar problems, in particular
* next and prev in DirrRangeInfo{} struct
* Open and Close in RxMatchRes{} struct.

Externals/crystaledit/editlib/ccrystaleditview.cpp
Externals/crystaledit/editlib/ccrystaltextview.cpp
Externals/crystaledit/editlib/cregexp.h
Externals/crystaledit/editlib/cs2cs.cpp
Src/DiffList.cpp
Src/DiffList.h
Src/codepage_detect.cpp
Src/codepage_detect.h
Src/stringdiffs.cpp

index 9b490ea..b134337 100644 (file)
@@ -2798,8 +2798,6 @@ OnUpdateToolsCharCoding (CCmdUI * pCmdUI)
   pCmdUI->Enable (IsSelection ());
 }
 
-size_t str_pos (LPCTSTR whole, LPCTSTR piece);
-
 void CCrystalEditView::
 OnToolsCharCoding ()
 {
index 3c2b4ec..9b64908 100644 (file)
@@ -156,7 +156,7 @@ IMPLEMENT_DYNCREATE (CCrystalTextView, CView)
 
 HINSTANCE CCrystalTextView::s_hResourceInst = NULL;
 
-static size_t FindStringHelper(LPCTSTR pszFindWhere, LPCTSTR pszFindWhat, DWORD dwFlags, int &nLen, RxNode *&rxnode, RxMatchRes *rxmatch);
+static ptrdiff_t FindStringHelper(LPCTSTR pszFindWhere, LPCTSTR pszFindWhat, DWORD dwFlags, int &nLen, RxNode *&rxnode, RxMatchRes *rxmatch);
 
 BEGIN_MESSAGE_MAP (CCrystalTextView, CView)
 //{{AFX_MSG_MAP(CCrystalTextView)
@@ -4805,12 +4805,12 @@ PrepareDragData ()
   return hData;
 }
 
-static size_t
+static ptrdiff_t
 FindStringHelper (LPCTSTR pszFindWhere, LPCTSTR pszFindWhat, DWORD dwFlags, int &nLen, RxNode *&rxnode, RxMatchRes *rxmatch)
 {
   if (dwFlags & FIND_REGEXP)
     {
-      size_t pos;
+      ptrdiff_t pos;
 
       if (rxnode)
         RxFree (rxnode);
@@ -5044,7 +5044,7 @@ FindTextInBlock (LPCTSTR pszText, const CPoint & ptStartPosition,
                   line.ReleaseBuffer (ptCurrentPos.x + 1);
                 }
 
-              size_t nFoundPos = -1;
+              ptrdiff_t nFoundPos = -1;
               int nMatchLen = what.GetLength();
               int nLineLen = line.GetLength();
               size_t nPos;
index 7d99565..d411e24 100644 (file)
@@ -95,8 +95,8 @@ typedef struct _RxNode RxNode;
 struct _RxNode;
 
 typedef struct {
-    size_t Open[NSEXPS];    // -1 = not matched
-    size_t Close[NSEXPS];
+    ptrdiff_t Open[NSEXPS];    // -1 = not matched
+    ptrdiff_t Close[NSEXPS];
 } RxMatchRes;
 
 RxNode EDITPADC_CLASS *RxCompile(LPCTSTR Regexp, unsigned int RxOpt = RX_CASE);
index ff41095..05be626 100644 (file)
@@ -61,7 +61,7 @@ str_fill (LPTSTR s, TCHAR ch, long count)
   *s = _T ('\0');
 }
 
-size_t
+ptrdiff_t
 str_pos (LPCTSTR whole, LPCTSTR piece)
 {
   LPCTSTR s = whole;
@@ -107,7 +107,7 @@ skip_word (LPCTSTR s)
   return skip_spaces (s);
 }
 
-size_t
+ptrdiff_t
 get_coding (LPCTSTR name, type_codes *codes, int *coding)
 {
   size_t pos;
@@ -122,11 +122,12 @@ get_coding (LPCTSTR name, type_codes *codes, int *coding)
   return -2;
 }
 
-size_t
+
+ptrdiff_t
 fget_coding (LPCTSTR text, int *coding)
 {
-  size_t posit = 0;
-  size_t i = 0;
+  ptrdiff_t posit = 0;
+  ptrdiff_t i = 0;
   LPCTSTR s, s1;
 
   while ((i = str_pos (text, FD_ENCODING_LBRACKET)) >= 0)
@@ -179,7 +180,7 @@ TCHAR iconvert_char (TCHAR ch, int source_coding, int destination_coding, bool a
 int
 iconvert (LPTSTR string, int source_coding, int destination_coding, bool alphabet_only)
   {
-    size_t posit = -2;
+    ptrdiff_t posit = -2;
     LPCTSTR source_chars, destination_chars, cod_pos = NULL;
     TCHAR ch;
     LPTSTR s = string;
index 42b695b..1935540 100644 (file)
@@ -448,8 +448,8 @@ void DiffList::ConstructSignificantChain()
        m_lastSignificantLeftRight = -1;
        m_lastSignificantMiddleRight = -1;
        m_lastSignificantConflict = -1;
-       int prev = -1;
-       const int size = (int) m_diffs.size();
+       ptrdiff_t prev = -1;
+       const ptrdiff_t size = (int) m_diffs.size();
 
        // must be called after diff list is entirely populated
     for (int i = 0; i < size; ++i)
index 85c7d44..8d529e1 100644 (file)
@@ -105,8 +105,8 @@ public:
  */
 struct DiffRangeInfo: public DIFFRANGE
 {
-       size_t next; /**< link (array index) for doubly-linked chain of non-trivial DIFFRANGEs */
-       size_t prev; /**< link (array index) for doubly-linked chain of non-trivial DIFFRANGEs */
+       ptrdiff_t next; /**< link (array index) for doubly-linked chain of non-trivial DIFFRANGEs */
+       ptrdiff_t prev; /**< link (array index) for doubly-linked chain of non-trivial DIFFRANGEs */
 
        DiffRangeInfo() { InitLinks(); }
        explicit DiffRangeInfo(const DIFFRANGE & di) : DIFFRANGE(di) { InitLinks(); }
index 58491e4..c1fa967 100644 (file)
@@ -230,7 +230,7 @@ static unsigned GuessEncoding_from_bytes(const String& ext, const char *src, siz
  * @param [in] bGuessEncoding Try to guess codepage (not just unicode encoding).
  * @return Structure getting the encoding info.
  */
-FileTextEncoding GuessCodepageEncoding(const String& filepath, int guessEncodingType, int mapmaxlen)
+FileTextEncoding GuessCodepageEncoding(const String& filepath, int guessEncodingType, ptrdiff_t mapmaxlen)
 {
        FileTextEncoding encoding;
        CMarkdown::FileImage fi(filepath.c_str(), mapmaxlen);
index 19dbda6..44c1fa6 100644 (file)
@@ -11,4 +11,4 @@
 /** @brief Buffer size used in this file. */
 static const int BufSize = 65536;
 
-FileTextEncoding GuessCodepageEncoding(const String& filepath, int guessEncodingType, int mapmaxlen = BufSize);
+FileTextEncoding GuessCodepageEncoding(const String& filepath, int guessEncodingType, ptrdiff_t mapmaxlen = BufSize);
index aca475c..a98f0f2 100644 (file)
@@ -365,6 +365,10 @@ stringdiffs::BuildWordsArray(const String & str, std::vector<word>& words)
 {
        int i=0, begin=0;
 
+       size_t sLen = str.length();
+       assert(sLen < INT_MAX);
+       int iLen = static_cast<int>(sLen);
+
        // dummy;
        words.push_back(word(0, -1, 0, 0));
 
@@ -383,7 +387,7 @@ inspace:
 
                words.push_back(word(begin, e, dlspace, Hash(str, begin, e, 0)));
        }
-       if (i == str.length())
+       if (i == iLen)
                return;
        begin = i;
        goto inword;
@@ -391,7 +395,7 @@ inspace:
        // state when we are inside a word
 inword:
        bool atspace=false;
-       if (i == str.length() || ((atspace = isSafeWhitespace(str[i])) != 0) || isWordBreak(m_breakType, str.c_str(), i))
+       if (i == iLen || ((atspace = isSafeWhitespace(str[i])) != 0) || isWordBreak(m_breakType, str.c_str(), i))
        {
                if (begin<i)
                {
@@ -401,7 +405,7 @@ inword:
                        
                        words.push_back(word(begin, e, dlword, Hash(str, begin, e, 0)));
                }
-               if (i == str.length())
+               if (i == iLen)
                {
                        return;
                }