LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
printing_helpers.cc
Go to the documentation of this file.
2 #include "fhiclcpp/exception.h"
3 
4 #include <cassert>
5 #include <limits>
6 #include <regex>
7 
8 using namespace fhicl;
9 using namespace fhicl::detail;
10 
11 //==================================================================
12 
13 namespace {
14  std::string const UNKNOWN_SRC{"-:1"};
15  std::regex const reSequenceElement{R"(.*\[(\d+)\])"};
16 
17  inline bool
18  allowed_info(std::string const& src_info)
19  {
20  return !src_info.empty() && src_info != UNKNOWN_SRC;
21  }
22 
23  constexpr auto size_t_max = std::numeric_limits<std::size_t>::max();
24 }
25 
26 //==================================================================
27 
28 std::string
29 detail::print_annotated_info(std::string const& curr_info,
30  std::string const& cached_info)
31 {
32  if (!allowed_info(curr_info))
33  return "";
34  std::string const tag = " # ";
35  std::string const printed_info =
36  curr_info == cached_info ? "\"\"" : curr_info;
37  return tag + printed_info;
38 }
39 
40 std::string
41 detail::print_prefix_annotated_info(std::string const& info)
42 {
43  std::string const printed_info = allowed_info(info) ? info : "";
44  return "#SRC|" + printed_info + "|";
45 }
46 
47 //==================================================================
48 
49 std::string
50 detail::printed_suffix(std::string const& key, std::size_t const sz)
51 {
52  using namespace std::string_literals;
53  std::string result;
54 
55  if (sz == size_t_max)
56  return result;
57 
58  std::smatch m;
59  if (std::regex_match(key, m, reSequenceElement) &&
60  std::stoul(m[1]) != sz - 1) {
61  result = ","s;
62  }
63  return result;
64 }
65 
66 //==================================================================
67 // table
68 
69 std::string
70 table::printed_prefix(std::string const& key)
71 {
72  std::string result{opening_brace()};
73  if (!is_sequence_element(key))
74  result = key + ": " + result;
75  return result;
76 }
77 
78 //==================================================================
79 // sequence
80 
81 std::string
82 sequence::printed_prefix(std::string const& key)
83 {
84  std::string result{opening_brace()};
85  if (!is_sequence_element(key))
86  result = key + ": " + result;
87  return result;
88 }
89 
90 //==================================================================
91 // atom
92 
93 std::string
94 atom::printed_prefix(std::string const& key)
95 {
96  return is_sequence_element(key) ? "" : key + ": ";
97 }
98 
99 std::string
100 atom::value(std::any const& a)
101 {
102  using ps_atom_t = std::string;
103  std::string const str = std::any_cast<ps_atom_t>(a);
104  return str == std::string(9, '\0') ? "@nil" : str;
105 }
106 
107 //==================================================================
108 // miscellany
109 
110 std::size_t
111 detail::index_for_sequence_element(std::string const& name)
112 {
113  if (!is_sequence_element(name)) {
114  throw exception{error::other,
115  "An error occurred while converting a name to a sequence "
116  "element index.\n"}
117  << "The name '" << name << "' does not correspond to a sequence element.";
118  }
119  auto const b = name.find_last_of("[");
120  assert(b != std::string::npos);
121  auto const e = name.find_last_of("]");
122  assert(e == name.length() - 1);
123  auto const start = b + 1;
124  assert(start < e);
125  return std::stoull(name.substr(start, e - start));
126 }
std::string printed_suffix(std::string const &key, std::size_t const sz)
std::string print_annotated_info(std::string const &curr_info, std::string const &cached_info)
bool is_sequence_element(std::string const &key)
std::string opening_brace()
std::string printed_prefix(std::string const &key)
std::string print_prefix_annotated_info(std::string const &curr_info)
std::string printed_prefix(std::string const &key)
parameter set interface
std::size_t index_for_sequence_element(std::string const &name)
std::string value(std::any const &)
std::string ps_atom_t
Definition: coding.h:44
Float_t e
Definition: plot.C:35
std::string printed_prefix(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33