OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <sstream> | 5 #include <sstream> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "tools/gn/commands.h" | 10 #include "tools/gn/commands.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 kPrecedenceAssign, | 73 kPrecedenceAssign, |
74 kPrecedenceOr, | 74 kPrecedenceOr, |
75 kPrecedenceAnd, | 75 kPrecedenceAnd, |
76 kPrecedenceCompare, | 76 kPrecedenceCompare, |
77 kPrecedenceAdd, | 77 kPrecedenceAdd, |
78 kPrecedenceUnary, | 78 kPrecedenceUnary, |
79 kPrecedenceSuffix, | 79 kPrecedenceSuffix, |
80 }; | 80 }; |
81 | 81 |
82 int CountLines(const std::string& str) { | 82 int CountLines(const std::string& str) { |
83 std::vector<std::string> lines; | 83 return static_cast<int>(base::SplitStringPiece( |
84 base::SplitStringDontTrim(str, '\n', &lines); | 84 str, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL).size()); |
85 return static_cast<int>(lines.size()); | |
86 } | 85 } |
87 | 86 |
88 class Printer { | 87 class Printer { |
89 public: | 88 public: |
90 Printer(); | 89 Printer(); |
91 ~Printer(); | 90 ~Printer(); |
92 | 91 |
93 void Block(const ParseNode* file); | 92 void Block(const ParseNode* file); |
94 | 93 |
95 std::string String() const { return output_; } | 94 std::string String() const { return output_; } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 if (block->comments()) { | 386 if (block->comments()) { |
388 for (const auto& c : block->comments()->after()) { | 387 for (const auto& c : block->comments()->after()) { |
389 TrimAndPrintToken(c); | 388 TrimAndPrintToken(c); |
390 Newline(); | 389 Newline(); |
391 } | 390 } |
392 } | 391 } |
393 } | 392 } |
394 | 393 |
395 int Printer::AssessPenalty(const std::string& output) { | 394 int Printer::AssessPenalty(const std::string& output) { |
396 int penalty = 0; | 395 int penalty = 0; |
397 std::vector<std::string> lines; | 396 std::vector<std::string> lines = base::SplitString( |
398 base::SplitStringDontTrim(output, '\n', &lines); | 397 output, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
399 penalty += static_cast<int>(lines.size() - 1) * GetPenaltyForLineBreak(); | 398 penalty += static_cast<int>(lines.size() - 1) * GetPenaltyForLineBreak(); |
400 for (const auto& line : lines) { | 399 for (const auto& line : lines) { |
401 if (line.size() > kMaximumWidth) | 400 if (line.size() > kMaximumWidth) |
402 penalty += static_cast<int>(line.size() - kMaximumWidth) * kPenaltyExcess; | 401 penalty += static_cast<int>(line.size() - kMaximumWidth) * kPenaltyExcess; |
403 } | 402 } |
404 return penalty; | 403 return penalty; |
405 } | 404 } |
406 | 405 |
407 bool Printer::ExceedsMaximumWidth(const std::string& output) { | 406 bool Printer::ExceedsMaximumWidth(const std::string& output) { |
408 std::vector<std::string> lines; | 407 for (const auto& line : base::SplitString( |
409 base::SplitStringDontTrim(output, '\n', &lines); | 408 output, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL)) { |
410 for (const auto& line : lines) { | |
411 if (line.size() > kMaximumWidth) | 409 if (line.size() > kMaximumWidth) |
412 return true; | 410 return true; |
413 } | 411 } |
414 return false; | 412 return false; |
415 } | 413 } |
416 | 414 |
417 void Printer::AddParen(int prec, int outer_prec, bool* parenthesized) { | 415 void Printer::AddParen(int prec, int outer_prec, bool* parenthesized) { |
418 if (prec < outer_prec) { | 416 if (prec < outer_prec) { |
419 Print("("); | 417 Print("("); |
420 *parenthesized = true; | 418 *parenthesized = true; |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 } | 1045 } |
1048 } else { | 1046 } else { |
1049 printf("%s", output_string.c_str()); | 1047 printf("%s", output_string.c_str()); |
1050 } | 1048 } |
1051 } | 1049 } |
1052 | 1050 |
1053 return 0; | 1051 return 0; |
1054 } | 1052 } |
1055 | 1053 |
1056 } // namespace commands | 1054 } // namespace commands |
OLD | NEW |