CUBRID Engine  latest
string_regex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // string_regex - definitions and functions related to regular expression
21 //
22 
23 #ifndef _STRING_REGEX_HPP_
24 #define _STRING_REGEX_HPP_
25 
26 #ifdef __cplusplus
27 #include <regex>
28 #include <locale>
29 
30 #include "error_manager.h"
31 #include "language_support.h"
32 
33 // forward declarations
34 namespace cubregex
35 {
36  struct compiled_regex;
37  struct cub_reg_traits;
38 }
39 
40 // alias
41 using cub_compiled_regex = cubregex::compiled_regex;
42 using cub_regex_object = std::basic_regex <wchar_t, cubregex::cub_reg_traits>;
43 using cub_regex_iterator = std::regex_iterator<std::wstring::iterator, wchar_t, cubregex::cub_reg_traits>;
44 using cub_regex_results = std::match_results <std::wstring::iterator>;
45 
46 namespace cubregex
47 {
48  struct compiled_regex
49  {
50  cub_regex_object *regex;
51  char *pattern;
52 
53  compiled_regex ();
54  ~compiled_regex ();
55  };
56 
57  /* it throws the error_collate when collatename syntax ([[. .]]), which gives an inconsistent result, is detected. */
58  struct cub_reg_traits : std::regex_traits<wchar_t>
59  {
60  template< class Iter >
61  string_type lookup_collatename ( Iter first, Iter last ) const
62  {
63  throw std::regex_error (std::regex_constants::error_collate);
64  }
65 
66  bool isctype ( char_type c, char_class_type f ) const
67  {
68 #if !defined(WINDOWS)
69  // HACK: matching '[[:blank:]]' for blank character doesn't work on gcc
70  // C++ regex uses std::ctype<char_type>::is () to match character class
71  // It does not support blank char class type so '[[:blank:]]' doesn't work to match ' '(0x20).
72  // For backward compatability, Here use iswblank () explicitly to match blank character.
73  if ((f & std::ctype_base::blank) == 1)
74  {
75  return std::iswblank (c);
76  }
77 #endif
78  return std::regex_traits<char_type>::isctype (c, f);
79  }
80  };
81 
82  void clear (cub_regex_object *&compiled_regex, char *&compiled_pattern);
83  int parse_match_type (std::regex_constants::syntax_option_type &reg_flags, std::string &opt_str);
84 
85  /* because regex_error::what() gives different messages depending on compiler, an error message should be returned by error code of regex_error explicitly. */
86  std::string parse_regex_exception (std::regex_error &e);
87 
88  bool check_should_recompile (const cub_regex_object *compiled_regex, const char *compiled_pattern,
89  const std::string &pattern,
90  const std::regex_constants::syntax_option_type reg_flags);
91 
92  int compile (cub_regex_object *&rx_compiled_regex, const char *pattern,
93  const std::regex_constants::syntax_option_type reg_flags, const LANG_COLLATION *collation);
94  int search (int &result, const cub_regex_object &reg, const std::string &src, const INTL_CODESET codeset);
95 
96  int count (int &result, const cub_regex_object &reg, const std::string &src, const int position,
97  const INTL_CODESET codeset);
98  int instr (int &result, const cub_regex_object &reg, const std::string &src,
99  const int position, const int occurrence, const int return_opt, const INTL_CODESET codeset);
100  int replace (std::string &result, const cub_regex_object &reg, const std::string &src,
101  const std::string &repl, const int position,
102  const int occurrence, const INTL_CODESET codeset);
103  int substr (std::string &result, bool &is_matched, const cub_regex_object &reg, const std::string &src,
104  const int position, const int occurrence, const INTL_CODESET codeset);
105 }
106 #endif
107 
108 #endif // _STRING_REGEX_HPP_
int instr(int &result, const cub_regex_object &reg, const std::string &src, const int position, const int occurrence, const int return_opt, const INTL_CODESET codeset)
int compile(cub_regex_object *&compiled_regex, const char *pattern, const std::regex_constants::syntax_option_type reg_flags, const LANG_COLLATION *collation)
int substr(std::string &result, bool &is_matched, const cub_regex_object &reg, const std::string &src, const int position, const int occurrence, const INTL_CODESET codeset)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
int parse_match_type(std::regex_constants::syntax_option_type &reg_flags, std::string &opt_str)
int replace(std::string &result, const cub_regex_object &reg, const std::string &src, const std::string &repl, const int position, const int occurrence, const INTL_CODESET codeset)
enum intl_codeset INTL_CODESET
Definition: intl_support.h:190
std::string parse_regex_exception(std::regex_error &e)
bool check_should_recompile(const cub_regex_object *compiled_regex, const char *compiled_pattern, const std::string &pattern, const std::regex_constants::syntax_option_type reg_flags)
int search(int &result, const cub_regex_object &reg, const std::string &src, const INTL_CODESET codeset)
void clear(cub_regex_object *&regex, char *&pattern)