regaux.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "regexp.h"
  4. #include "regcomp.h"
  5. /*
  6. * save a new match in mp
  7. */
  8. extern void
  9. _renewmatch(Resub *mp, int ms, Resublist *sp)
  10. {
  11. int i;
  12. if(mp==0 || ms<=0)
  13. return;
  14. if(mp[0].sp==0 || sp->m[0].sp<mp[0].sp ||
  15. (sp->m[0].sp==mp[0].sp && sp->m[0].ep>mp[0].ep)){
  16. for(i=0; i<ms && i<NSUBEXP; i++)
  17. mp[i] = sp->m[i];
  18. for(; i<ms; i++)
  19. mp[i].sp = mp[i].ep = 0;
  20. }
  21. }
  22. /*
  23. * Note optimization in _renewthread:
  24. * *lp must be pending when _renewthread called; if *l has been looked
  25. * at already, the optimization is a bug.
  26. */
  27. extern Relist*
  28. _renewthread(Relist *lp, /* _relist to add to */
  29. Reinst *ip, /* instruction to add */
  30. int ms,
  31. Resublist *sep) /* pointers to subexpressions */
  32. {
  33. Relist *p;
  34. for(p=lp; p->inst; p++){
  35. if(p->inst == ip){
  36. if(sep->m[0].sp < p->se.m[0].sp){
  37. if(ms > 1)
  38. p->se = *sep;
  39. else
  40. p->se.m[0] = sep->m[0];
  41. }
  42. return 0;
  43. }
  44. }
  45. p->inst = ip;
  46. if(ms > 1)
  47. p->se = *sep;
  48. else
  49. p->se.m[0] = sep->m[0];
  50. (++p)->inst = 0;
  51. return p;
  52. }
  53. /*
  54. * same as renewthread, but called with
  55. * initial empty start pointer.
  56. */
  57. extern Relist*
  58. _renewemptythread(Relist *lp, /* _relist to add to */
  59. Reinst *ip, /* instruction to add */
  60. int ms,
  61. char *sp) /* pointers to subexpressions */
  62. {
  63. Relist *p;
  64. for(p=lp; p->inst; p++){
  65. if(p->inst == ip){
  66. if(sp < p->se.m[0].sp) {
  67. if(ms > 1)
  68. memset(&p->se, 0, sizeof(p->se));
  69. p->se.m[0].sp = sp;
  70. }
  71. return 0;
  72. }
  73. }
  74. p->inst = ip;
  75. if(ms > 1)
  76. memset(&p->se, 0, sizeof(p->se));
  77. p->se.m[0].sp = sp;
  78. (++p)->inst = 0;
  79. return p;
  80. }
  81. extern Relist*
  82. _rrenewemptythread(Relist *lp, /* _relist to add to */
  83. Reinst *ip, /* instruction to add */
  84. int ms,
  85. Rune *rsp) /* pointers to subexpressions */
  86. {
  87. Relist *p;
  88. for(p=lp; p->inst; p++){
  89. if(p->inst == ip){
  90. if(rsp < p->se.m[0].rsp) {
  91. if(ms > 1)
  92. memset(&p->se, 0, sizeof(p->se));
  93. p->se.m[0].rsp = rsp;
  94. }
  95. return 0;
  96. }
  97. }
  98. p->inst = ip;
  99. if(ms > 1)
  100. memset(&p->se, 0, sizeof(p->se));
  101. p->se.m[0].rsp = rsp;
  102. (++p)->inst = 0;
  103. return p;
  104. }