You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0 KiB

  1. //==============================================================================
  2. //
  3. // Copyright (c) 2015-
  4. // Authors:
  5. // * Joachim Klein <klein@tcs.inf.tu-dresden.de>
  6. // * David Mueller <david.mueller@tcs.inf.tu-dresden.de>
  7. //
  8. //------------------------------------------------------------------------------
  9. //
  10. // This file is part of the cpphoafparser library,
  11. // http://automata.tools/hoa/cpphoafparser/
  12. //
  13. // The cpphoafparser library is free software; you can redistribute it and/or
  14. // modify it under the terms of the GNU Lesser General Public
  15. // License as published by the Free Software Foundation; either
  16. // version 2.1 of the License, or (at your option) any later version.
  17. //
  18. // The cpphoafparser library is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. // Lesser General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Lesser General Public
  24. // License along with this library; if not, write to the Free Software
  25. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. //
  27. //==============================================================================
  28. #ifndef CPPHOAFPARSER_HOAPARSERHELPER_H
  29. #define CPPHOAFPARSER_HOAPARSERHELPER_H
  30. #include <iostream>
  31. #include <string>
  32. #include <sstream>
  33. #include "cpphoafparser/parser/hoa_parser_exception.hh"
  34. namespace cpphoafparser {
  35. /** Helper function for dealing with quoted strings */
  36. class HOAParserHelper {
  37. public:
  38. /**
  39. * Outputs a quoted version of the string to the ostream.
  40. * The string is quoted to conform the to double-quoted
  41. * string syntax of the HOA format.
  42. */
  43. static void print_quoted(std::ostream& out, const std::string& s) {
  44. std::string::size_type pos = s.find_first_of("\\\""); // find next \ or "
  45. if (pos == std::string::npos) {
  46. // nothing to quote
  47. out << "\"" << s << "\"";
  48. return;
  49. }
  50. out << "\"";
  51. std::string::size_type last_pos = 0;
  52. do {
  53. out << s.substr(last_pos, pos); // characters that don't need to be quoted
  54. out << "\\"; // add quote
  55. out << s.substr(pos,1); // add character
  56. last_pos = pos+1;
  57. pos = s.find_first_of("\\\"", last_pos); // find next \ or "
  58. } while (pos != std::string::npos);
  59. if (last_pos < s.length()) {
  60. out << s.substr(last_pos); // remaining
  61. }
  62. out << "\"";
  63. }
  64. /** Returns a double-quoted version of the string (HOA syntax and quoting rules). */
  65. static std::string quote(const std::string& s) {
  66. std::stringstream ss;
  67. print_quoted(ss, s);
  68. return ss.str();
  69. }
  70. /**
  71. * Performs unquoting of a double-quoted string (HOA syntax and quoting rules).
  72. * If the string is not a validly quoted string, a HOAParserException is thrown.
  73. * @returns the unquoted string
  74. **/
  75. static std::string unquote(const std::string& s) {
  76. if (s.length() == 0 || s.at(0) != '"') {
  77. throw HOAParserException("String not quoted : " + s);
  78. }
  79. if (s.back() != '"') {
  80. throw HOAParserException("String does not end with quote: " + s);
  81. }
  82. std::string::size_type pos = s.find_first_of("\\", 1); // find first '\'
  83. if (pos == std::string::npos) {
  84. // nothing to unquote, just remove outer quotes
  85. return s.substr(1, s.length()-2);
  86. }
  87. std::stringstream ss;
  88. std::string::size_type last_pos = 1;
  89. do {
  90. ss << s.substr(last_pos, pos); // characters that don't need to be quoted
  91. if (pos+1 >= s.length()) {
  92. throw HOAParserException("String ends with \\ character: s");
  93. }
  94. ss << s.substr(pos+1,1); // the quoted character
  95. last_pos = pos+2; // skip quote character
  96. pos = s.find_first_of("\\\"", last_pos); // find next '\'
  97. } while (pos != std::string::npos);
  98. if (last_pos < s.length()-1) {
  99. ss << s.substr(last_pos, s.length()-1-last_pos); // remaining
  100. }
  101. return ss.str();
  102. }
  103. };
  104. }
  105. #endif