1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Andres Noetzli, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Quotes a string if necessary for smt2. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "util/smt2_quote_string.h" |
17 |
|
|
18 |
|
#include <sstream> |
19 |
|
#include <string> |
20 |
|
|
21 |
|
namespace cvc5 { |
22 |
|
|
23 |
|
/** |
24 |
|
* SMT-LIB 2 quoting for symbols |
25 |
|
*/ |
26 |
2590672 |
std::string quoteSymbol(const std::string& s){ |
27 |
|
// this is the set of SMT-LIBv2 permitted characters in "simple" (non-quoted) |
28 |
|
// symbols |
29 |
5181344 |
if (s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
30 |
|
"0123456789~!@$%^&*_-+=<>.?/") |
31 |
|
!= std::string::npos |
32 |
2590672 |
|| s.empty() || (s[0] >= '0' && s[0] <= '9')) |
33 |
|
{ |
34 |
513788 |
std::string tmp(s); |
35 |
|
// must quote the symbol, but it cannot contain | or \, we turn those into _ |
36 |
|
size_t p; |
37 |
260246 |
while((p = tmp.find_first_of("\\|")) != std::string::npos) { |
38 |
1676 |
tmp = tmp.replace(p, 1, "_"); |
39 |
|
} |
40 |
256894 |
return "|" + tmp + "|"; |
41 |
|
} |
42 |
2333778 |
return s; |
43 |
|
} |
44 |
|
|
45 |
73 |
std::string quoteString(const std::string& s) { |
46 |
|
// escape all double-quotes |
47 |
146 |
std::string output = s; |
48 |
73 |
size_t pos = 0; |
49 |
85 |
while ((pos = output.find('"', pos)) != std::string::npos) |
50 |
|
{ |
51 |
6 |
output.replace(pos, 1, "\"\""); |
52 |
6 |
pos += 2; |
53 |
|
} |
54 |
146 |
return '"' + output + '"'; |
55 |
|
} |
56 |
|
|
57 |
|
} // namespace cvc5 |