Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 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 | #include "Common.h" |
| 18 | |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 23 | #include <algorithm> |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 24 | #include <cctype> |
| 25 | #include <cerrno> |
| 26 | #include <cmath> |
| 27 | #include <cstdlib> |
| 28 | #include <cstring> |
| 29 | #include <initializer_list> |
| 30 | #include <memory> |
| 31 | #include <regex> |
| 32 | #include <string> |
| 33 | #include <unordered_set> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <android-base/file.h> |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 38 | #include <android-base/strings.h> |
| 39 | #include <google/protobuf/text_format.h> |
| 40 | |
| 41 | #include "sysprop.pb.h" |
| 42 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 43 | using android::base::Result; |
| 44 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 45 | namespace { |
| 46 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 47 | std::string GenerateDefaultPropName(const sysprop::Properties& props, |
| 48 | const sysprop::Property& prop); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 49 | bool IsCorrectIdentifier(const std::string& name); |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 50 | Result<void> ValidateProp(const sysprop::Properties& props, |
| 51 | const sysprop::Property& prop); |
| 52 | Result<void> ValidateProps(const sysprop::Properties& props); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 53 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 54 | std::string GenerateDefaultPropName(const sysprop::Properties& props, |
| 55 | const sysprop::Property& prop) { |
| 56 | std::string ret; |
| 57 | |
| 58 | if (prop.access() != sysprop::ReadWrite) ret = "ro."; |
| 59 | |
| 60 | switch (props.owner()) { |
| 61 | case sysprop::Vendor: |
| 62 | ret += "vendor."; |
| 63 | break; |
| 64 | case sysprop::Odm: |
| 65 | ret += "odm."; |
| 66 | break; |
| 67 | default: |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | ret += prop.api_name(); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 76 | bool IsCorrectIdentifier(const std::string& name) { |
| 77 | if (name.empty()) return false; |
| 78 | if (std::isalpha(name[0]) == 0 && name[0] != '_') return false; |
| 79 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 80 | return std::all_of(name.begin() + 1, name.end(), [](char ch) { |
| 81 | return std::isalnum(ch) != 0 || ch == '_'; |
| 82 | }); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 85 | bool IsCorrectName(const std::string& name, |
| 86 | const std::unordered_set<char>& allowed_chars) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 87 | if (name.empty()) return false; |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 88 | if (!std::isalpha(*name.begin())) return false; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 89 | |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 90 | return std::all_of(name.begin(), name.end(), [allowed_chars](char ch) { |
| 91 | return std::isalnum(ch) != 0 || allowed_chars.count(ch) != 0; |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 92 | }); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 95 | bool IsCorrectPropertyName(const std::string& name) { |
| 96 | std::unordered_set<char> allowed{'_', '-', '.'}; |
| 97 | if (android::base::StartsWith(name, "ctl.")) { |
| 98 | allowed.emplace('$'); |
| 99 | } |
| 100 | return IsCorrectName(name, allowed); |
| 101 | } |
| 102 | |
| 103 | bool IsCorrectApiName(const std::string& name) { |
| 104 | static std::unordered_set<char> allowed{'_', '-'}; |
| 105 | return IsCorrectName(name, allowed); |
| 106 | } |
| 107 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 108 | Result<void> ValidateProp(const sysprop::Properties& props, |
| 109 | const sysprop::Property& prop) { |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 110 | if (!IsCorrectApiName(prop.api_name())) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 111 | return Errorf("Invalid API name \"{}\"", prop.api_name()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) { |
| 115 | std::vector<std::string> names = |
| 116 | android::base::Split(prop.enum_values(), "|"); |
| 117 | if (names.empty()) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 118 | return Errorf("Enum values are empty for API \"{}\"", prop.api_name()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | for (const std::string& name : names) { |
| 122 | if (!IsCorrectIdentifier(name)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 123 | return Errorf("Invalid enum value \"{}\" for API \"{}\"", name, |
| 124 | prop.api_name()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | std::unordered_set<std::string> name_set; |
| 129 | for (const std::string& name : names) { |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 130 | if (!name_set.insert(ToUpper(name)).second) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 131 | return Errorf("Duplicated enum value \"{}\" for API \"{}\"", name, |
| 132 | prop.api_name()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 137 | std::string prop_name = prop.prop_name(); |
| 138 | if (prop_name.empty()) prop_name = GenerateDefaultPropName(props, prop); |
| 139 | |
Jiyong Park | 87d439d | 2019-11-27 19:24:07 +0900 | [diff] [blame] | 140 | if (!IsCorrectPropertyName(prop_name)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 141 | return Errorf("Invalid prop name \"{}\"", prop.prop_name()); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Inseob Kim | fcb5db7 | 2019-03-15 14:33:01 +0900 | [diff] [blame] | 144 | static const std::regex vendor_regex( |
| 145 | "(init\\.svc\\.|ro\\.|persist\\.)?vendor\\..+|ro\\.hardware\\..+"); |
| 146 | static const std::regex odm_regex( |
| 147 | "(init\\.svc\\.|ro\\.|persist\\.)?odm\\..+|ro\\.hardware\\..+"); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 148 | |
| 149 | switch (props.owner()) { |
| 150 | case sysprop::Platform: |
| 151 | if (std::regex_match(prop_name, vendor_regex) || |
| 152 | std::regex_match(prop_name, odm_regex)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 153 | return Errorf( |
| 154 | "Prop \"{}\" owned by platform cannot have vendor. or odm. " |
| 155 | "namespace", |
| 156 | prop_name); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 157 | } |
| 158 | break; |
| 159 | case sysprop::Vendor: |
| 160 | if (!std::regex_match(prop_name, vendor_regex)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 161 | return Errorf( |
| 162 | "Prop \"{}\" owned by vendor should have vendor. namespace", |
| 163 | prop_name); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 164 | } |
| 165 | break; |
| 166 | case sysprop::Odm: |
| 167 | if (!std::regex_match(prop_name, odm_regex)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 168 | return Errorf("Prop \"{}\" owned by odm should have odm. namespace", |
| 169 | prop_name); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 170 | } |
| 171 | break; |
Inseob Kim | 8802c7d | 2018-11-02 15:01:47 +0900 | [diff] [blame] | 172 | default: |
| 173 | break; |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | switch (prop.access()) { |
| 177 | case sysprop::ReadWrite: |
| 178 | if (android::base::StartsWith(prop_name, "ro.")) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 179 | return Errorf("Prop \"{}\" is ReadWrite and also have prefix \"ro.\"", |
| 180 | prop_name); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 181 | } |
| 182 | break; |
| 183 | default: |
Inseob Kim | aa5054b | 2018-12-07 00:13:38 +0900 | [diff] [blame] | 184 | /* |
| 185 | * TODO: Some properties don't have prefix "ro." but not written in any |
| 186 | * Java or C++ codes. They might be misnamed and should be readonly. Will |
| 187 | * uncomment this check after fixing them all / or making a whitelist for |
| 188 | * them |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 189 | if (!android::base::StartsWith(prop_name, "ro.")) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 190 | return Errorf("Prop \"{}\" isn't ReadWrite, but don't have prefix |
| 191 | \"ro.\"", prop_name); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 192 | } |
Inseob Kim | aa5054b | 2018-12-07 00:13:38 +0900 | [diff] [blame] | 193 | */ |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 194 | break; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 195 | } |
| 196 | |
Inseob Kim | 9c5147d | 2019-03-06 11:36:36 +0900 | [diff] [blame] | 197 | if (prop.integer_as_bool() && !(prop.type() == sysprop::Boolean || |
| 198 | prop.type() == sysprop::BooleanList)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 199 | return Errorf("Prop \"{}\" has integer_as_bool: true, but not a boolean", |
| 200 | prop_name); |
Inseob Kim | 9c5147d | 2019-03-06 11:36:36 +0900 | [diff] [blame] | 201 | } |
| 202 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 203 | return {}; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 204 | } |
| 205 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 206 | Result<void> ValidateProps(const sysprop::Properties& props) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 207 | std::vector<std::string> names = android::base::Split(props.module(), "."); |
| 208 | if (names.size() <= 1) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 209 | return Errorf("Invalid module name \"{}\"", props.module()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | for (const auto& name : names) { |
| 213 | if (!IsCorrectIdentifier(name)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 214 | return Errorf("Invalid name \"{}\" in module", name); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 218 | if (props.prop_size() == 0) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 219 | return Errorf("There is no defined property"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | for (int i = 0; i < props.prop_size(); ++i) { |
| 223 | const auto& prop = props.prop(i); |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 224 | if (auto res = ValidateProp(props, prop); !res.ok()) return res; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | std::unordered_set<std::string> prop_names; |
| 228 | |
| 229 | for (int i = 0; i < props.prop_size(); ++i) { |
| 230 | const auto& prop = props.prop(i); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 231 | auto res = prop_names.insert(ApiNameToIdentifier(prop.api_name())); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 232 | |
| 233 | if (!res.second) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 234 | return Errorf("Duplicated API name \"{}\"", prop.api_name()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 238 | return {}; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Inseob Kim | 900fbad | 2019-06-26 14:09:20 +0900 | [diff] [blame] | 241 | void SetDefaultValues(sysprop::Properties* props) { |
| 242 | for (int i = 0; i < props->prop_size(); ++i) { |
| 243 | // set each optional field to its default value |
| 244 | sysprop::Property& prop = *props->mutable_prop(i); |
| 245 | if (prop.prop_name().empty()) |
| 246 | prop.set_prop_name(GenerateDefaultPropName(*props, prop)); |
| 247 | if (prop.scope() == sysprop::Scope::System) { |
| 248 | LOG(WARNING) << "Sysprop API " << prop.api_name() |
| 249 | << ": System scope is deprecated." |
| 250 | << " Please use Public scope instead."; |
| 251 | prop.set_scope(sysprop::Scope::Public); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 256 | } // namespace |
| 257 | |
Inseob Kim | b04d619 | 2018-12-14 08:33:16 +0900 | [diff] [blame] | 258 | bool IsListProp(const sysprop::Property& prop) { |
| 259 | switch (prop.type()) { |
| 260 | case sysprop::BooleanList: |
| 261 | case sysprop::IntegerList: |
| 262 | case sysprop::LongList: |
| 263 | case sysprop::DoubleList: |
| 264 | case sysprop::StringList: |
| 265 | case sysprop::EnumList: |
| 266 | return true; |
| 267 | default: |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 272 | std::string GetModuleName(const sysprop::Properties& props) { |
| 273 | const std::string& module = props.module(); |
| 274 | return module.substr(module.rfind('.') + 1); |
| 275 | } |
| 276 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 277 | Result<sysprop::Properties> ParseProps(const std::string& input_file_path) { |
| 278 | sysprop::Properties ret; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 279 | std::string file_contents; |
| 280 | |
| 281 | if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 282 | return ErrnoErrorf("Error reading file {}", input_file_path); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 285 | if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) { |
| 286 | return Errorf("Error parsing file {}", input_file_path); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 289 | if (auto res = ValidateProps(ret); !res.ok()) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 290 | return res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Inseob Kim | 900fbad | 2019-06-26 14:09:20 +0900 | [diff] [blame] | 293 | SetDefaultValues(&ret); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | Result<sysprop::SyspropLibraryApis> ParseApiFile( |
| 299 | const std::string& input_file_path) { |
| 300 | sysprop::SyspropLibraryApis ret; |
| 301 | std::string file_contents; |
| 302 | |
| 303 | if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) { |
| 304 | return ErrnoErrorf("Error reading file {}", input_file_path); |
| 305 | } |
| 306 | |
| 307 | if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) { |
| 308 | return Errorf("Error parsing file {}", input_file_path); |
| 309 | } |
| 310 | |
| 311 | std::unordered_set<std::string> modules; |
| 312 | |
| 313 | for (int i = 0; i < ret.props_size(); ++i) { |
| 314 | sysprop::Properties* props = ret.mutable_props(i); |
| 315 | |
| 316 | if (!modules.insert(props->module()).second) { |
| 317 | return Errorf("Error parsing file {}: duplicated module {}", |
| 318 | input_file_path, props->module()); |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 319 | } |
Inseob Kim | 900fbad | 2019-06-26 14:09:20 +0900 | [diff] [blame] | 320 | |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 321 | if (auto res = ValidateProps(*props); !res.ok()) { |
Inseob Kim | 900fbad | 2019-06-26 14:09:20 +0900 | [diff] [blame] | 322 | return res.error(); |
| 323 | } |
| 324 | |
| 325 | SetDefaultValues(props); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 326 | } |
| 327 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 328 | return ret; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 331 | std::string ToUpper(std::string str) { |
| 332 | for (char& ch : str) { |
| 333 | ch = toupper(ch); |
| 334 | } |
| 335 | return str; |
| 336 | } |
| 337 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 338 | std::string ApiNameToIdentifier(const std::string& name) { |
| 339 | static const std::regex kRegexAllowed{"-|\\."}; |
| 340 | return (isdigit(name[0]) ? "_" : "") + |
| 341 | std::regex_replace(name, kRegexAllowed, "_"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 342 | } |