RegexExplorer.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Kitware, Inc., Gregor Jasny
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "RegexExplorer.h"
  11. RegexExplorer::RegexExplorer(QWidget* p) : QDialog(p), m_matched(false)
  12. {
  13. this->setupUi(this);
  14. for(int i = 1; i < cmsys::RegularExpression::NSUBEXP; ++i)
  15. {
  16. matchNumber->addItem(
  17. QString("Match %1").arg(QString::number(i)),
  18. QVariant(i));
  19. }
  20. matchNumber->setCurrentIndex(0);
  21. }
  22. void RegexExplorer::setStatusColor(QWidget* widget, bool successful)
  23. {
  24. QColor color = successful ? QColor(0, 127, 0) : Qt::red;
  25. QPalette palette = widget->palette();
  26. palette.setColor(QPalette::Foreground, color);
  27. widget->setPalette(palette);
  28. }
  29. void RegexExplorer::on_regularExpression_textChanged(const QString& text)
  30. {
  31. #ifdef QT_NO_STL
  32. m_regex = text.toAscii().constData();
  33. #else
  34. m_regex = text.toStdString();
  35. #endif
  36. bool validExpression =
  37. stripEscapes(m_regex) && m_regexParser.compile(m_regex);
  38. if(!validExpression)
  39. {
  40. m_regexParser.set_invalid();
  41. }
  42. setStatusColor(labelRegexValid, validExpression);
  43. on_inputText_textChanged();
  44. }
  45. void RegexExplorer::on_inputText_textChanged()
  46. {
  47. if(m_regexParser.is_valid())
  48. {
  49. QString plainText = inputText->toPlainText();
  50. #ifdef QT_NO_STL
  51. m_text = plainText.toAscii().constData();
  52. #else
  53. m_text = plainText.toStdString();
  54. #endif
  55. m_matched = m_regexParser.find(m_text);
  56. }
  57. else
  58. {
  59. m_matched = false;
  60. }
  61. setStatusColor(labelRegexMatch, m_matched);
  62. if(!m_matched)
  63. {
  64. clearMatch();
  65. return;
  66. }
  67. #ifdef QT_NO_STL
  68. QString matchText = m_regexParser.match(0).c_str();
  69. #else
  70. QString matchText = QString::fromStdString(m_regexParser.match(0));
  71. #endif
  72. match0->setPlainText(matchText);
  73. on_matchNumber_currentIndexChanged(matchNumber->currentIndex());
  74. }
  75. void RegexExplorer::on_matchNumber_currentIndexChanged(int index)
  76. {
  77. if(!m_matched)
  78. {
  79. return;
  80. }
  81. QVariant itemData = matchNumber->itemData(index);
  82. int idx = itemData.toInt();
  83. if(idx < 1 || idx >= cmsys::RegularExpression::NSUBEXP)
  84. {
  85. return;
  86. }
  87. #ifdef QT_NO_STL
  88. QString match = m_regexParser.match(idx).c_str();
  89. #else
  90. QString match = QString::fromStdString(m_regexParser.match(idx));
  91. #endif
  92. matchN->setPlainText(match);
  93. }
  94. void RegexExplorer::clearMatch()
  95. {
  96. match0->clear();
  97. matchN->clear();
  98. }
  99. bool RegexExplorer::stripEscapes(std::string& source)
  100. {
  101. const char* in = source.c_str();
  102. std::string result;
  103. result.reserve(source.size());
  104. for(char inc = *in; inc != '\0'; inc = *++in)
  105. {
  106. if(inc == '\\')
  107. {
  108. char nextc = in[1];
  109. if(nextc == 't')
  110. {
  111. result.append(1, '\t');
  112. in++;
  113. }
  114. else if(nextc == 'n')
  115. {
  116. result.append(1, '\n');
  117. in++;
  118. }
  119. else if(nextc == 't')
  120. {
  121. result.append(1, '\t');
  122. in++;
  123. }
  124. else if(isalnum(nextc) || nextc == '\0')
  125. {
  126. return false;
  127. }
  128. else
  129. {
  130. result.append(1, nextc);
  131. in++;
  132. }
  133. }
  134. else
  135. {
  136. result.append(1, inc);
  137. }
  138. }
  139. source = result;
  140. return true;
  141. }