regsub.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "regexp.h"
  4. /* substitute into one string using the matches from the last regexec() */
  5. extern void
  6. regsub(char *sp, /* source string */
  7. char *dp, /* destination string */
  8. int dlen,
  9. Resub *mp, /* subexpression elements */
  10. int ms) /* number of elements pointed to by mp */
  11. {
  12. char *ssp, *ep;
  13. int i;
  14. ep = dp+dlen-1;
  15. while(*sp != '\0'){
  16. if(*sp == '\\'){
  17. switch(*++sp){
  18. case '0':
  19. case '1':
  20. case '2':
  21. case '3':
  22. case '4':
  23. case '5':
  24. case '6':
  25. case '7':
  26. case '8':
  27. case '9':
  28. i = *sp-'0';
  29. if(mp[i].sp != 0 && mp!=0 && ms>i)
  30. for(ssp = mp[i].sp;
  31. ssp < mp[i].ep;
  32. ssp++)
  33. if(dp < ep)
  34. *dp++ = *ssp;
  35. break;
  36. case '\\':
  37. if(dp < ep)
  38. *dp++ = '\\';
  39. break;
  40. case '\0':
  41. sp--;
  42. break;
  43. default:
  44. if(dp < ep)
  45. *dp++ = *sp;
  46. break;
  47. }
  48. }else if(*sp == '&'){
  49. if(mp[0].sp != 0 && mp!=0 && ms>0)
  50. if(mp[0].sp != 0)
  51. for(ssp = mp[0].sp;
  52. ssp < mp[0].ep; ssp++)
  53. if(dp < ep)
  54. *dp++ = *ssp;
  55. }else{
  56. if(dp < ep)
  57. *dp++ = *sp;
  58. }
  59. sp++;
  60. }
  61. *dp = '\0';
  62. }