regex.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* regex.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "regex.h"
  30. #include "nrex.hpp"
  31. #include "core/os/memory.h"
  32. void RegEx::_bind_methods() {
  33. ObjectTypeDB::bind_method(_MD("compile","pattern", "capture"),&RegEx::compile, DEFVAL(9));
  34. ObjectTypeDB::bind_method(_MD("find","text","start","end"),&RegEx::find, DEFVAL(0), DEFVAL(-1));
  35. ObjectTypeDB::bind_method(_MD("clear"),&RegEx::clear);
  36. ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid);
  37. ObjectTypeDB::bind_method(_MD("get_capture_count"),&RegEx::get_capture_count);
  38. ObjectTypeDB::bind_method(_MD("get_capture","capture"),&RegEx::get_capture);
  39. ObjectTypeDB::bind_method(_MD("get_capture_start","capture"),&RegEx::get_capture_start);
  40. ObjectTypeDB::bind_method(_MD("get_captures"),&RegEx::_bind_get_captures);
  41. };
  42. StringArray RegEx::_bind_get_captures() const {
  43. StringArray ret;
  44. int count = get_capture_count();
  45. for (int i=0; i<count; i++) {
  46. String c = get_capture(i);
  47. ret.push_back(c);
  48. };
  49. return ret;
  50. };
  51. void RegEx::clear() {
  52. text.clear();
  53. captures.clear();
  54. exp.reset();
  55. };
  56. bool RegEx::is_valid() const {
  57. return exp.valid();
  58. };
  59. int RegEx::get_capture_count() const {
  60. ERR_FAIL_COND_V( !exp.valid(), 0 );
  61. return exp.capture_size();
  62. }
  63. String RegEx::get_capture(int capture) const {
  64. ERR_FAIL_COND_V( get_capture_count() <= capture, String() );
  65. return text.substr(captures[capture].start, captures[capture].length);
  66. }
  67. int RegEx::get_capture_start(int capture) const {
  68. ERR_FAIL_COND_V( get_capture_count() <= capture, -1 );
  69. return captures[capture].start;
  70. }
  71. Error RegEx::compile(const String& p_pattern, int capture) {
  72. clear();
  73. exp.compile(p_pattern.c_str(), capture);
  74. ERR_FAIL_COND_V( !exp.valid(), FAILED );
  75. captures.resize(exp.capture_size());
  76. return OK;
  77. };
  78. int RegEx::find(const String& p_text, int p_start, int p_end) const {
  79. ERR_FAIL_COND_V( !exp.valid(), -1 );
  80. ERR_FAIL_COND_V( p_text.length() < p_start, -1 );
  81. ERR_FAIL_COND_V( p_text.length() < p_end, -1 );
  82. bool res = exp.match(p_text.c_str(), &captures[0], p_start, p_end);
  83. if (res) {
  84. text = p_text;
  85. return captures[0].start;
  86. }
  87. text.clear();
  88. return -1;
  89. };
  90. RegEx::RegEx(const String& p_pattern) {
  91. compile(p_pattern);
  92. };
  93. RegEx::RegEx() {
  94. };
  95. RegEx::~RegEx() {
  96. clear();
  97. };