1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/core/graph/tensor_id.h" 17 18#include <string> 19 20#include "tensorflow/core/lib/core/stringpiece.h" 21 22namespace tensorflow { 23 24TensorId ParseTensorName(const string& name) { 25 return ParseTensorName(StringPiece(name.data(), name.size())); 26} 27 28TensorId ParseTensorName(StringPiece name) { 29 // Parse either a name, ^name, or name:digits. To do so, we go backwards from 30 // the end of the string, skipping over a run of digits. If we hit a ':' 31 // character, then we know we are in the 'name:digits' regime. Otherwise, we 32 // see if the name starts with '^', indicating a control edge. If we find 33 // neither ':' nor '^' characters, the output index is implicitly 0, and the 34 // whole name string forms the first part of the tensor name. 35 const char* base = name.data(); 36 const char* p = base + name.size() - 1; 37 unsigned int index = 0; 38 unsigned int mul = 1; 39 while (p > base && (*p >= '0' && *p <= '9')) { 40 index += ((*p - '0') * mul); 41 mul *= 10; 42 p--; 43 } 44 TensorId id; 45 if (p > base && *p == ':' && mul > 1) { 46 id.first = StringPiece(base, p - base); 47 id.second = index; 48 } else if (name.starts_with("^")) { 49 // Control edge 50 id.first = StringPiece(base + 1); 51 id.second = Graph::kControlSlot; 52 } else { 53 id.first = name; 54 id.second = 0; 55 } 56 return id; 57} 58 59} // namespace tensorflow 60