Awali
Another Weighted Automata library
escape.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_ESCAPE_HH
18 # define AWALI_MISC_ESCAPE_HH
19 
20 # include <iosfwd>
21 # include <string>
22 #include <cctype>
23 #include <iomanip>
24 #include <iostream>
25 #include <sstream>
26 
27 namespace awali { namespace sttc {
28 
29  std::ostream&
30  str_escape(std::ostream& os, const int c)
31  {
32  std::ios_base::fmtflags flags = os.flags(std::ios_base::hex);
33  char fill = os.fill('0');
34  switch (c)
35  {
36  case -1: os << "<end-of-file>"; break;
37  case '\\': os << "\\\\"; break;
38  case '"': os << "\\\""; break;
39  case '\n': os << "\\n"; break;
40  default:
41  if (0 <= c && c <= 0177 && std::isprint(c))
42  os << char(c);
43  else
44  os << "\\0x" << std::setw(3) << c;
45  break;
46  }
47  os.fill(fill);
48  os.flags(flags);
49  return os;
50  }
51 
52  std::string
53  str_escape(const int c)
54  {
55  std::ostringstream o;
56  str_escape(o, c);
57  return o.str();
58  }
59 
60  std::ostream&
61  str_escape(std::ostream& os, const std::string& str)
62  {
63  for (auto c: str)
64  str_escape(os, c);
65  return os;
66  }
67 
68  std::string
69  str_escape(const std::string& s)
70  {
71  std::ostringstream o;
72  str_escape(o, s);
73  return o.str();
74  }
75 
76 }}//end of ns awali::stc
77 
78 #endif // !AWALI_MISC_ESCAPE_HH
The semiring of complex numbers.
Definition: c.hh:44
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
Main namespace of Awali.
Definition: ato.hh:22