regex.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************/
  2. /* regex.cpp */
  3. /*************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /*************************************************/
  7. /* Source code within this file is: */
  8. /* (c) 2007-2016 Juan Linietsky, Ariel Manzur */
  9. /* All Rights Reserved. */
  10. /*************************************************/
  11. #include "regex.h"
  12. #include "nrex.hpp"
  13. #include "core/os/memory.h"
  14. void RegEx::_bind_methods() {
  15. ObjectTypeDB::bind_method(_MD("compile","pattern", "capture"),&RegEx::compile, DEFVAL(9));
  16. ObjectTypeDB::bind_method(_MD("find","text","start","end"),&RegEx::find, DEFVAL(0), DEFVAL(-1));
  17. ObjectTypeDB::bind_method(_MD("clear"),&RegEx::clear);
  18. ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid);
  19. ObjectTypeDB::bind_method(_MD("get_capture_count"),&RegEx::get_capture_count);
  20. ObjectTypeDB::bind_method(_MD("get_capture","capture"),&RegEx::get_capture);
  21. ObjectTypeDB::bind_method(_MD("get_capture_start","capture"),&RegEx::get_capture_start);
  22. ObjectTypeDB::bind_method(_MD("get_captures"),&RegEx::_bind_get_captures);
  23. };
  24. StringArray RegEx::_bind_get_captures() const {
  25. StringArray ret;
  26. int count = get_capture_count();
  27. for (int i=0; i<count; i++) {
  28. String c = get_capture(i);
  29. ret.push_back(c);
  30. };
  31. return ret;
  32. };
  33. void RegEx::clear() {
  34. text.clear();
  35. captures.clear();
  36. exp.reset();
  37. };
  38. bool RegEx::is_valid() const {
  39. return exp.valid();
  40. };
  41. int RegEx::get_capture_count() const {
  42. ERR_FAIL_COND_V( !exp.valid(), 0 );
  43. return exp.capture_size();
  44. }
  45. String RegEx::get_capture(int capture) const {
  46. ERR_FAIL_COND_V( get_capture_count() <= capture, String() );
  47. return text.substr(captures[capture].start, captures[capture].length);
  48. }
  49. int RegEx::get_capture_start(int capture) const {
  50. ERR_FAIL_COND_V( get_capture_count() <= capture, -1 );
  51. return captures[capture].start;
  52. }
  53. Error RegEx::compile(const String& p_pattern, int capture) {
  54. clear();
  55. exp.compile(p_pattern.c_str(), capture);
  56. ERR_FAIL_COND_V( !exp.valid(), FAILED );
  57. captures.resize(exp.capture_size());
  58. return OK;
  59. };
  60. int RegEx::find(const String& p_text, int p_start, int p_end) const {
  61. ERR_FAIL_COND_V( !exp.valid(), -1 );
  62. ERR_FAIL_COND_V( p_text.length() < p_start, -1 );
  63. ERR_FAIL_COND_V( p_text.length() < p_end, -1 );
  64. bool res = exp.match(p_text.c_str(), &captures[0], p_start, p_end);
  65. if (res) {
  66. text = p_text;
  67. return captures[0].start;
  68. }
  69. text.clear();
  70. return -1;
  71. };
  72. RegEx::RegEx(const String& p_pattern) {
  73. compile(p_pattern);
  74. };
  75. RegEx::RegEx() {
  76. };
  77. RegEx::~RegEx() {
  78. clear();
  79. };