buffer.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef CMARK_BUFFER_H
  2. #define CMARK_BUFFER_H
  3. #include <stddef.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <limits.h>
  7. #include <stdint.h>
  8. #include "config.h"
  9. #include "cmark.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. typedef int32_t bufsize_t;
  14. typedef struct {
  15. cmark_mem *mem;
  16. unsigned char *ptr;
  17. bufsize_t asize, size;
  18. } cmark_strbuf;
  19. extern unsigned char cmark_strbuf__initbuf[];
  20. #define CMARK_BUF_INIT(mem) \
  21. { mem, cmark_strbuf__initbuf, 0, 0 }
  22. /**
  23. * Initialize a cmark_strbuf structure.
  24. *
  25. * For the cases where CMARK_BUF_INIT cannot be used to do static
  26. * initialization.
  27. */
  28. void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
  29. bufsize_t initial_size);
  30. /**
  31. * Grow the buffer to hold at least `target_size` bytes.
  32. */
  33. void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size);
  34. void cmark_strbuf_free(cmark_strbuf *buf);
  35. void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b);
  36. bufsize_t cmark_strbuf_len(const cmark_strbuf *buf);
  37. int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b);
  38. unsigned char *cmark_strbuf_detach(cmark_strbuf *buf);
  39. void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
  40. const cmark_strbuf *buf);
  41. static CMARK_INLINE const char *cmark_strbuf_cstr(const cmark_strbuf *buf) {
  42. return (char *)buf->ptr;
  43. }
  44. #define cmark_strbuf_at(buf, n) ((buf)->ptr[n])
  45. void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
  46. bufsize_t len);
  47. void cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
  48. void cmark_strbuf_putc(cmark_strbuf *buf, int c);
  49. void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
  50. bufsize_t len);
  51. void cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
  52. void cmark_strbuf_clear(cmark_strbuf *buf);
  53. bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos);
  54. bufsize_t cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, bufsize_t pos);
  55. void cmark_strbuf_drop(cmark_strbuf *buf, bufsize_t n);
  56. void cmark_strbuf_truncate(cmark_strbuf *buf, bufsize_t len);
  57. void cmark_strbuf_rtrim(cmark_strbuf *buf);
  58. void cmark_strbuf_trim(cmark_strbuf *buf);
  59. void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
  60. void cmark_strbuf_unescape(cmark_strbuf *s);
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif