Home | History | Annotate | Download | only in strings
      1 /* Copyright 2016 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 
     16 #include "tensorflow/core/lib/strings/scanner.h"
     17 
     18 namespace tensorflow {
     19 namespace strings {
     20 
     21 void Scanner::ScanUntilImpl(char end_ch, bool escaped) {
     22   for (;;) {
     23     if (cur_.empty()) {
     24       Error();
     25       return;
     26     }
     27     const char ch = cur_[0];
     28     if (ch == end_ch) {
     29       return;
     30     }
     31 
     32     cur_.remove_prefix(1);
     33     if (escaped && ch == '\\') {
     34       // Escape character, skip next character.
     35       if (cur_.empty()) {
     36         Error();
     37         return;
     38       }
     39       cur_.remove_prefix(1);
     40     }
     41   }
     42 }
     43 
     44 bool Scanner::GetResult(StringPiece* remaining, StringPiece* capture) {
     45   if (error_) {
     46     return false;
     47   }
     48   if (remaining != nullptr) {
     49     *remaining = cur_;
     50   }
     51   if (capture != nullptr) {
     52     const char* end = capture_end_ == nullptr ? cur_.data() : capture_end_;
     53     *capture = StringPiece(capture_start_, end - capture_start_);
     54   }
     55   return true;
     56 }
     57 
     58 }  // namespace strings
     59 }  // namespace tensorflow
     60