Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 19 | #include <android-base/strings.h> |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 20 | #include <hidl-util/StringHelper.h> |
| 21 | |
| 22 | #include <cctype> |
| 23 | #include <sstream> |
| 24 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 25 | #include <iostream> |
| 26 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | DocComment::DocComment(const std::string& comment) { |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 30 | std::vector<std::string> lines = base::Split(base::Trim(comment), "\n"); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 31 | |
| 32 | bool foundFirstLine = false; |
| 33 | |
| 34 | std::ostringstream is; |
| 35 | for (size_t l = 0; l < lines.size(); l++) { |
| 36 | const std::string& line = lines[l]; |
| 37 | |
| 38 | // Delete prefixes like " * ", " *", or " ". |
| 39 | size_t idx = 0; |
| 40 | for (; idx < line.size() && isspace(line[idx]); idx++) |
| 41 | ; |
| 42 | if (idx < line.size() && line[idx] == '*') idx++; |
| 43 | if (idx < line.size() && line[idx] == ' ') idx++; |
| 44 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 45 | bool isEmptyLine = idx == line.size(); |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 46 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 47 | foundFirstLine = foundFirstLine || !isEmptyLine; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 48 | if (!foundFirstLine) continue; |
| 49 | |
Steven Moreland | 7645fbd | 2019-03-12 18:49:28 -0700 | [diff] [blame] | 50 | is << line.substr(idx) << "\n"; |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | mComment = is.str(); |
| 54 | } |
| 55 | |
| 56 | void DocComment::merge(const DocComment* comment) { |
| 57 | mComment = mComment + "\n\n" + comment->mComment; |
| 58 | } |
| 59 | |
| 60 | void DocComment::emit(Formatter& out) const { |
| 61 | out << "/**\n"; |
| 62 | out.setLinePrefix(" * "); |
| 63 | out << mComment; |
| 64 | out.unsetLinePrefix(); |
| 65 | out << " */\n"; |
| 66 | } |
| 67 | |
| 68 | } // namespace android |