Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Mudathir Mohamed, Abdalrhman Mohamed, Tim King 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2025 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::internal { 22 : : 23 : : /** 24 : : * SMT-LIB 2 quoting for symbols 25 : : */ 26 : 17999415 : std::string quoteSymbol(const std::string& s) 27 : : { 28 [ + + ]: 17999415 : if (s.empty()) 29 : : { 30 : 15 : return "||"; 31 : : } 32 : : 33 : : // this is the set of SMT-LIBv2 permitted characters in "simple" (non-quoted) 34 : : // symbols 35 : 17999400 : if (s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 36 : : "0123456789~!@$%^&*_-+=<>.?/") 37 : : == std::string::npos 38 [ + + ][ + + ]: 17999400 : && (s[0] < '0' || s[0] > '9')) [ + + ][ + + ] 39 : : { 40 : 17992006 : return s; 41 : : } 42 : 7394 : std::string tmp = s; 43 [ + + ][ + - ]: 7394 : if (s.front() == '|' && s.back() == '|' && s.length() > 1) [ + - ][ + + ] 44 : : { 45 : : // if s is already surrounded with vertical bars, we need to check the 46 : : // characters between them 47 : 58 : tmp = s.substr(1, s.length() - 2); 48 : : } 49 : : 50 : : // must quote the symbol, but it cannot contain | or \, we turn those into _ 51 : : size_t p; 52 [ + + ]: 7494 : while ((p = tmp.find_first_of("\\|")) != std::string::npos) 53 : : { 54 : 100 : tmp = tmp.replace(p, 1, "_"); 55 : : } 56 : 14788 : return "|" + tmp + "|"; 57 : 7394 : } 58 : : 59 : 146 : std::string quoteString(const std::string& s) 60 : : { 61 : : // escape all double-quotes 62 : 146 : std::string output = s; 63 : 146 : size_t pos = 0; 64 [ + + ]: 158 : while ((pos = output.find('"', pos)) != std::string::npos) 65 : : { 66 : 12 : output.replace(pos, 1, "\"\""); 67 : 12 : pos += 2; 68 : : } 69 : 438 : return '"' + output + '"'; 70 : 146 : } 71 : : 72 : : } // namespace cvc5::internal