Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / pp / src / LinearDocument.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 "pp/LinearDocument.h"
18
19 #include <stdexcept>
20
21 namespace pp
22 {
23
24 void LinearDocument::indent(void) { _indent.increase(); }
25 void LinearDocument::unindent(void) { _indent.decrease(); }
26
27 void LinearDocument::append(void)
28 {
29   // NOTE Do NOT indent empty lines
30   _lines.emplace_back("");
31 }
32
33 void LinearDocument::append(const std::string &line)
34 {
35   if (line.empty())
36   {
37     append();
38     return;
39   }
40
41   // Append indentation space(s), and insert the update string to lines
42   _lines.emplace_back(_indent.build(line));
43 }
44
45 void LinearDocument::append(const LinearDocument &doc)
46 {
47   for (uint32_t n = 0; n < doc.lines(); ++n)
48   {
49     // NOTE Do NOT update _lines here and use append method
50     append(doc.line(n));
51   }
52 }
53
54 const std::string &LinearDocument::line(uint32_t n) const
55 {
56   switch (_direction)
57   {
58     case Direction::Forward:
59     {
60       return _lines.at(n);
61     }
62     case Direction::Reverse:
63     {
64       return _lines.at(lines() - n - 1);
65     }
66     default:
67       throw std::runtime_error{"Not supported Direction"};
68   }
69 }
70
71 } // namespace pp