python: implement formula.__format__

Fixes #105.

* src/bin/common_trans.cc (quote_shell_string): Move ...
* src/misc/escape.cc, src/misc/escape.hh (quote_shell_string):
... here.
* wrap/python/spot_impl.i: Wrap escape.hh.
* wrap/python/spot.py: Implement formula.__format__.
* wrap/python/tests/ltlsimple.py: Test it.
* NEWS, doc/org/tut01.org, wrap/python/tests/formulas.ipynb: Document
it.
This commit is contained in:
Alexandre Duret-Lutz 2015-10-03 11:20:02 +02:00
parent 20bb171904
commit 5bfd0267e7
9 changed files with 364 additions and 67 deletions

View file

@ -27,6 +27,7 @@
#include <functional>
#include <cctype>
#include <locale>
#include <cstring>
#include "escape.hh"
namespace spot
@ -143,4 +144,35 @@ namespace spot
std::find_if(str.begin(), str.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
std::ostream&
quote_shell_string(std::ostream& os, const char* str)
{
// Single quotes are best, unless the string to quote contains one.
if (!strchr(str, '\''))
{
os << '\'' << str << '\'';
}
else
{
// In double quotes we have to escape $ ` " or \.
os << '"';
while (*str)
switch (*str)
{
case '$':
case '`':
case '"':
case '\\':
os << '\\';
// fall through
default:
os << *str++;
break;
}
os << '"';
}
return os;
}
}

View file

@ -65,5 +65,13 @@ namespace spot
/// \brief Remove spaces at the front and back of \a str.
SPOT_API void
trim(std::string& str);
/// \brief Output \a str between simple quote or double quotes
///
/// Simple quotes are preferred unless \a str contains some simple
/// quotes. In that case we use double quotes and escape anything
/// that needs to be escaped.
SPOT_API std::ostream&
quote_shell_string(std::ostream& os, const char* str);
/// @}
}