aboutsummaryrefslogtreecommitdiff
path: root/tensorflow/lite/testing/nnapi_tflite_zip_tests/tokenize.cc
blob: 95251dcd679c9957628e9ddf691b9e36ae289579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// NOTE: this is a Android version of the file with the same name in parent folder.
// The main difference is the removal of absl, re2 and tensorflow core dependencies.

#include "tokenize.h"
#include <istream>
#include <string>
#include "tensorflow/lite/string_tflite.h"

namespace tflite {
namespace testing {

void Tokenize(std::istream* input, TokenProcessor* processor) {
  enum State { kBuildQuotedToken, kBuildToken, kIdle };

  std::string current_token;
  State state = kIdle;
  auto start_token = [&](char c) {
    state = kBuildToken;
    current_token.clear();
    current_token = c;
  };
  auto issue_token = [&]() {
    state = kIdle;
    processor->ConsumeToken(&current_token);
    current_token.clear();
  };
  auto start_quoted_token = [&]() {
    state = kBuildQuotedToken;
    current_token.clear();
  };
  auto issue_quoted_token = [&]() {
    state = kIdle;
    processor->ConsumeToken(&current_token);
    current_token.clear();
  };
  auto issue_delim = [&](char d) {
    current_token = string(1, d);
    processor->ConsumeToken(&current_token);
    current_token.clear();
  };
  auto is_delim = [](char c) { return c == '{' || c == '}' || c == ':'; };
  auto is_quote = [](char c) { return c == '"'; };

  for (auto it = std::istreambuf_iterator<char>(*input);
       it != std::istreambuf_iterator<char>(); ++it) {
    switch (state) {
      case kIdle:
        if (is_delim(*it)) {
          issue_delim(*it);
        } else if (is_quote(*it)) {
          start_quoted_token();
        } else if (!isspace(*it)) {
          start_token(*it);
        }
        break;
      case kBuildToken:
        if (is_delim(*it)) {
          issue_token();
          issue_delim(*it);
        } else if (is_quote(*it)) {
          issue_token();
          start_quoted_token();
        } else if (isspace(*it)) {
          issue_token();
        } else {
          current_token += *it;
        }
        break;
      case kBuildQuotedToken:
        if (is_quote(*it)) {
          issue_quoted_token();
        } else {
          current_token += *it;
        }
        break;
    }
  }
  if (state != kIdle) {
    issue_token();
  }
}

}  // namespace testing
}  // namespace tflite