OR-Tools  9.6
message_callback_data.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <cstddef>
17 #include <optional>
18 #include <string>
19 #include <string_view>
20 #include <utility>
21 #include <vector>
22 
23 namespace operations_research {
24 namespace math_opt {
25 
26 std::vector<std::string> MessageCallbackData::Parse(
27  const std::string_view message) {
28  std::vector<std::string> strings;
29 
30  // Iterate on all complete lines (lines ending with a '\n').
31  std::string_view remainder = message;
32  for (std::size_t end = 0; end = remainder.find('\n'), end != remainder.npos;
33  remainder = remainder.substr(end + 1)) {
34  const auto line = remainder.substr(0, end);
35  if (!unfinished_line_.empty()) {
36  std::string new_message = std::move(unfinished_line_);
37  unfinished_line_.clear();
38  new_message += line;
39  strings.push_back(std::move(new_message));
40  } else {
41  strings.emplace_back(line);
42  }
43  }
44 
45  // At the end of the loop, the remainder may contains the last unfinished
46  // line. This could be the first line too if the entire message does not
47  // contain '\n'.
48  unfinished_line_ += remainder;
49 
50  return strings;
51 }
52 
53 std::vector<std::string> MessageCallbackData::Flush() {
54  if (unfinished_line_.empty()) {
55  return {};
56  }
57 
58  std::vector<std::string> strings = {std::move(unfinished_line_)};
59  unfinished_line_.clear();
60  return strings;
61 }
62 
63 } // namespace math_opt
64 } // namespace operations_research
std::vector< std::string > Parse(std::string_view message)
Collection of objects used to extend the Constraint Solver library.
Definition: case.cc:32
int line
Definition: parse_proto.cc:31
std::optional< int64_t > end
std::string message
Definition: trace.cc:399