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 
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 
42 {
43  std::string const printed_info = allowed_info(info) ? info : "";
44  return "#SRC|" + printed_info + "|";
45 }
46 
47 //==================================================================
48 
50 detail::printed_suffix(std::string const& key, std::size_t const sz)
51 {
53 
54  if (sz == size_t_max)
55  return result;
56 
57  std::smatch m;
58  if (std::regex_match(key, m, reSequenceElement) &&
59  std::stoul(m[1]) != sz - 1) {
60  result = ",";
61  }
62  return result;
63 }
64 
65 //==================================================================
66 // table
67 
70 {
72  if (!is_sequence_element(key))
73  result = key + ": " + result;
74  return result;
75 }
76 
77 //==================================================================
78 // sequence
79 
82 {
84  if (!is_sequence_element(key))
85  result = key + ": " + result;
86  return result;
87 }
88 
89 //==================================================================
90 // atom
91 
94 {
95  return is_sequence_element(key) ? "" : key + ": ";
96 }
97 
99 atom::value(std::any const& a)
100 {
101  using ps_atom_t = std::string;
102  std::string const str = std::any_cast<ps_atom_t>(a);
103  return str == std::string(9, '\0') ? "@nil" : str;
104 }
105 
106 //==================================================================
107 // miscellany
108 
109 std::size_t
111 {
112  if (!is_sequence_element(name)) {
113  throw exception{error::other,
114  "An error occurred while converting a name to a sequence "
115  "element index.\n"}
116  << "The name '" << name << "' does not correspond to a sequence element.";
117  }
118  auto const b = name.find_last_of("[");
119  assert(b != std::string::npos);
120  auto const e = name.find_last_of("]");
121  assert(e == name.length() - 1);
122  auto const start = b + 1;
123  assert(start < e);
124  return std::stoull(name.substr(start, e - start));
125 }
std::string printed_suffix(std::string const &key, std::size_t const sz)
static QCString name
Definition: declinfo.cpp:673
std::string print_annotated_info(std::string const &curr_info, std::string const &cached_info)
bool is_sequence_element(std::string const &key)
static QCString result
std::string opening_brace()
std::string printed_prefix(std::string const &key)
std::string string
Definition: nybbler.cc:12
std::string print_prefix_annotated_info(std::string const &curr_info)
const double e
std::string printed_prefix(std::string const &key)
def key(type, name=None)
Definition: graph.py:13
const double a
std::size_t index_for_sequence_element(std::string const &name)
std::string value(std::any const &)
static int max(int a, int b)
std::string ps_atom_t
Definition: coding.h:44
static bool * b
Definition: config.cpp:1043
std::string printed_prefix(std::string const &key)
static QCString str
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33