kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/proto/offset_util.cc (about)

     1  /*
     2   * Copyright 2020 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "offset_util.h"
    18  
    19  #include "absl/log/log.h"
    20  
    21  namespace kythe {
    22  namespace lang_proto {
    23  
    24  int ByteOffsetOfTabularColumn(absl::string_view line_text, int column_number) {
    25    int computed_column = 0;
    26    int offset = 0;
    27    while (computed_column < column_number && offset < line_text.size()) {
    28      if (line_text[offset] == '\t') {
    29        // In proto land, tabs go to the next multiple of 8.  There are a million
    30        // ways of computing this.  This one will do.
    31        computed_column = (computed_column + 8) - (computed_column % 8);
    32      } else {
    33        ++computed_column;
    34      }
    35      ++offset;
    36    }
    37    if (computed_column != column_number) {
    38      LOG(ERROR) << "Error computing byte offset: expected " << column_number
    39                 << " columns but counted up to " << computed_column
    40                 << " in line \"" << line_text << "\"";
    41      return -1;
    42    }
    43    return offset;
    44  }
    45  
    46  }  // namespace lang_proto
    47  }  // namespace kythe