summaryrefslogtreecommitdiff
path: root/src/chunk.h
blob: e4a57b2cd206cb1e385c62a5fa4c9d98a005b19f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef _CHUNK_H_
#define _CHUNK_H_

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include "buffer.h"

typedef struct {
	const unsigned char *data;
	int len;
	int alloc;
} cmark_chunk;

static inline void cmark_chunk_free(cmark_chunk *c)
{
	if (c->alloc)
		free((char *)c->data);

	c->data = NULL;
	c->alloc = 0;
	c->len = 0;
}

static inline void cmark_chunk_ltrim(cmark_chunk *c)
{
	assert(!c->alloc);

	while (c->len && isspace(c->data[0])) {
		c->data++;
		c->len--;
	}
}

static inline void cmark_chunk_rtrim(cmark_chunk *c)
{
	while (c->len > 0) {
		if (!isspace(c->data[c->len - 1]))
			break;

		c->len--;
	}
}

static inline void cmark_chunk_trim(cmark_chunk *c)
{
	cmark_chunk_ltrim(c);
	cmark_chunk_rtrim(c);
}

static inline int cmark_chunk_strchr(cmark_chunk *ch, int c, int offset)
{
	const unsigned char *p = memchr(ch->data + offset, c, ch->len - offset);
	return p ? (int)(p - ch->data) : ch->len;
}

static inline unsigned char *cmark_chunk_to_cstr(cmark_chunk *c)
{
	unsigned char *str;

	str = calloc(c->len + 1, sizeof(*str));
    if(str != NULL) {
    	memcpy(str, c->data, c->len);
	    str[c->len] = 0;
    }
	return str;
}

static inline cmark_chunk cmark_chunk_literal(const char *data)
{
	cmark_chunk c = {(const unsigned char *)data, data ? strlen(data) : 0, 0};
	return c;
}

static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, int pos, int len)
{
	cmark_chunk c = {ch->data + pos, len, 0};
	return c;
}

static inline cmark_chunk cmark_chunk_buf_detach(cmark_strbuf *buf)
{
	cmark_chunk c;

	c.len = buf->size;
	c.data = cmark_strbuf_detach(buf);
	c.alloc = 1;

	return c;
}

#ifndef CMARK_NO_SHORT_NAMES
  #define chunk             cmark_chunk
  #define chunk_free        cmark_chunk_free
  #define chunk_ltrim       cmark_chunk_ltrim
  #define chunk_rtrim       cmark_chunk_rtrim
  #define chunk_trim        cmark_chunk_trim
  #define chunk_strchr      cmark_chunk_strchr
  #define chunk_to_cstr     cmark_chunk_to_cstr
  #define chunk_literal     cmark_chunk_literal
  #define chunk_dup         cmark_chunk_dup
  #define chunk_buf_detach  cmark_chunk_buf_detach
#endif

#endif