Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
char.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALPHABETS_CHAR_HH
2 # define VCSN_ALPHABETS_CHAR_HH
3 
4 # include <cassert>
5 # include <string>
6 # include <iostream>
7 
8 # include <vcsn/misc/escape.hh>
9 # include <vcsn/misc/raise.hh>
10 
11 namespace vcsn
12 {
14  {
15  public:
16  using letter_t = char;
17  using word_t = std::string;
18 
19  static std::string sname()
20  {
21  return "char_letters";
22  }
23 
24  virtual std::string vname(bool = true) const
25  {
26  return sname();
27  }
28 
29  word_t
30  to_word(const letter_t l) const
31  {
32  return {l};
33  }
34 
35  const word_t&
36  to_word(const word_t& l) const
37  {
38  return l;
39  }
40 
41  word_t
42  concat(const letter_t l, const letter_t r) const
43  {
44  if (l == one_letter())
45  {
46  if (r == one_letter())
47  return {};
48  else
49  return {l};
50  }
51  else if (r == one_letter())
52  return {l};
53  else
54  return {l, r};
55  }
56 
57  word_t
58  concat(const word_t& l, const letter_t r) const
59  {
60  return r == one_letter() ? l : l + r;
61  }
62 
63  word_t
64  concat(const letter_t l, const word_t& r) const
65  {
66  return l == one_letter() ? r : l + r;
67  }
68 
69  word_t
70  concat(const word_t& l, const word_t& r) const
71  {
72  return l + r;
73  }
74 
76  word_t delimit(const word_t& w) const
77  {
78  return concat(concat(special_letter(), w), special_letter());
79  }
80 
82  word_t undelimit(const word_t& w) const
83  {
84  size_t s = w.size();
85  assert(2 <= s);
86  assert(w[0] == special_letter());
87  assert(w[s-1] == special_letter());
88  return w.substr(1, s-2);
89  }
90 
91  static word_t
93  {
94  return {};
95  }
96 
97  static bool
99  {
100  return w.empty();
101  }
102 
103  word_t
104  transpose(const word_t& w) const
105  {
106  // C++11 lacks std::rbegin/rend...
107  return {w.rbegin(), w.rend()};
108  }
109 
110  letter_t
112  {
113  return l;
114  }
115 
116  bool
117  equals(const letter_t& l1, const letter_t& l2) const
118  {
119  return l1 == l2;
120  }
121 
122  bool
123  equals(const word_t& w1, const word_t& w2) const
124  {
125  return w1 == w2;
126  }
127 
128  bool
129  is_letter(const letter_t&) const
130  {
131  return true;
132  }
133 
134  bool
135  is_letter(const word_t& w) const
136  {
137  return w.size() == 1;
138  }
139 
142  static constexpr letter_t one_letter() { return 0; }
143 
144  private:
149  static constexpr letter_t special_letter() { return 127; }
150 
151  public:
154  static char get_char(std::istream& i)
155  {
156  int res = i.get();
157  if (res == '\\')
158  res = i.get();
159  require(res != EOF, "invalid label: unexpected end-of-file");
160  return res;
161  }
162 
163 
167  static letter_t get_letter(std::istream& i)
168  {
169  return get_char(i);
170  }
171 
172  std::ostream&
173  print(const letter_t& l, std::ostream& o) const
174  {
175  if (l != one_letter() && l != special_letter())
176  o << l;
177  return o;
178  }
179 
180  std::ostream&
181  print(const word_t& w, std::ostream& o) const
182  {
183  size_t s = w.size();
184 
185  if (s == 0
186  || (s == 1 && w[0] == one_letter()))
187  o << "\\e";
188 
189  // If the string starts or ends with the special letter, skip
190  // it. If the resulting string is empty, format it this way.
191  // (We DON'T want to format it as "\\e".)
192  else if (w[0] == special_letter())
193  o << ((s == 1) ? "" : str_escape(w.substr(1)));
194  else if (s > 1 && w[s - 1] == special_letter())
195  o << str_escape(w.substr(0, s - 1));
196  else
197  o << str_escape(w);
198  return o;
199  }
200 
203  template <typename T = letter_t>
204  static T special();
205  };
206 
207  template <>
208  inline
210  char_letters::special<char_letters::letter_t>()
211  {
212  return special_letter();
213  }
214 
215  template <>
216  inline
218  char_letters::special<char_letters::word_t>()
219  {
220  return {special_letter()};
221  }
222 
223 }
224 
225 #endif // !VCSN_ALPHABETS_CHAR_HH
bool is_letter(const letter_t &) const
Definition: char.hh:129
word_t undelimit(const word_t &w) const
Remove first and last characters, that must be "special".
Definition: char.hh:82
const word_t & to_word(const word_t &l) const
Definition: char.hh:36
virtual std::string vname(bool=true) const
Definition: char.hh:24
static word_t empty_word()
Definition: char.hh:92
word_t delimit(const word_t &w) const
Add the special character first and last.
Definition: char.hh:76
word_t concat(const word_t &l, const letter_t r) const
Definition: char.hh:58
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
Definition: escape.cc:43
word_t concat(const word_t &l, const word_t &r) const
Definition: char.hh:70
bool equals(const word_t &w1, const word_t &w2) const
Definition: char.hh:123
word_t transpose(const word_t &w) const
Definition: char.hh:104
static letter_t get_letter(std::istream &i)
Read one letter from i.
Definition: char.hh:167
static T special()
Special character, used to label transitions from pre() and to post().
letter_t transpose(letter_t l) const
Definition: char.hh:111
static constexpr letter_t special_letter()
The reserved letter used to forge the labels for initial and final transitions.
Definition: char.hh:149
static std::string sname()
Definition: char.hh:19
static constexpr letter_t one_letter()
The reserved letter used to forge the "one" label (the unit, the identity).
Definition: char.hh:142
word_t to_word(const letter_t l) const
Definition: char.hh:30
static char get_char(std::istream &i)
Read a single char, with possible -escape support.
Definition: char.hh:154
static bool is_empty_word(const word_t &w)
Definition: char.hh:98
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::ostream & print(const letter_t &l, std::ostream &o) const
Definition: char.hh:173
word_t concat(const letter_t l, const word_t &r) const
Definition: char.hh:64
std::string word_t
Definition: char.hh:17
bool equals(const letter_t &l1, const letter_t &l2) const
Definition: char.hh:117
bool is_letter(const word_t &w) const
Definition: char.hh:135
word_t concat(const letter_t l, const letter_t r) const
Definition: char.hh:42
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39
std::ostream & print(const word_t &w, std::ostream &o) const
Definition: char.hh:181