Blame view

3rdparty/boost_1_81_0/tools/inspect/ascii_check.cpp 3.29 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  //  ascii_check implementation  ------------------------------------------------//
  
  //  Copyright Marshall Clow 2007.
  //  Based on the tab-check checker by Beman Dawes
  //
  //  Distributed under the Boost Software License, Version 1.0.
  //  (See accompanying file LICENSE_1_0.txt or copy at
  //  http://www.boost.org/LICENSE_1_0.txt)
  //   -- this is a test.
  
  #include "ascii_check.hpp"
  #include <algorithm>
  
  namespace boost
  {
    namespace inspect
    {
  
      static const string gPunct ( "$_{}[]#()<>%:;.?*+-/ˆ&|~!=,\\\"'@^`" );
      
     // Legal characters for a source file are defined in section 2.2 of the standard
     // I have added '@', '^', and '`' to the "legal" chars because they are commonly
     //    used in comments, and they are strictly ASCII.
     struct non_ascii {
     public:
        non_ascii () {}
        ~non_ascii () {}
        bool operator () ( char c ) const
        {
           if ( c == ' ' ) return false;
           if ( c >= 'a' && c <= 'z' ) return false;
           if ( c >= 'A' && c <= 'Z' ) return false;
           if ( c >= '0' && c <= '9' ) return false;
        // Horizontal/Vertical tab, newline, and form feed
           if ( c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f' ) return false;
           return gPunct.find ( c ) == string::npos;
        }
     };
  
     struct is_CRLF {
     public:
        is_CRLF () {}
        ~is_CRLF () {}
        bool operator () ( char c ) const
        {
           return c == '\015' || c == '\012';
        }
     };
  
     const char *kCRLF = "\012\015";
     
  // Given a position in the file, extract and return the line
     std::string find_line ( const std::string &contents, std::string::const_iterator iter_pos )
     {
        std::size_t pos = iter_pos - contents.begin ();
        
     // Search backwards for a CR or LR
        std::size_t start_pos = contents.find_last_of ( kCRLF, pos );
        std::string::const_iterator line_start = contents.begin () + ( start_pos == std::string::npos ? 0 : start_pos + 1 );
        
  
     // Search forwards for a CR or LF
        std::size_t end_pos = contents.find_first_of ( kCRLF, pos + 1 );
        std::string::const_iterator line_end;
        if ( end_pos == std::string::npos )
           line_end = contents.end ();
        else
           line_end = contents.begin () + end_pos - 1;
  
        return std::string ( line_start, line_end );
     }
  
     ascii_check::ascii_check() : m_files_with_errors(0)
     {
       register_signature( ".c" );
       register_signature( ".cpp" );
       register_signature( ".cxx" );
       register_signature( ".h" );
       register_signature( ".hpp" );
       register_signature( ".hxx" );
       register_signature( ".ipp" );
     }
  
     void ascii_check::inspect(
        const string & library_name,
        const path & full_path,   // example: c:/foo/boost/filesystem/path.hpp
        const string & contents )     // contents of file to be inspected
      {
        if (contents.find( "boostinspect:" "noascii" ) != string::npos) return;
        string::const_iterator bad_char = std::find_if ( contents.begin (), contents.end (), non_ascii ());
        if ( bad_char != contents.end ())
        {
          ++m_files_with_errors;
          int ln = std::count( contents.begin(), bad_char, '\n' ) + 1;
          string the_line = find_line ( contents, bad_char );
          error( library_name, full_path, "Non-ASCII: " + the_line, ln );
        }
      }
    } // namespace inspect
  } // namespace boost