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