~ubuntu-branches/ubuntu/quantal/kyotocabinet/quantal

« back to all changes in this revision

Viewing changes to kcregex.h

  • Committer: Package Import Robot
  • Author(s): Shawn Landden
  • Date: 2012-06-07 16:12:07 UTC
  • Revision ID: package-import@ubuntu.com-20120607161207-prbj5blqgzzfl8of
Tags: upstream-1.2.76
ImportĀ upstreamĀ versionĀ 1.2.76

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************************
 
2
 * Regular expression
 
3
 *                                                               Copyright (C) 2009-2012 FAL Labs
 
4
 * This file is part of Kyoto Cabinet.
 
5
 * This program is free software: you can redistribute it and/or modify it under the terms of
 
6
 * the GNU General Public License as published by the Free Software Foundation, either version
 
7
 * 3 of the License, or any later version.
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 
9
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
10
 * See the GNU General Public License for more details.
 
11
 * You should have received a copy of the GNU General Public License along with this program.
 
12
 * If not, see <http://www.gnu.org/licenses/>.
 
13
 *************************************************************************************************/
 
14
 
 
15
 
 
16
#ifndef _KCREGEX_H                       // duplication check
 
17
#define _KCREGEX_H
 
18
 
 
19
#include <kccommon.h>
 
20
#include <kcutil.h>
 
21
 
 
22
namespace kyotocabinet {                 // common namespace
 
23
 
 
24
 
 
25
/**
 
26
 * Regular expression.
 
27
 */
 
28
class Regex {
 
29
 public:
 
30
  /**
 
31
   * Options.
 
32
   */
 
33
  enum Option {
 
34
    IGNCASE = 1 << 0,                    ///< case-insensitive
 
35
    MATCHONLY = 1 << 1,                  ///< matching only
 
36
  };
 
37
  /**
 
38
   * Default constructor.
 
39
   */
 
40
  explicit Regex();
 
41
  /**
 
42
   * Destructor.
 
43
   */
 
44
  ~Regex();
 
45
  /**
 
46
   * Compile a string of regular expression.
 
47
   * @param regex the string of regular expression.
 
48
   * @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
 
49
   * matching, Regex::MATCHONLY for matching only usage.
 
50
   */
 
51
  bool compile(const std::string& regex, uint32_t opts = 0);
 
52
  /**
 
53
   * Check whether a string matches the regular expression.
 
54
   * @param str the string.
 
55
   * @return true if the string matches, or false if not.
 
56
   */
 
57
  bool match(const std::string& str);
 
58
  /**
 
59
   * Check whether a string matches the regular expression.
 
60
   * @param str the string.
 
61
   * @param alt the alternative string with which each substring is replaced.  Each "$" in the
 
62
   * string escapes the following character.  Special escapes "$1" through "$9" refer to partial
 
63
   * substrings corresponding to sub-expressions in the regular expression.  "$0" and "$&" refer
 
64
   * to the whole matching substring.
 
65
   * @return the result string.
 
66
   */
 
67
  std::string replace(const std::string& str, const std::string& alt);
 
68
  /**
 
69
   * Check whether a string matches a regular expression.
 
70
   * @param str the string.
 
71
   * @param pattern the matching pattern.
 
72
   * @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
 
73
   * matching, Regex::MATCHONLY for matching only usage.
 
74
   * @return true if the string matches, or false if not.
 
75
   */
 
76
  static bool match(const std::string& str, const std::string& pattern, uint32_t opts = 0) {
 
77
    Regex regex;
 
78
    if (!regex.compile(pattern, opts)) return false;
 
79
    return regex.match(str);
 
80
  }
 
81
  /**
 
82
   * Check whether a string matches the regular expression.
 
83
   * @param str the string.
 
84
   * @param pattern the matching pattern.
 
85
   * @param alt the alternative string with which each substring is replaced.  Each "$" in the
 
86
   * string escapes the following character.  Special escapes "$1" through "$9" refer to partial
 
87
   * substrings corresponding to sub-expressions in the regular expression.  "$0" and "$&" refer
 
88
   * to the whole matching substring.
 
89
   * @param opts the optional features by bitwise-or: Regex::IGNCASE for case-insensitive
 
90
   * matching, Regex::MATCHONLY for matching only usage.
 
91
   * @return the result string.
 
92
   */
 
93
  static std::string replace(const std::string& str, const std::string& pattern,
 
94
                             const std::string& alt, uint32_t opts = 0) {
 
95
    Regex regex;
 
96
    if (!regex.compile(pattern, opts)) return str;
 
97
    return regex.replace(str, alt);
 
98
  }
 
99
 private:
 
100
  /** Dummy constructor to forbid the use. */
 
101
  Regex(const Regex&);
 
102
  /** Dummy Operator to forbid the use. */
 
103
  Regex& operator =(const Regex&);
 
104
  /** Opaque pointer. */
 
105
  void* opq_;
 
106
};
 
107
 
 
108
 
 
109
}                                        // common namespace
 
110
 
 
111
#endif                                   // duplication check
 
112
 
 
113
// END OF FILE