Euphoria
vectortostring.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "base/stringbuilder.h"
4 
5 
6 namespace eu::tests
7 {
8  template
9  <
10  typename T,
11  typename TConverter
12  >
13  std::string
15  (
16  const std::vector<T>& v,
17  bool one_line,
18  TConverter converter
19  )
20  {
21  auto ss = StringBuilder{};
22  bool first = true;
23 
24  const std::string_view newline = one_line ? "" : "\n";
25 
26  ss.add_view(newline).add_char('{').add_view(newline);
27  int index = 0;
28  for(const auto& s: v)
29  {
30  if(first) { first = false; }
31  else { ss.add_view(", ").add_view(newline); }
32  if (one_line == false) { ss.add_string(fmt::to_string(index)); }
33  ss.add_char('\'').add_string(converter(s)).add_char('\'');
34  index += 1;
35  }
36  if(!v.empty()) { ss.add_view(newline); }
37  ss.add_char('}').add_view(newline);
38 
39  return ss.to_string();
40  }
41 
42 
43  template
44  <
45  typename T,
46  typename TConverter
47  >
48  std::pair<std::string, bool>
49  from_vector_to_string_ex(const std::vector<T>& v, TConverter converter)
50  {
51  const auto oneline = from_vector_to_string_impl(v, true, converter);
52  if( oneline.size() <20)
53  {
54  return std::make_pair(oneline, true);
55  }
56  else
57  {
58  return std::make_pair
59  (
60  from_vector_to_string_impl(v, false, converter),
61  false
62  );
63  }
64  }
65 
66  template
67  <
68  typename T,
69  typename TConverter
70  >
71  std::string
72  from_vector_to_string(const std::vector<T>& v, TConverter converter)
73  {
74  return from_vector_to_string_ex(v, converter).first;
75  }
76 }
std::pair< std::string, bool > from_vector_to_string_ex(const std::vector< T > &v, TConverter converter)
std::string from_vector_to_string(const std::vector< T > &v, TConverter converter)
std::string from_vector_to_string_impl(const std::vector< T > &v, bool one_line, TConverter converter)
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
std::string to_string()
Complete the builder and return the resulting string.
StringBuilder & add_view(const std::string_view &str)
StringBuilder & add_string(const std::string &str)
StringBuilder & add_char(char c)