]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v3/cregex.hpp
0af1ef6c46a2efd3e00a9147b2c66ad2f539775a
[lyx.git] / boost / boost / regex / v3 / cregex.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr John Maddock
5  *
6  * Use, modification and distribution are subject to the 
7  * Boost Software License, Version 1.0. (See accompanying file 
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11  
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         cregex.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Declares POSIX API functions
17   *                + boost::RegEx high level wrapper.
18   */
19
20 #ifndef BOOST_RE_CREGEX_HPP_INCLUDED
21 #define BOOST_RE_CREGEX_HPP_INCLUDED
22
23 #ifndef BOOST_REGEX_CONFIG_HPP
24 #include <boost/regex/config.hpp>
25 #endif
26
27 #ifdef __BORLANDC__
28    #pragma option push -a8 -b -Vx -Ve -pc
29 #endif
30
31 /* include these defs only for POSIX compatablity */
32 #ifdef __cplusplus
33 namespace boost{
34 extern "C" {
35 #endif
36
37 #if defined(__cplusplus) && !defined(BOOST_NO_STDC_NAMESPACE)
38 typedef std::ptrdiff_t regoff_t;
39 typedef std::size_t regsize_t;
40 #else
41 typedef ptrdiff_t regoff_t;
42 typedef size_t regsize_t;
43 #endif
44
45 typedef struct
46 {
47    unsigned int re_magic;
48    unsigned int re_nsub;      /* number of parenthesized subexpressions */
49    const char* re_endp;       /* end pointer for REG_PEND */
50    void* guts;             /* none of your business :-) */
51    unsigned int eflags;       /* none of your business :-) */
52 } regex_tA;
53
54 #ifndef BOOST_NO_WREGEX
55 typedef struct
56 {
57    unsigned int re_magic;
58    unsigned int re_nsub;      /* number of parenthesized subexpressions */
59    const wchar_t* re_endp;       /* end pointer for REG_PEND */
60    void* guts;             /* none of your business :-) */
61    unsigned int eflags;       /* none of your business :-) */
62 } regex_tW;
63 #endif
64
65 typedef struct
66 {
67    regoff_t rm_so;      /* start of match */
68    regoff_t rm_eo;      /* end of match */
69 } regmatch_t;
70
71 /* regcomp() flags */
72 typedef enum{
73    REG_BASIC = 0000,
74    REG_EXTENDED = 0001,
75    REG_ICASE = 0002,
76    REG_NOSUB = 0004,
77    REG_NEWLINE = 0010,
78    REG_NOSPEC = 0020,
79    REG_PEND = 0040,
80    REG_DUMP = 0200,
81    REG_NOCOLLATE = 0400,
82    REG_ESCAPE_IN_LISTS = 01000,
83    REG_NEWLINE_ALT = 02000,
84
85    REG_PERL = REG_EXTENDED | REG_NOCOLLATE | REG_ESCAPE_IN_LISTS,
86    REG_AWK = REG_EXTENDED | REG_ESCAPE_IN_LISTS,
87    REG_GREP = REG_BASIC | REG_NEWLINE_ALT,
88    REG_EGREP = REG_EXTENDED | REG_NEWLINE_ALT,
89
90    REG_ASSERT = 15,
91    REG_INVARG = 16,
92    REG_ATOI = 255,   /* convert name to number (!) */
93    REG_ITOA = 0400   /* convert number to name (!) */
94 } reg_comp_flags;
95
96 /* regexec() flags */
97 typedef enum{
98    REG_NOTBOL =    00001,
99    REG_NOTEOL =    00002,
100    REG_STARTEND =  00004
101 } reg_exec_flags;
102
103 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompA(regex_tA*, const char*, int);
104 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int, const regex_tA*, char*, regsize_t);
105 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecA(const regex_tA*, const char*, regsize_t, regmatch_t*, int);
106 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeA(regex_tA*);
107
108 #ifndef BOOST_NO_WREGEX
109 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regcompW(regex_tW*, const wchar_t*, int);
110 BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int, const regex_tW*, wchar_t*, regsize_t);
111 BOOST_REGEX_DECL int BOOST_REGEX_CCALL regexecW(const regex_tW*, const wchar_t*, regsize_t, regmatch_t*, int);
112 BOOST_REGEX_DECL void BOOST_REGEX_CCALL regfreeW(regex_tW*);
113 #endif
114
115 #ifdef UNICODE
116 #define regcomp regcompW
117 #define regerror regerrorW
118 #define regexec regexecW
119 #define regfree regfreeW
120 #define regex_t regex_tW
121 #else
122 #define regcomp regcompA
123 #define regerror regerrorA
124 #define regexec regexecA
125 #define regfree regfreeA
126 #define regex_t regex_tA
127 #endif
128
129 /* regerror() flags */
130 typedef enum
131 {
132   REG_NOERROR = 0,   /* Success.  */
133   REG_NOMATCH = 1,      /* Didn't find a match (for regexec).  */
134
135   /* POSIX regcomp return error codes.  (In the order listed in the
136      standard.)  */
137   REG_BADPAT = 2,    /* Invalid pattern.  */
138   REG_ECOLLATE = 3,  /* Undefined collating element.  */
139   REG_ECTYPE = 4,    /* Invalid character class name.  */
140   REG_EESCAPE = 5,   /* Trailing backslash.  */
141   REG_ESUBREG = 6,   /* Invalid back reference.  */
142   REG_EBRACK = 7,    /* Unmatched left bracket.  */
143   REG_EPAREN = 8,    /* Parenthesis imbalance.  */
144   REG_EBRACE = 9,    /* Unmatched \{.  */
145   REG_BADBR = 10,    /* Invalid contents of \{\}.  */
146   REG_ERANGE = 11,   /* Invalid range end.  */
147   REG_ESPACE = 12,   /* Ran out of memory.  */
148   REG_BADRPT = 13,   /* No preceding re for repetition op.  */
149   REG_EEND = 14,     /* unexpected end of expression */
150   REG_ESIZE = 15,    /* expression too big */
151   REG_ERPAREN = 16,   /* unmatched right parenthesis */
152   REG_EMPTY = 17,    /* empty expression */
153   REG_E_MEMORY = REG_ESIZE, /* out of memory */
154   REG_E_UNKNOWN = 18 /* unknown error */
155 } reg_errcode_t;
156
157 enum match_flags
158 {
159    match_default = 0,
160    match_not_bol = 1,                                // first is not start of line
161    match_not_eol = match_not_bol << 1,               // last is not end of line
162    match_not_bob = match_not_eol << 1,               // first is not start of buffer
163    match_not_eob = match_not_bob << 1,               // last is not end of buffer
164    match_not_bow = match_not_eob << 1,               // first is not start of word
165    match_not_eow = match_not_bow << 1,               // last is not end of word
166    match_not_dot_newline = match_not_eow << 1,       // \n is not matched by '.'
167    match_not_dot_null = match_not_dot_newline << 1,  // '\0' is not matched by '.'
168    match_prev_avail = match_not_dot_null << 1,       // *--first is a valid expression
169    match_init = match_prev_avail << 1,               // internal use
170    match_any = match_init << 1,                      // don't care what we match
171    match_not_null = match_any << 1,                  // string can't be null
172    match_continuous = match_not_null << 1,           // each grep match must continue from
173                                                      // uninterupted from the previous one
174    match_partial = match_continuous << 1,            // find partial matches
175    
176    match_stop = match_partial << 1,                  // stop after first match (grep)
177    match_all = match_stop << 1,                      // must find the whole of input even if match_any is set
178    match_max = match_all
179 };
180
181 typedef unsigned long match_flag_type;
182
183 #ifdef __cplusplus
184 } // extern "C"
185 } // namespace
186 #endif
187
188
189 #ifdef __BORLANDC__
190  #if __BORLANDC__ > 0x520
191   #pragma option pop
192  #endif
193 #endif
194
195
196 //
197 // C++ high level wrapper goes here:
198 //
199 #if defined(__cplusplus)
200 #include <string>
201 #include <vector>
202 namespace boost{
203
204 #ifdef __BORLANDC__
205    #if __BORLANDC__ == 0x530
206     #pragma option push -a4 -b
207    #elif __BORLANDC__ > 0x530
208     #pragma option push -a8 -b
209    #endif
210 #endif
211
212 class RegEx;
213
214 namespace re_detail{
215
216 class RegExData;
217 struct pred1;
218 struct pred2;
219 struct pred3;
220 struct pred4;
221
222 }  // namespace re_detail
223
224 #if defined(BOOST_MSVC) || defined(__BORLANDC__)
225 typedef bool (__cdecl *GrepCallback)(const RegEx& expression);
226 typedef bool (__cdecl *GrepFileCallback)(const char* file, const RegEx& expression);
227 typedef bool (__cdecl *FindFilesCallback)(const char* file);
228 #else
229 typedef bool (*GrepCallback)(const RegEx& expression);
230 typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
231 typedef bool (*FindFilesCallback)(const char* file);
232 #endif
233
234 class BOOST_REGEX_DECL RegEx
235 {
236 private:
237    re_detail::RegExData* pdata;
238 public:
239    RegEx();
240    RegEx(const RegEx& o);
241    ~RegEx();
242    explicit RegEx(const char* c, bool icase = false);
243    explicit RegEx(const std::string& s, bool icase = false);
244    RegEx& operator=(const RegEx& o);
245    RegEx& operator=(const char* p);
246    RegEx& operator=(const std::string& s){ return this->operator=(s.c_str()); }
247    unsigned int SetExpression(const char* p, bool icase = false);
248    unsigned int SetExpression(const std::string& s, bool icase = false){ return SetExpression(s.c_str(), icase); }
249    std::string Expression()const;
250    unsigned int error_code()const;
251    //
252    // now matching operators:
253    //
254    bool Match(const char* p, match_flag_type flags = match_default);
255    bool Match(const std::string& s, match_flag_type flags = match_default) { return Match(s.c_str(), flags); }
256    bool Search(const char* p, match_flag_type flags = match_default);
257    bool Search(const std::string& s, match_flag_type flags = match_default) { return Search(s.c_str(), flags); }
258    unsigned int Grep(GrepCallback cb, const char* p, match_flag_type flags = match_default);
259    unsigned int Grep(GrepCallback cb, const std::string& s, match_flag_type flags = match_default) { return Grep(cb, s.c_str(), flags); }
260    unsigned int Grep(std::vector<std::string>& v, const char* p, match_flag_type flags = match_default);
261    unsigned int Grep(std::vector<std::string>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); }
262    unsigned int Grep(std::vector<std::size_t>& v, const char* p, match_flag_type flags = match_default);
263    unsigned int Grep(std::vector<std::size_t>& v, const std::string& s, match_flag_type flags = match_default) { return Grep(v, s.c_str(), flags); }
264 #ifndef BOOST_REGEX_NO_FILEITER
265    unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default);
266    unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return GrepFiles(cb, files.c_str(), recurse, flags); }
267    unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, match_flag_type flags = match_default);
268    unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, match_flag_type flags = match_default) { return FindFiles(cb, files.c_str(), recurse, flags); }
269 #endif
270
271    std::string Merge(const std::string& in, const std::string& fmt,
272                        bool copy = true, match_flag_type flags = match_default);
273    std::string Merge(const char* in, const char* fmt,
274                        bool copy = true, match_flag_type flags = match_default);
275
276    std::size_t Split(std::vector<std::string>& v, std::string& s, match_flag_type flags = match_default, unsigned max_count = ~0);
277    //
278    // now operators for returning what matched in more detail:
279    //
280    std::size_t Position(int i = 0)const;
281    std::size_t Length(int i = 0)const;
282    bool Matched(int i = 0)const;
283    unsigned int Line()const;
284    unsigned int Marks()const;
285    std::string What(int i = 0)const;
286    std::string operator[](int i)const { return What(i); }
287
288    static const unsigned int npos;
289
290    friend struct re_detail::pred1;
291    friend struct re_detail::pred2;
292    friend struct re_detail::pred3;
293    friend struct re_detail::pred4;
294 };
295
296 #ifdef __BORLANDC__
297   #pragma option pop
298 #endif
299
300 } // namespace boost
301
302 #endif
303
304 #endif // include guard
305
306
307
308
309
310
311
312
313
314