1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/kernels/acceleration_test_util_internal.h"
16
17 #include <ctype.h>
18
19 #include <algorithm>
20 #include <functional>
21 #include <iterator>
22 #include <sstream>
23 #include <string>
24
25 namespace tflite {
26
ReadAccelerationConfig(const char * config,const std::function<void (std::string,std::string,bool)> & consumer)27 void ReadAccelerationConfig(
28 const char* config,
29 const std::function<void(std::string, std::string, bool)>& consumer) {
30 if (config) {
31 std::istringstream istream{config};
32
33 std::string curr_config_line;
34 while (std::getline(istream, curr_config_line)) {
35 // trim whitespaces
36 curr_config_line.erase(
37 curr_config_line.begin(),
38 std::find_if_not(curr_config_line.begin(), curr_config_line.end(),
39 [](int ch) { return std::isspace(ch); }));
40 // skipping comments and empty lines.
41 if (curr_config_line.empty() || curr_config_line.at(0) == '#') {
42 continue;
43 }
44
45 // split in test id regexp and rest of the config.
46 auto first_sep_pos =
47 std::find(curr_config_line.begin(), curr_config_line.end(), ',');
48
49 bool is_denylist = false;
50 std::string key = curr_config_line;
51 std::string value{};
52 if (first_sep_pos != curr_config_line.end()) {
53 key = std::string(curr_config_line.begin(), first_sep_pos);
54 value = std::string(first_sep_pos + 1, curr_config_line.end());
55 }
56
57 // Regexps starting with '-'' are denylist ones.
58 if (key[0] == '-') {
59 key = key.substr(1);
60 is_denylist = true;
61 }
62
63 consumer(key, value, is_denylist);
64 }
65 }
66 }
67
68 } // namespace tflite
69