parse_helper.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "base/parse_helper.h"
  8. namespace base {
  9. namespace parse {
  10. // inspired by https://github.com/sindresorhus/strip-json-comments
  11. QByteArray stripComments(const QByteArray &content) {
  12. enum class InsideComment {
  13. None,
  14. SingleLine,
  15. MultiLine,
  16. };
  17. auto insideComment = InsideComment::None;
  18. auto insideString = false;
  19. QByteArray result;
  20. auto begin = content.cbegin(), end = content.cend(), offset = begin;
  21. auto feedContent = [&result, &offset, end](const char *ch) {
  22. if (ch > offset) {
  23. if (result.isEmpty()) result.reserve(end - offset - 2);
  24. result.append(offset, ch - offset);
  25. offset = ch;
  26. }
  27. };
  28. auto feedComment = [&result, &offset, end](const char *ch) {
  29. if (ch > offset) {
  30. if (result.isEmpty()) result.reserve(end - offset - 2);
  31. result.append(' ');
  32. offset = ch;
  33. }
  34. };
  35. for (auto ch = offset; ch != end;) {
  36. auto currentChar = *ch;
  37. auto nextChar = (ch + 1 == end) ? 0 : *(ch + 1);
  38. if (insideComment == InsideComment::None && currentChar == '"') {
  39. auto escaped = ((ch > begin) && *(ch - 1) == '\\') && ((ch - 1 < begin) || *(ch - 2) != '\\');
  40. if (!escaped) {
  41. insideString = !insideString;
  42. }
  43. }
  44. if (insideString) {
  45. ++ch;
  46. continue;
  47. }
  48. if (insideComment == InsideComment::None && currentChar == '/' && nextChar == '/') {
  49. feedContent(ch);
  50. insideComment = InsideComment::SingleLine;
  51. ch += 2;
  52. } else if (insideComment == InsideComment::SingleLine && currentChar == '\r' && nextChar == '\n') {
  53. feedComment(ch);
  54. ch += 2;
  55. insideComment = InsideComment::None;
  56. } else if (insideComment == InsideComment::SingleLine && currentChar == '\n') {
  57. feedComment(ch);
  58. ++ch;
  59. insideComment = InsideComment::None;
  60. } else if (insideComment == InsideComment::None && currentChar == '/' && nextChar == '*') {
  61. feedContent(ch);
  62. ch += 2;
  63. insideComment = InsideComment::MultiLine;
  64. } else if (insideComment == InsideComment::MultiLine && currentChar == '*' && nextChar == '/') {
  65. ch += 2;
  66. feedComment(ch);
  67. insideComment = InsideComment::None;
  68. } else if (insideComment == InsideComment::MultiLine && currentChar == '\r' && nextChar == '\n') {
  69. feedComment(ch);
  70. ch += 2;
  71. feedContent(ch);
  72. } else if (insideComment == InsideComment::MultiLine && currentChar == '\n') {
  73. feedComment(ch);
  74. ++ch;
  75. feedContent(ch);
  76. } else {
  77. ++ch;
  78. }
  79. }
  80. if (insideComment == InsideComment::MultiLine) {
  81. // unexpected end of content
  82. }
  83. if (insideComment == InsideComment::None && end > offset) {
  84. if (result.isEmpty()) {
  85. return content;
  86. } else {
  87. result.append(offset, end - offset);
  88. }
  89. }
  90. return result;
  91. }
  92. } // namespace parse
  93. } // namespace base