Home | History | Annotate | Download | only in hidl
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      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 "DocComment.h"
     18 
     19 #include <hidl-util/StringHelper.h>
     20 
     21 #include <cctype>
     22 #include <sstream>
     23 
     24 namespace android {
     25 
     26 DocComment::DocComment(const std::string& comment) {
     27     std::vector<std::string> lines;
     28     StringHelper::SplitString(comment, '\n', &lines);
     29 
     30     bool foundFirstLine = false;
     31 
     32     std::ostringstream is;
     33     for (size_t l = 0; l < lines.size(); l++) {
     34         const std::string& line = lines[l];
     35 
     36         // Delete prefixes like "    * ", "   *", or "    ".
     37         size_t idx = 0;
     38         for (; idx < line.size() && isspace(line[idx]); idx++)
     39             ;
     40         if (idx < line.size() && line[idx] == '*') idx++;
     41         if (idx < line.size() && line[idx] == ' ') idx++;
     42 
     43         if (idx < line.size()) {
     44             foundFirstLine = true;
     45         }
     46 
     47         if (!foundFirstLine) continue;
     48 
     49         is << line.substr(idx);
     50 
     51         if (l + 1 < lines.size()) {
     52             is << "\n";
     53         }
     54     }
     55 
     56     mComment = is.str();
     57 }
     58 
     59 void DocComment::merge(const DocComment* comment) {
     60     mComment = mComment + "\n\n" + comment->mComment;
     61 }
     62 
     63 void DocComment::emit(Formatter& out) const {
     64     out << "/**\n";
     65     out.setLinePrefix(" * ");
     66     out << mComment;
     67     out.unsetLinePrefix();
     68     out << " */\n";
     69 }
     70 
     71 }  // namespace android
     72