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

     1  /*
     2   * Copyright 2018 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 "kythe/cxx/indexer/proto/comments.h"
    18  
    19  #include <vector>
    20  
    21  #include "absl/strings/str_join.h"
    22  #include "absl/strings/str_split.h"
    23  #include "absl/strings/strip.h"
    24  
    25  namespace kythe {
    26  
    27  std::string StripCommentMarkers(const std::string& source) {
    28    absl::string_view stripped = absl::StripAsciiWhitespace(source);
    29  
    30    // Handle block comments, of the form "/* ... */".
    31    if (absl::ConsumePrefix(&stripped, "/*")) {
    32      absl::ConsumeSuffix(&stripped, "*/");
    33      std::vector<absl::string_view> lines = absl::StrSplit(stripped, '\n');
    34      for (auto& line : lines) {
    35        line = absl::StripAsciiWhitespace(line);
    36        if (!absl::ConsumePrefix(&line, "* ")) {
    37          absl::ConsumePrefix(&line, "*");  // fallback
    38        }
    39      }
    40      return absl::StrJoin(lines, "\n");
    41    }
    42  
    43    // Handle line-ending comments, of the form "// ...".
    44    std::vector<absl::string_view> lines = absl::StrSplit(stripped, '\n');
    45    for (auto& line : lines) {
    46      line = absl::StripAsciiWhitespace(line);
    47      if (!absl::ConsumePrefix(&line, "// ")) {
    48        absl::ConsumePrefix(&line, "//");  // fallback
    49      }
    50    }
    51    return absl::StrJoin(lines, "\n");
    52  }
    53  
    54  }  // namespace kythe