aboutsummaryrefslogtreecommitdiff
path: root/include/slice.h
blob: add82d539fa9513a41538e51527f44ad9d8c53c5 (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
#ifndef CMONKEY_SLICE_H
#define CMONKEY_SLICE_H

#include "sds/sds.h"

#include <string.h>
#include <sys/types.h>

/* A slice of a C string */
struct slice {
	const char *str;
	size_t start;
	size_t end;
};

/* Create a new slice from an existing string indicating its bounds */
struct slice slice_new(const char *str, size_t start, size_t end);

/* Create a new slice from a string literal */
#define slice_whole(s) (struct slice){ s, 0, strlen(s), }

/* Set a slice to a new string and bounds */
void slice_set(struct slice *, const char *str, size_t start, size_t end);

/* Get the length of the slice */
size_t slice_len(const struct slice *);

/* Returns 0 if equal, 1 if a > b, -1 if a < b */
int slice_cmp(const struct slice *restrict a, const struct slice *restrict b);

/* Copy the slice from src to dst; dst should already be allocated */
void slice_cpy(struct slice *dst, const struct slice *src);

/* Concatenate the slice to an SDS string */
sds slice_string(const struct slice *, sds str);

#endif