/[pcre]/code/trunk/pcrecpp.cc
ViewVC logotype

Contents of /code/trunk/pcrecpp.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (show annotations)
Sat Feb 24 21:41:21 2007 UTC (8 years, 4 months ago) by nigel
File size: 26943 byte(s)
Error occurred while calculating annotation data.
Load pcre-6.5 into code/trunk.
1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Sanjay Ghemawat
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <limits.h> /* for SHRT_MIN, USHRT_MAX, etc */
36 #include <assert.h>
37 #include <errno.h>
38 #include <string>
39 #include <algorithm>
40 #include "config.h"
41 // We need this to compile the proper dll on windows/msys. This is copied
42 // from pcre_internal.h. It would probably be better just to include that.
43 #define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */
44 #include "pcre.h"
45 #include "pcre_stringpiece.h"
46 #include "pcrecpp.h"
47
48
49 namespace pcrecpp {
50
51 // Maximum number of args we can set
52 static const int kMaxArgs = 16;
53 static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
54
55 // Special object that stands-in for no argument
56 Arg no_arg((void*)NULL);
57
58 // If a regular expression has no error, its error_ field points here
59 static const string empty_string;
60
61 // If the user doesn't ask for any options, we just use this one
62 static RE_Options default_options;
63
64 void RE::Init(const char* pat, const RE_Options* options) {
65 pattern_ = pat;
66 if (options == NULL) {
67 options_ = default_options;
68 } else {
69 options_ = *options;
70 }
71 error_ = &empty_string;
72 re_full_ = NULL;
73 re_partial_ = NULL;
74
75 re_partial_ = Compile(UNANCHORED);
76 if (re_partial_ != NULL) {
77 // Check for complicated patterns. The following change is
78 // conservative in that it may treat some "simple" patterns
79 // as "complex" (e.g., if the vertical bar is in a character
80 // class or is escaped). But it seems good enough.
81 if (strchr(pat, '|') == NULL) {
82 // Simple pattern: we can use position-based checks to perform
83 // fully anchored matches
84 re_full_ = re_partial_;
85 } else {
86 // We need a special pattern for anchored matches
87 re_full_ = Compile(ANCHOR_BOTH);
88 }
89 }
90 }
91
92 RE::~RE() {
93 if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_);
94 if (re_partial_ != NULL) (*pcre_free)(re_partial_);
95 if (error_ != &empty_string) delete error_;
96 }
97
98 pcre* RE::Compile(Anchor anchor) {
99 // First, convert RE_Options into pcre options
100 int pcre_options = 0;
101 pcre_options = options_.all_options();
102
103 // Special treatment for anchoring. This is needed because at
104 // runtime pcre only provides an option for anchoring at the
105 // beginning of a string (unless you use offset).
106 //
107 // There are three types of anchoring we want:
108 // UNANCHORED Compile the original pattern, and use
109 // a pcre unanchored match.
110 // ANCHOR_START Compile the original pattern, and use
111 // a pcre anchored match.
112 // ANCHOR_BOTH Tack a "\z" to the end of the original pattern
113 // and use a pcre anchored match.
114
115 const char* compile_error;
116 int eoffset;
117 pcre* re;
118 if (anchor != ANCHOR_BOTH) {
119 re = pcre_compile(pattern_.c_str(), pcre_options,
120 &compile_error, &eoffset, NULL);
121 } else {
122 // Tack a '\z' at the end of RE. Parenthesize it first so that
123 // the '\z' applies to all top-level alternatives in the regexp.
124 string wrapped = "(?:"; // A non-counting grouping operator
125 wrapped += pattern_;
126 wrapped += ")\\z";
127 re = pcre_compile(wrapped.c_str(), pcre_options,
128 &compile_error, &eoffset, NULL);
129 }
130 if (re == NULL) {
131 if (error_ == &empty_string) error_ = new string(compile_error);
132 }
133 return re;
134 }
135
136 /***** Matching interfaces *****/
137
138 bool RE::FullMatch(const StringPiece& text,
139 const Arg& ptr1,
140 const Arg& ptr2,
141 const Arg& ptr3,
142 const Arg& ptr4,
143 const Arg& ptr5,
144 const Arg& ptr6,
145 const Arg& ptr7,
146 const Arg& ptr8,
147 const Arg& ptr9,
148 const Arg& ptr10,
149 const Arg& ptr11,
150 const Arg& ptr12,
151 const Arg& ptr13,
152 const Arg& ptr14,
153 const Arg& ptr15,
154 const Arg& ptr16) const {
155 const Arg* args[kMaxArgs];
156 int n = 0;
157 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
158 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
159 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
160 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
161 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
162 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
163 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
164 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
165 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
166 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
167 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
168 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
169 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
170 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
171 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
172 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
173 done:
174
175 int consumed;
176 int vec[kVecSize];
177 return DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
178 }
179
180 bool RE::PartialMatch(const StringPiece& text,
181 const Arg& ptr1,
182 const Arg& ptr2,
183 const Arg& ptr3,
184 const Arg& ptr4,
185 const Arg& ptr5,
186 const Arg& ptr6,
187 const Arg& ptr7,
188 const Arg& ptr8,
189 const Arg& ptr9,
190 const Arg& ptr10,
191 const Arg& ptr11,
192 const Arg& ptr12,
193 const Arg& ptr13,
194 const Arg& ptr14,
195 const Arg& ptr15,
196 const Arg& ptr16) const {
197 const Arg* args[kMaxArgs];
198 int n = 0;
199 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
200 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
201 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
202 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
203 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
204 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
205 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
206 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
207 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
208 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
209 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
210 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
211 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
212 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
213 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
214 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
215 done:
216
217 int consumed;
218 int vec[kVecSize];
219 return DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
220 }
221
222 bool RE::Consume(StringPiece* input,
223 const Arg& ptr1,
224 const Arg& ptr2,
225 const Arg& ptr3,
226 const Arg& ptr4,
227 const Arg& ptr5,
228 const Arg& ptr6,
229 const Arg& ptr7,
230 const Arg& ptr8,
231 const Arg& ptr9,
232 const Arg& ptr10,
233 const Arg& ptr11,
234 const Arg& ptr12,
235 const Arg& ptr13,
236 const Arg& ptr14,
237 const Arg& ptr15,
238 const Arg& ptr16) const {
239 const Arg* args[kMaxArgs];
240 int n = 0;
241 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
242 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
243 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
244 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
245 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
246 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
247 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
248 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
249 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
250 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
251 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
252 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
253 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
254 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
255 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
256 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
257 done:
258
259 int consumed;
260 int vec[kVecSize];
261 if (DoMatchImpl(*input, ANCHOR_START, &consumed,
262 args, n, vec, kVecSize)) {
263 input->remove_prefix(consumed);
264 return true;
265 } else {
266 return false;
267 }
268 }
269
270 bool RE::FindAndConsume(StringPiece* input,
271 const Arg& ptr1,
272 const Arg& ptr2,
273 const Arg& ptr3,
274 const Arg& ptr4,
275 const Arg& ptr5,
276 const Arg& ptr6,
277 const Arg& ptr7,
278 const Arg& ptr8,
279 const Arg& ptr9,
280 const Arg& ptr10,
281 const Arg& ptr11,
282 const Arg& ptr12,
283 const Arg& ptr13,
284 const Arg& ptr14,
285 const Arg& ptr15,
286 const Arg& ptr16) const {
287 const Arg* args[kMaxArgs];
288 int n = 0;
289 if (&ptr1 == &no_arg) goto done; args[n++] = &ptr1;
290 if (&ptr2 == &no_arg) goto done; args[n++] = &ptr2;
291 if (&ptr3 == &no_arg) goto done; args[n++] = &ptr3;
292 if (&ptr4 == &no_arg) goto done; args[n++] = &ptr4;
293 if (&ptr5 == &no_arg) goto done; args[n++] = &ptr5;
294 if (&ptr6 == &no_arg) goto done; args[n++] = &ptr6;
295 if (&ptr7 == &no_arg) goto done; args[n++] = &ptr7;
296 if (&ptr8 == &no_arg) goto done; args[n++] = &ptr8;
297 if (&ptr9 == &no_arg) goto done; args[n++] = &ptr9;
298 if (&ptr10 == &no_arg) goto done; args[n++] = &ptr10;
299 if (&ptr11 == &no_arg) goto done; args[n++] = &ptr11;
300 if (&ptr12 == &no_arg) goto done; args[n++] = &ptr12;
301 if (&ptr13 == &no_arg) goto done; args[n++] = &ptr13;
302 if (&ptr14 == &no_arg) goto done; args[n++] = &ptr14;
303 if (&ptr15 == &no_arg) goto done; args[n++] = &ptr15;
304 if (&ptr16 == &no_arg) goto done; args[n++] = &ptr16;
305 done:
306
307 int consumed;
308 int vec[kVecSize];
309 if (DoMatchImpl(*input, UNANCHORED, &consumed,
310 args, n, vec, kVecSize)) {
311 input->remove_prefix(consumed);
312 return true;
313 } else {
314 return false;
315 }
316 }
317
318 bool RE::Replace(const StringPiece& rewrite,
319 string *str) const {
320 int vec[kVecSize];
321 int matches = TryMatch(*str, 0, UNANCHORED, vec, kVecSize);
322 if (matches == 0)
323 return false;
324
325 string s;
326 if (!Rewrite(&s, rewrite, *str, vec, matches))
327 return false;
328
329 assert(vec[0] >= 0);
330 assert(vec[1] >= 0);
331 str->replace(vec[0], vec[1] - vec[0], s);
332 return true;
333 }
334
335 int RE::GlobalReplace(const StringPiece& rewrite,
336 string *str) const {
337 int count = 0;
338 int vec[kVecSize];
339 string out;
340 int start = 0;
341 int lastend = -1;
342
343 for (; start <= static_cast<int>(str->length()); count++) {
344 int matches = TryMatch(*str, start, UNANCHORED, vec, kVecSize);
345 if (matches <= 0)
346 break;
347 int matchstart = vec[0], matchend = vec[1];
348 assert(matchstart >= start);
349 assert(matchend >= matchstart);
350 if (matchstart == matchend && matchstart == lastend) {
351 // advance one character if we matched an empty string at the same
352 // place as the last match occurred
353 if (start < static_cast<int>(str->length()))
354 out.push_back((*str)[start]);
355 start++;
356 } else {
357 out.append(*str, start, matchstart - start);
358 Rewrite(&out, rewrite, *str, vec, matches);
359 start = matchend;
360 lastend = matchend;
361 count++;
362 }
363 }
364
365 if (count == 0)
366 return 0;
367
368 if (start < static_cast<int>(str->length()))
369 out.append(*str, start, str->length() - start);
370 swap(out, *str);
371 return count;
372 }
373
374 bool RE::Extract(const StringPiece& rewrite,
375 const StringPiece& text,
376 string *out) const {
377 int vec[kVecSize];
378 int matches = TryMatch(text, 0, UNANCHORED, vec, kVecSize);
379 if (matches == 0)
380 return false;
381 out->erase();
382 return Rewrite(out, rewrite, text, vec, matches);
383 }
384
385 /***** Actual matching and rewriting code *****/
386
387 int RE::TryMatch(const StringPiece& text,
388 int startpos,
389 Anchor anchor,
390 int *vec,
391 int vecsize) const {
392 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
393 if (re == NULL) {
394 //fprintf(stderr, "Matching against invalid re: %s\n", error_->c_str());
395 return 0;
396 }
397
398 pcre_extra extra = { 0 };
399 if (options_.match_limit() > 0) {
400 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
401 extra.match_limit = options_.match_limit();
402 }
403 if (options_.match_limit_recursion() > 0) {
404 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
405 extra.match_limit_recursion = options_.match_limit_recursion();
406 }
407 int rc = pcre_exec(re, // The regular expression object
408 &extra,
409 (text.data() == NULL) ? "" : text.data(),
410 text.size(),
411 startpos,
412 (anchor == UNANCHORED) ? 0 : PCRE_ANCHORED,
413 vec,
414 vecsize);
415
416 // Handle errors
417 if (rc == PCRE_ERROR_NOMATCH) {
418 return 0;
419 } else if (rc < 0) {
420 //fprintf(stderr, "Unexpected return code: %d when matching '%s'\n",
421 // re, pattern_.c_str());
422 return 0;
423 } else if (rc == 0) {
424 // pcre_exec() returns 0 as a special case when the number of
425 // capturing subpatterns exceeds the size of the vector.
426 // When this happens, there is a match and the output vector
427 // is filled, but we miss out on the positions of the extra subpatterns.
428 rc = vecsize / 2;
429 }
430
431 if ((anchor == ANCHOR_BOTH) && (re_full_ == re_partial_)) {
432 // We need an extra check to make sure that the match extended
433 // to the end of the input string
434 assert(vec[0] == 0); // PCRE_ANCHORED forces starting match
435 if (vec[1] != text.size()) return 0; // Did not get ending match
436 }
437
438 return rc;
439 }
440
441 bool RE::DoMatchImpl(const StringPiece& text,
442 Anchor anchor,
443 int* consumed,
444 const Arg* const* args,
445 int n,
446 int* vec,
447 int vecsize) const {
448 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
449 int matches = TryMatch(text, 0, anchor, vec, vecsize);
450 assert(matches >= 0); // TryMatch never returns negatives
451 if (matches == 0)
452 return false;
453
454 *consumed = vec[1];
455
456 if (n == 0 || args == NULL) {
457 // We are not interested in results
458 return true;
459 }
460
461 if (NumberOfCapturingGroups() < n) {
462 // RE has fewer capturing groups than number of arg pointers passed in
463 return false;
464 }
465
466 // If we got here, we must have matched the whole pattern.
467 // We do not need (can not do) any more checks on the value of 'matches' here
468 // -- see the comment for TryMatch.
469 for (int i = 0; i < n; i++) {
470 const int start = vec[2*(i+1)];
471 const int limit = vec[2*(i+1)+1];
472 if (!args[i]->Parse(text.data() + start, limit-start)) {
473 // TODO: Should we indicate what the error was?
474 return false;
475 }
476 }
477
478 return true;
479 }
480
481 bool RE::DoMatch(const StringPiece& text,
482 Anchor anchor,
483 int* consumed,
484 const Arg* const args[],
485 int n) const {
486 assert(n >= 0);
487 size_t const vecsize = (1 + n) * 3; // results + PCRE workspace
488 // (as for kVecSize)
489 int space[21]; // use stack allocation for small vecsize (common case)
490 int* vec = vecsize <= 21 ? space : new int[vecsize];
491 bool retval = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
492 if (vec != space) delete [] vec;
493 return retval;
494 }
495
496 bool RE::Rewrite(string *out, const StringPiece &rewrite,
497 const StringPiece &text, int *vec, int veclen) const {
498 for (const char *s = rewrite.data(), *end = s + rewrite.size();
499 s < end; s++) {
500 int c = *s;
501 if (c == '\\') {
502 c = *++s;
503 if (isdigit(c)) {
504 int n = (c - '0');
505 if (n >= veclen) {
506 //fprintf(stderr, requested group %d in regexp %.*s\n",
507 // n, rewrite.size(), rewrite.data());
508 return false;
509 }
510 int start = vec[2 * n];
511 if (start >= 0)
512 out->append(text.data() + start, vec[2 * n + 1] - start);
513 } else if (c == '\\') {
514 out->push_back('\\');
515 } else {
516 //fprintf(stderr, "invalid rewrite pattern: %.*s\n",
517 // rewrite.size(), rewrite.data());
518 return false;
519 }
520 } else {
521 out->push_back(c);
522 }
523 }
524 return true;
525 }
526
527 // Return the number of capturing subpatterns, or -1 if the
528 // regexp wasn't valid on construction.
529 int RE::NumberOfCapturingGroups() const {
530 if (re_partial_ == NULL) return -1;
531
532 int result;
533 int pcre_retval = pcre_fullinfo(re_partial_, // The regular expression object
534 NULL, // We did not study the pattern
535 PCRE_INFO_CAPTURECOUNT,
536 &result);
537 assert(pcre_retval == 0);
538 return result;
539 }
540
541 /***** Parsers for various types *****/
542
543 bool Arg::parse_null(const char* str, int n, void* dest) {
544 // We fail if somebody asked us to store into a non-NULL void* pointer
545 return (dest == NULL);
546 }
547
548 bool Arg::parse_string(const char* str, int n, void* dest) {
549 reinterpret_cast<string*>(dest)->assign(str, n);
550 return true;
551 }
552
553 bool Arg::parse_stringpiece(const char* str, int n, void* dest) {
554 reinterpret_cast<StringPiece*>(dest)->set(str, n);
555 return true;
556 }
557
558 bool Arg::parse_char(const char* str, int n, void* dest) {
559 if (n != 1) return false;
560 *(reinterpret_cast<char*>(dest)) = str[0];
561 return true;
562 }
563
564 bool Arg::parse_uchar(const char* str, int n, void* dest) {
565 if (n != 1) return false;
566 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
567 return true;
568 }
569
570 // Largest number spec that we are willing to parse
571 static const int kMaxNumberLength = 32;
572
573 // REQUIRES "buf" must have length at least kMaxNumberLength+1
574 // REQUIRES "n > 0"
575 // Copies "str" into "buf" and null-terminates if necessary.
576 // Returns one of:
577 // a. "str" if no termination is needed
578 // b. "buf" if the string was copied and null-terminated
579 // c. "" if the input was invalid and has no hope of being parsed
580 static const char* TerminateNumber(char* buf, const char* str, int n) {
581 if ((n > 0) && isspace(*str)) {
582 // We are less forgiving than the strtoxxx() routines and do not
583 // allow leading spaces.
584 return "";
585 }
586
587 // See if the character right after the input text may potentially
588 // look like a digit.
589 if (isdigit(str[n]) ||
590 ((str[n] >= 'a') && (str[n] <= 'f')) ||
591 ((str[n] >= 'A') && (str[n] <= 'F'))) {
592 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
593 memcpy(buf, str, n);
594 buf[n] = '\0';
595 return buf;
596 } else {
597 // We can parse right out of the supplied string, so return it.
598 return str;
599 }
600 }
601
602 bool Arg::parse_long_radix(const char* str,
603 int n,
604 void* dest,
605 int radix) {
606 if (n == 0) return false;
607 char buf[kMaxNumberLength+1];
608 str = TerminateNumber(buf, str, n);
609 char* end;
610 errno = 0;
611 long r = strtol(str, &end, radix);
612 if (end != str + n) return false; // Leftover junk
613 if (errno) return false;
614 *(reinterpret_cast<long*>(dest)) = r;
615 return true;
616 }
617
618 bool Arg::parse_ulong_radix(const char* str,
619 int n,
620 void* dest,
621 int radix) {
622 if (n == 0) return false;
623 char buf[kMaxNumberLength+1];
624 str = TerminateNumber(buf, str, n);
625 if (str[0] == '-') return false; // strtoul() on a negative number?!
626 char* end;
627 errno = 0;
628 unsigned long r = strtoul(str, &end, radix);
629 if (end != str + n) return false; // Leftover junk
630 if (errno) return false;
631 *(reinterpret_cast<unsigned long*>(dest)) = r;
632 return true;
633 }
634
635 bool Arg::parse_short_radix(const char* str,
636 int n,
637 void* dest,
638 int radix) {
639 long r;
640 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
641 if (r < SHRT_MIN || r > SHRT_MAX) return false; // Out of range
642 *(reinterpret_cast<short*>(dest)) = r;
643 return true;
644 }
645
646 bool Arg::parse_ushort_radix(const char* str,
647 int n,
648 void* dest,
649 int radix) {
650 unsigned long r;
651 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
652 if (r > USHRT_MAX) return false; // Out of range
653 *(reinterpret_cast<unsigned short*>(dest)) = r;
654 return true;
655 }
656
657 bool Arg::parse_int_radix(const char* str,
658 int n,
659 void* dest,
660 int radix) {
661 long r;
662 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
663 if (r < INT_MIN || r > INT_MAX) return false; // Out of range
664 *(reinterpret_cast<int*>(dest)) = r;
665 return true;
666 }
667
668 bool Arg::parse_uint_radix(const char* str,
669 int n,
670 void* dest,
671 int radix) {
672 unsigned long r;
673 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
674 if (r > UINT_MAX) return false; // Out of range
675 *(reinterpret_cast<unsigned int*>(dest)) = r;
676 return true;
677 }
678
679 bool Arg::parse_longlong_radix(const char* str,
680 int n,
681 void* dest,
682 int radix) {
683 #ifndef HAVE_LONG_LONG
684 return false;
685 #else
686 if (n == 0) return false;
687 char buf[kMaxNumberLength+1];
688 str = TerminateNumber(buf, str, n);
689 char* end;
690 errno = 0;
691 #if defined HAVE_STRTOQ
692 long long r = strtoq(str, &end, radix);
693 #elif defined HAVE_STRTOLL
694 long long r = strtoll(str, &end, radix);
695 #else
696 #error parse_longlong_radix: cannot convert input to a long-long
697 #endif
698 if (end != str + n) return false; // Leftover junk
699 if (errno) return false;
700 *(reinterpret_cast<long long*>(dest)) = r;
701 return true;
702 #endif /* HAVE_LONG_LONG */
703 }
704
705 bool Arg::parse_ulonglong_radix(const char* str,
706 int n,
707 void* dest,
708 int radix) {
709 #ifndef HAVE_UNSIGNED_LONG_LONG
710 return false;
711 #else
712 if (n == 0) return false;
713 char buf[kMaxNumberLength+1];
714 str = TerminateNumber(buf, str, n);
715 if (str[0] == '-') return false; // strtoull() on a negative number?!
716 char* end;
717 errno = 0;
718 #if defined HAVE_STRTOQ
719 unsigned long long r = strtouq(str, &end, radix);
720 #elif defined HAVE_STRTOLL
721 unsigned long long r = strtoull(str, &end, radix);
722 #else
723 #error parse_ulonglong_radix: cannot convert input to a long-long
724 #endif
725 if (end != str + n) return false; // Leftover junk
726 if (errno) return false;
727 *(reinterpret_cast<unsigned long long*>(dest)) = r;
728 return true;
729 #endif /* HAVE_UNSIGNED_LONG_LONG */
730 }
731
732 bool Arg::parse_double(const char* str, int n, void* dest) {
733 if (n == 0) return false;
734 static const int kMaxLength = 200;
735 char buf[kMaxLength];
736 if (n >= kMaxLength) return false;
737 memcpy(buf, str, n);
738 buf[n] = '\0';
739 errno = 0;
740 char* end;
741 double r = strtod(buf, &end);
742 if (end != buf + n) return false; // Leftover junk
743 if (errno) return false;
744 *(reinterpret_cast<double*>(dest)) = r;
745 return true;
746 }
747
748 bool Arg::parse_float(const char* str, int n, void* dest) {
749 double r;
750 if (!parse_double(str, n, &r)) return false;
751 *(reinterpret_cast<float*>(dest)) = static_cast<float>(r);
752 return true;
753 }
754
755
756 #define DEFINE_INTEGER_PARSERS(name) \
757 bool Arg::parse_##name(const char* str, int n, void* dest) { \
758 return parse_##name##_radix(str, n, dest, 10); \
759 } \
760 bool Arg::parse_##name##_hex(const char* str, int n, void* dest) { \
761 return parse_##name##_radix(str, n, dest, 16); \
762 } \
763 bool Arg::parse_##name##_octal(const char* str, int n, void* dest) { \
764 return parse_##name##_radix(str, n, dest, 8); \
765 } \
766 bool Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
767 return parse_##name##_radix(str, n, dest, 0); \
768 }
769
770 DEFINE_INTEGER_PARSERS(short);
771 DEFINE_INTEGER_PARSERS(ushort);
772 DEFINE_INTEGER_PARSERS(int);
773 DEFINE_INTEGER_PARSERS(uint);
774 DEFINE_INTEGER_PARSERS(long);
775 DEFINE_INTEGER_PARSERS(ulong);
776 DEFINE_INTEGER_PARSERS(longlong);
777 DEFINE_INTEGER_PARSERS(ulonglong);
778
779 #undef DEFINE_INTEGER_PARSERS
780
781 } // namespace pcrecpp

  ViewVC Help
Powered by ViewVC 1.1.5