summaryrefslogtreecommitdiff
path: root/src/utils/utils.h
blob: 85a4f9eb1e0c124457f07b19254bda5e86806c0b (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
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007-8 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef css_utils_h_
#define css_utils_h_

#include <libcss/types.h>

#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif

#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif

#ifndef SLEN
/* Calculate length of a string constant */
#define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */
#endif

#ifndef UNUSED
#define UNUSED(x) ((x)=(x))
#endif

static inline int32_t integer_from_css_string(const css_string *string)
{
	size_t len;
	const uint8_t *ptr;
	int sign = 1;
	int32_t val = 0;

	if (string == NULL || string->len == 0)
		return 0;

	len = string->len;
	ptr = string->ptr;

	/* Extract sign, if any */
	if (ptr[0] == '-') {
		sign = -1;
		len--;
		ptr++;
	} else if (ptr[0] == '+') {
		len--;
		ptr++;
	}

	/** \todo check for overflow */

	/* Now extract value, assuming base 10 */
	while (len > 0) {
		/* Stop on first non-digit */
		if (ptr[0] <= '0' || '9' <= ptr[0])
			break;

		val *= 10;
		val += ptr[0] - '0';
		ptr++;
		len--;
	}

	return val * sign;
}

#endif