diff options
author | 2016-04-14 19:15:20 -0800 | |
---|---|---|
committer | 2016-04-14 20:22:35 -0700 | |
commit | df15baa9b10a0b2d194181dff7ee14bff70d9b8f (patch) | |
tree | a66e1fcb309a7404a2749140d8bac991a6524ac0 /tensorflow/core/lib/strings/proto_text_util.cc | |
parent | 104fe2822b419c4154d11c401ffd4a3a6e8f24c6 (diff) |
Add tools/proto_text for generating ProtoDebugString,
ProtoShortDebugString, and ProtoParseFromString methods from protos. This will
allow changing code used on mobile to use the proto LITE_RUNTIME, to reduce
code size.
This change is only for the tool itself. A future change will add a better
genrule and use it the generated code in tensorflow.
Change: 119919087
Diffstat (limited to 'tensorflow/core/lib/strings/proto_text_util.cc')
-rw-r--r-- | tensorflow/core/lib/strings/proto_text_util.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tensorflow/core/lib/strings/proto_text_util.cc b/tensorflow/core/lib/strings/proto_text_util.cc new file mode 100644 index 0000000000..3df476e09f --- /dev/null +++ b/tensorflow/core/lib/strings/proto_text_util.cc @@ -0,0 +1,58 @@ +/* Copyright 2016 Google Inc. 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. +==============================================================================*/ + +#include "tensorflow/core/lib/strings/proto_text_util.h" + +namespace tensorflow { +namespace strings { + +bool ProtoParseBoolFromScanner(Scanner* scanner, bool* value) { + StringPiece bool_str; + if (!scanner->RestartCapture() + .Many(Scanner::LETTER_DIGIT) + .GetResult(nullptr, &bool_str)) { + return false; + } + ProtoSpaceAndComments(scanner); + if (bool_str == "false" || bool_str == "False" || bool_str == "0") { + *value = false; + return true; + } else if (bool_str == "true" || bool_str == "True" || bool_str == "1") { + *value = true; + return true; + } else { + return false; + } +} + +bool ProtoParseStringLiteralFromScanner(Scanner* scanner, string* value) { + const char quote = scanner->Peek(); + if (quote != '\'' && quote != '"') return false; + + StringPiece value_sp; + if (!scanner->One(Scanner::ALL) + .RestartCapture() + .ScanEscapedUntil(quote) + .StopCapture() + .One(Scanner::ALL) + .GetResult(nullptr, &value_sp)) { + return false; + } + ProtoSpaceAndComments(scanner); + return str_util::CUnescape(value_sp, value, nullptr /* error */); +} + +} // namespace strings +} // namespace tensorflow |