Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Quotes a string if necessary for smt2. 11 : : */ 12 : : 13 : : #include "util/smt2_quote_string.h" 14 : : 15 : : #include <sstream> 16 : : #include <string> 17 : : 18 : : namespace cvc5::internal { 19 : : 20 : : /** 21 : : * SMT-LIB 2 quoting for symbols 22 : : */ 23 : 18120191 : std::string quoteSymbol(const std::string& s) 24 : : { 25 [ + + ]: 18120191 : if (s.empty()) 26 : : { 27 : 15 : return "||"; 28 : : } 29 : : 30 : : // this is the set of SMT-LIBv2 permitted characters in "simple" (non-quoted) 31 : : // symbols 32 : 18120176 : if (s.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 33 : : "0123456789~!@$%^&*_-+=<>.?/") 34 : : == std::string::npos 35 [ + + ][ + + ]: 18120176 : && (s[0] < '0' || s[0] > '9')) [ + + ][ + + ] 36 : : { 37 : 18112810 : return s; 38 : : } 39 : 7366 : std::string tmp = s; 40 [ + + ][ + - ]: 7366 : if (s.front() == '|' && s.back() == '|' && s.length() > 1) [ + - ][ + + ] 41 : : { 42 : : // if s is already surrounded with vertical bars, we need to check the 43 : : // characters between them 44 : 58 : tmp = s.substr(1, s.length() - 2); 45 : : } 46 : : 47 : : // must quote the symbol, but it cannot contain | or \, we turn those into _ 48 : : size_t p; 49 [ + + ]: 7466 : while ((p = tmp.find_first_of("\\|")) != std::string::npos) 50 : : { 51 : 100 : tmp = tmp.replace(p, 1, "_"); 52 : : } 53 : 14732 : return "|" + tmp + "|"; 54 : 7366 : } 55 : : 56 : 142 : std::string quoteString(const std::string& s) 57 : : { 58 : : // escape all double-quotes 59 : 142 : std::string output = s; 60 : 142 : size_t pos = 0; 61 [ + + ]: 154 : while ((pos = output.find('"', pos)) != std::string::npos) 62 : : { 63 : 12 : output.replace(pos, 1, "\"\""); 64 : 12 : pos += 2; 65 : : } 66 : 426 : return '"' + output + '"'; 67 : 142 : } 68 : : 69 : : } // namespace cvc5::internal