/ext/escape_utils/houdini_js_u.c

http://github.com/brianmario/escape_utils · C · 60 lines · 44 code · 14 blank · 2 comment · 13 complexity · 280148634bc8ae5d04515d40a30eede1 MD5 · raw file

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "houdini.h"
  5. int
  6. houdini_unescape_js(gh_buf *ob, const uint8_t *src, size_t size)
  7. {
  8. size_t i = 0, org, ch;
  9. while (i < size) {
  10. org = i;
  11. while (i < size && src[i] != '\\')
  12. i++;
  13. if (likely(i > org)) {
  14. if (unlikely(org == 0)) {
  15. if (i >= size)
  16. return 0;
  17. gh_buf_grow(ob, HOUDINI_UNESCAPED_SIZE(size));
  18. }
  19. gh_buf_put(ob, src + org, i - org);
  20. }
  21. /* escaping */
  22. if (i == size)
  23. break;
  24. if (++i == size) {
  25. gh_buf_putc(ob, '\\');
  26. break;
  27. }
  28. ch = src[i];
  29. switch (ch) {
  30. case 'n':
  31. ch = '\n';
  32. /* pass through */
  33. case '\\':
  34. case '\'':
  35. case '\"':
  36. case '/':
  37. gh_buf_putc(ob, ch);
  38. i++;
  39. break;
  40. default:
  41. gh_buf_putc(ob, '\\');
  42. break;
  43. }
  44. }
  45. return 1;
  46. }