OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IMPORT_HELPERS_H_
18 #define IMPORT_HELPERS_H_
19 
21 #include <boost/algorithm/string.hpp>
22 #include <boost/algorithm/string/replace.hpp>
23 #include <boost/regex.hpp>
24 
25 namespace ImportHelpers {
26 
27 inline bool is_reserved_name(const std::string& name) {
28  return reserved_keywords.find(boost::to_upper_copy<std::string>(name)) !=
29  reserved_keywords.end();
30 }
31 
32 inline std::string sanitize_name(const std::string& name) {
33  boost::regex invalid_chars{R"([^0-9a-z_])",
34  boost::regex::extended | boost::regex::icase};
35  std::string sanitized_name = boost::regex_replace(name, invalid_chars, "");
36  boost::regex starts_with_digit{R"(^[0-9].*)"};
37  if (boost::regex_match(sanitized_name, starts_with_digit)) {
38  sanitized_name = "_" + sanitized_name;
39  }
40  if (is_reserved_name(sanitized_name)) {
41  sanitized_name += "_";
42  }
43  return sanitized_name;
44 }
45 
46 } // namespace ImportHelpers
47 
48 #endif // IMPORT_HELPERS_H_
static std::set< std::string > reserved_keywords
string name
Definition: setup.in.py:72
bool is_reserved_name(const std::string &name)
std::string sanitize_name(const std::string &name)