Minor fix for `number_set_prepend'.
[ttfautohint.git] / lib / numberset.h
blobe52fec56ea41df993221d03a306ff087c70d7bc8
1 /* numberset.h */
3 /*
4 * Copyright (C) 2012-2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #ifndef __NUMBERSET_H__
17 #define __NUMBERSET_H__
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
24 * A structure defining an integer range, to be used as a linked list. It
25 * gets allocated by a successful call to `number_set_parse'. Use
26 * `number_set_free' to deallocate it.
29 typedef struct number_range_
31 int start;
32 int end;
34 struct number_range_* next;
35 } number_range;
39 * Create and initialize a `number_range' object. In case of an allocation
40 * error, return NUMBERSET_ALLOCATION_ERROR. If either `start' or `end'
41 * exceeds the range [min;max], return NUMBERSET_INVALID_RANGE.
44 number_range*
45 number_set_new(int start,
46 int end,
47 int min,
48 int max);
52 * Prepend a single `number_range' object `element' to `list' of
53 * `number_range' objects, which might be NULL. `list' is expected to be
54 * stored in reversed order; consequently, the range in `element' must be
55 * larger than the first element of `list', otherwise an error is returned.
56 * If possible, the ranges of `element' and the first element of `list' are
57 * merged, in which case `element' gets deallocated.
59 * If `element' is NULL, return `list.
62 number_range*
63 number_set_prepend(number_range* list,
64 number_range* element);
68 * Reverse a list of `number_range' objects.
71 number_range*
72 number_set_reverse(number_range* list);
76 * Parse a description in string `s' for a set of non-negative integers
77 * within the limits given by the input parameters `min' and `max', and
78 * which consists of the following ranges, separated by commas (`n' and `m'
79 * are non-negative integers):
81 * -n min <= x <= n
82 * n x = n; this is a shorthand for `n-n'
83 * n-m n <= x <= m (or m <= x <= n if m < n)
84 * m- m <= x <= max
85 * - min <= x <= max
87 * Superfluous commas are ignored, as is whitespace around numbers, dashes,
88 * and commas. The ranges must be ordered, without overlaps. As a
89 * consequence, `-n' and `m-' can occur at most once and must be then the
90 * first and last range, respectively; similarly, `-' cannot be paired with
91 * any other range.
93 * In the following examples, `min' is 4 and `max' is 12:
95 * - -> 4, 5, 6, 7, 8, 9, 10, 11, 12
96 * -3, 5- -> invalid first range
97 * 4, 6-8, 10- -> 4, 6, 7, 8, 10, 11, 12
98 * 4-8, 6-10 -> invalid overlapping ranges
100 * In case of success (this is, the number set description in `s' is valid)
101 * the return value is a pointer to the final zero byte in string `s'. In
102 * case of an error, the return value is a pointer to the beginning position
103 * of the offending range in string `s'.
105 * If s is NULL, the function exits immediately with NULL as the return
106 * value.
108 * If the user provides a non-NULL `number_set' value, `number_set_parse'
109 * stores a linked list of ordered number ranges in `*number_set', allocated
110 * with `malloc'. If there is no range at all (for example, an empty string
111 * or whitespace and commas only) no data gets allocated, and `*number_set'
112 * is set to NULL. In case of error, `*number_set' returns an error code;
113 * you should use the following macros to compare with.
115 * NUMBERSET_INVALID_CHARACTER invalid character in description string
116 * NUMBERSET_OVERFLOW numerical overflow
117 * NUMBERSET_INVALID_RANGE invalid range, exceeding `min' or `max'
118 * NUMBERSET_OVERLAPPING_RANGES overlapping ranges
119 * NUMBERSET_NOT_ASCENDING not ascending ranges or values
120 * NUMBERSET_ALLOCATION_ERROR allocation error
122 * Note that a negative value for `min' is replaced with zero, and a
123 * negative value for `max' with the largest representable integer, INT_MAX.
126 #define NUMBERSET_INVALID_CHARACTER (number_range*)-1
127 #define NUMBERSET_OVERFLOW (number_range*)-2
128 #define NUMBERSET_INVALID_RANGE (number_range*)-3
129 #define NUMBERSET_OVERLAPPING_RANGES (number_range*)-4
130 #define NUMBERSET_NOT_ASCENDING (number_range*)-5
131 #define NUMBERSET_ALLOCATION_ERROR (number_range*)-6
133 const char*
134 number_set_parse(const char* s,
135 number_range** number_set,
136 int min,
137 int max);
141 * Free the allocated data in `number_set'.
144 void
145 number_set_free(number_range* number_set);
149 * Return a string representation of `number_set', viewed through a
150 * `window', so to say, spanned up by the parameters `min' and `max'. After
151 * use, the string should be deallocated with a call to `free'. In case of
152 * an allocation error, the return value is NULL.
154 * Note that a negative value for `min' is replaced with zero, and a
155 * negative value for `max' with the largest representable integer, INT_MAX.
158 char*
159 number_set_show(number_range* number_set,
160 int min,
161 int max);
165 * Return value 1 if `number' is element of `number_set', zero otherwise.
169 number_set_is_element(number_range* number_set,
170 int number);
174 * A structure used to iterate over a number set.
177 typedef struct number_set_iter_
179 number_range* range;
180 int val;
181 } number_set_iter;
185 * Get first element of a number set. `iter_p' must be initialized with the
186 * `number_range' structure to iterate over. After the call, `iter_p' is
187 * ready to be used in a call to `number_set_get_next'.
191 number_set_get_first(number_set_iter* iter_p);
195 * Get next element of a number set, using `iter_p' from a previous call to
196 * `number_set_get_first' or `number_set_get_next'. If `iter_p->range' is
197 * NULL after the call, there is no next element, and the return value is
198 * undefined.
202 number_set_get_next(number_set_iter* iter_p);
204 #ifdef __cplusplus
205 } /* extern "C" */
206 #endif
208 #endif /* __NUMBERSET_H__ */
210 /* end of numberset.h */