blob: eba3fc0825d05906e5953fec11db6a3544ebd6b9 [file] [log] [blame]
Steven Moreland49bad8d2018-05-17 15:45:26 -07001/*
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 Moreland7645fbd2019-03-12 18:49:28 -070019#include <android-base/strings.h>
Steven Moreland49bad8d2018-05-17 15:45:26 -070020#include <hidl-util/StringHelper.h>
21
22#include <cctype>
23#include <sstream>
24
Steven Moreland7645fbd2019-03-12 18:49:28 -070025#include <iostream>
26
Steven Moreland49bad8d2018-05-17 15:45:26 -070027namespace android {
28
29DocComment::DocComment(const std::string& comment) {
Steven Moreland7645fbd2019-03-12 18:49:28 -070030 std::vector<std::string> lines = base::Split(base::Trim(comment), "\n");
Steven Moreland49bad8d2018-05-17 15:45:26 -070031
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 Moreland7645fbd2019-03-12 18:49:28 -070045 bool isEmptyLine = idx == line.size();
Steven Moreland49bad8d2018-05-17 15:45:26 -070046
Steven Moreland7645fbd2019-03-12 18:49:28 -070047 foundFirstLine = foundFirstLine || !isEmptyLine;
Steven Moreland49bad8d2018-05-17 15:45:26 -070048 if (!foundFirstLine) continue;
49
Steven Moreland7645fbd2019-03-12 18:49:28 -070050 is << line.substr(idx) << "\n";
Steven Moreland49bad8d2018-05-17 15:45:26 -070051 }
52
53 mComment = is.str();
54}
55
56void DocComment::merge(const DocComment* comment) {
57 mComment = mComment + "\n\n" + comment->mComment;
58}
59
60void 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