GS1 Barcode Syntax Tests reference
A reference to the AI component "linter" routines referred to by the GS1 Barcode Syntax Dictionary. Copyright (c) 2022-2024 GS1 AISBL.
lint_posinseqslash.c File Reference

Purpose

The posinseqslash linter ensures that the data represents a meaningful position in a sequence in the format "`<pos>/<end>`".

Functional Description

◆ gs1_lint_posinseqslash()

GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_posinseqslash ( const char *const  data,
size_t *const  err_pos,
size_t *const  err_len 
)

Used to ensure that an AI component conforms to a "`<pos>/<end>`" format for variable width <pos> and <end>.

Parameters
[in]dataPointer to the null-terminated data to be linted. Must not be NULL.
[out]err_posTo facilitate error highlighting, the start position of the bad data is written to this pointer, if not NULL.
[out]err_lenThe length of the bad data is written to this pointer, if not NULL.
Returns
GS1_LINTER_OK if okay.
GS1_LINTER_POSITION_IN_SEQUENCE_MALFORMED if the data does not conform to "`<pos>/<end>`" format.
GS1_LINTER_ILLEGAL_ZERO_PREFIX if either the position number or the end number are zero or contain a zero prefix.
GS1_LINTER_POSITION_EXCEEDS_END if the data contains a position number that is larger than the end position.
54{
55
56/// \cond
57#define P(i) data[i]
58#define E(i) data[i + pos + 1]
59/// \endcond
60
61 size_t pos, len, slash_pos;
62
63 assert(data);
64
65 /*
66 * First non-digit should be '/'
67 *
68 */
69 for (pos = 0; data[pos] >= '0' && data[pos] <= '9'; pos++);
70
71 /*
72 * Format so far must be digits + '/'
73 *
74 */
75 if (GS1_LINTER_UNLIKELY(pos == 0 || !data[pos] || data[pos] != '/')) {
76 while (data[pos]) pos++;
79 0,
80 pos
81 );
82 }
83
84 slash_pos = pos;
85
86 /*
87 * Validate that remaining characters are digits and measure length
88 *
89 */
90 for (pos++; data[pos]; pos++)
91 if (GS1_LINTER_UNLIKELY(data[pos] < '0' || data[pos] > '9')) {
92 while (data[pos]) pos++;
95 0,
96 pos
97 );
98 }
99
100 len = pos;
101
102 /*
103 * Must have digits after slash
104 */
105 if (GS1_LINTER_UNLIKELY(slash_pos >= len - 1))
108 0,
109 len
110 );
111
112 pos = slash_pos;
113
114 /*
115 * Ensure position number is non-zero and does not have zero prefix.
116 *
117 */
118 if (GS1_LINTER_UNLIKELY(P(0) == '0'))
121 0,
122 pos
123 );
124
125 /*
126 * Ensure end number is non-zero and does not have zero prefix.
127 *
128 */
129 if (GS1_LINTER_UNLIKELY(E(0) == '0'))
132 pos + 1,
133 len - pos - 1
134 );
135
136 /*
137 * Determine whether the position exceeds the end.
138 *
139 */
140 if (pos == len - pos - 1) {
141 size_t i;
142 int compare = 0; /* -1:P<E ; 0:P==E ; 1:P>E */
143 for (i = 0; i < pos && !compare; i++)
144 if (P(i) != E(i))
145 compare = P(i) < E(i) ? -1 : 1;
146 if (GS1_LINTER_UNLIKELY(compare == 1))
149 0,
150 len
151 );
152 } else if (GS1_LINTER_UNLIKELY(pos > len - pos - 1))
153 /* Non-zero prefix, so a length check is sufficient. */
156 0,
157 len
158 );
159
161
162}
#define GS1_LINTER_UNLIKELY(x)
Implemention may provide hint to the compiler that the expression is likely to be false.
Definition gs1syntaxdictionary-utils.h:76
#define GS1_LINTER_RETURN_ERROR(error, position, length)
Return from a linter indicating that a problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:103
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:88
@ GS1_LINTER_ILLEGAL_ZERO_PREFIX
A zero prefix is not permitted.
Definition gs1syntaxdictionary.h:94
@ GS1_LINTER_POSITION_IN_SEQUENCE_MALFORMED
The data must have the format "<pos>/<end>".
Definition gs1syntaxdictionary.h:189
@ GS1_LINTER_POSITION_EXCEEDS_END
The position number must not exceed the end number.
Definition gs1syntaxdictionary.h:190