test-pcre.nut 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //auto str = " this code 10.89.973.8.3.00.34-8 is special 23.456-2 car";
  2. auto str = " this code 10.89.973.8.3.00.34/8 is special 23.456/2 car";
  3. print("loadlib", sqpcre.loadlib("/libs/pcre-8.38/.libs/libpcre.so.1.2.6"));
  4. //print("loadlib", sqpcre.loadlib("/libs/pcre2-10.20/.libs/libpcre2-8.so"));
  5. //auto pcre = sqpcre(@"(\d+[.,\-])+\d+");
  6. //auto pcre = sqpcre(@"(?:\d+[.,\-/])+\d+");
  7. auto pcre = sqpcre(@"(?:\d+[.,\-/])+\d+");
  8. pcre.study(pcre.STUDY_JIT_COMPILE);
  9. print(pcre, typeof(pcre));
  10. print(pcre.version());
  11. print("match", pcre.match(str));
  12. pcre.gmatch(str,
  13. function(...){
  14. print("gmatch vargv.len()", vargv.len());
  15. foreach(idx, elm in vargv) print(idx, elm);
  16. return true;
  17. });
  18. auto new_str = pcre.gsub(str, "@$0@");
  19. print(new_str);
  20. auto result = [];
  21. auto rc, start_pos;
  22. auto max_loop = 1;
  23. auto start_time = os.getmillicount();
  24. for(auto i=0; i < max_loop; ++i)
  25. {
  26. start_pos = 0;
  27. while( (rc = pcre.exec(str, result, start_pos)) > 0)
  28. {
  29. print(rc, result.len());
  30. if(rc > 0)
  31. {
  32. //foreach(idx, elm in result)
  33. print(str.slice(result[0], result[1]));
  34. start_pos = result[1] + 1;
  35. }
  36. }
  37. }
  38. print("Spent time", os.getmillicount() - start_time);
  39. /*
  40. start_time = os.getmillicount();
  41. for(auto i=0; i < max_loop; ++i)
  42. {
  43. start_pos = 0;
  44. while( (rc = str.find_lua("(%d[,.%-])", result, start_pos)) > 0)
  45. {
  46. print(rc, result.len());
  47. if(rc > 0)
  48. {
  49. foreach(idx, elm in result) print(idx, elm, str.slice(result[0], result[1]));
  50. start_pos = result[1] + 1;
  51. }
  52. }
  53. }
  54. print("Spent time", os.getmillicount() - start_time);
  55. */