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;
62
63 assert(data);
64
65 len = strlen(data);
66
67 /*
68 * Determine that the format is "<pos>/<end>".
69 *
70 */
71 pos = strspn(data, "0123456789");
72 if (pos == 0 || pos >= len - 1 || data[pos] != '/' || strspn(data + pos + 1, "0123456789") != len - pos - 1)
75 0,
76 len
77 );
78
79 /*
80 * Ensure position number is non-zero and does not have zero prefix.
81 *
82 */
83 if (P(0) == '0')
86 0,
87 pos
88 );
89
90 /*
91 * Ensure end number is non-zero and does not have zero prefix.
92 *
93 */
94 if (E(0) == '0')
97 pos + 1,
98 len - pos - 1
99 );
100
101 /*
102 * Determine whether the position exceeds the end.
103 *
104 */
105 if (pos == len - pos - 1) {
106 size_t i;
107 int compare = 0; /* -1:P<E ; 0:P==E ; 1:P>E */
108 for (i = 0; i < pos && !compare; i++)
109 if (P(i) != E(i))
110 compare = P(i) < E(i) ? -1 : 1;
111 if (compare == 1)
114 0,
115 len
116 );
117 } else if (pos > len - pos - 1)
118 /* Non-zero prefix, so a length check is sufficient. */
121 0,
122 len
123 );
124
126
127}
#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:77
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:62
@ 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