GS1 Syntax Dictionary: Linter reference
A reference to the AI component linter routines referred to by the GS1 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.
53{
54
55/// \cond
56#define P(i) data[i]
57#define E(i) data[i + pos + 1]
58/// \endcond
59
60 size_t pos, len;
61
62 assert(data);
63
64 len = strlen(data);
65
66 /*
67 * Determine that the format is "<pos>/<end>".
68 *
69 */
70 pos = strspn(data, "0123456789");
71 if (pos == 0 || pos >= len - 1 || data[pos] != '/' || strspn(data + pos + 1, "0123456789") != len - pos - 1) {
72 if (err_pos) *err_pos = 0;
73 if (err_len) *err_len = len;
75 }
76
77 /*
78 * Ensure position number is non-zero and does not have zero prefix.
79 *
80 */
81 if (P(0) == '0') {
82 if (err_pos) *err_pos = 0;
83 if (err_len) *err_len = pos;
85 }
86
87 /*
88 * Ensure end number is non-zero and does not have zero prefix.
89 *
90 */
91 if (E(0) == '0') {
92 if (err_pos) *err_pos = pos + 1;
93 if (err_len) *err_len = len - pos - 1;
95 }
96
97 /*
98 * Determine whether the position exceeds the end.
99 *
100 */
101 if (pos == len - pos - 1) {
102 size_t i;
103 int compare = 0; /* -1:P<E ; 0:P==E ; 1:P>E */
104 for (i = 0; i < pos && !compare; i++)
105 if (P(i) != E(i))
106 compare = P(i) < E(i) ? -1 : 1;
107 if (compare == 1) {
108 if (err_pos) *err_pos = 0;
109 if (err_len) *err_len = len;
111 }
112 } else if (pos > len - pos - 1) {
113 /* Non-zero prefix, so a length check is sufficient. */
114 if (err_pos) *err_pos = 0;
115 if (err_len) *err_len = len;
117 }
118
119 return GS1_LINTER_OK;
120
121}
@ GS1_LINTER_ILLEGAL_ZERO_PREFIX
A zero prefix is not permitted.
Definition gs1syntaxdictionary.h:83
@ GS1_LINTER_POSITION_IN_SEQUENCE_MALFORMED
The data must have the format "<pos>/<end>".
Definition gs1syntaxdictionary.h:177
@ GS1_LINTER_OK
No issues were detected by the linter.
Definition gs1syntaxdictionary.h:66
@ GS1_LINTER_POSITION_EXCEEDS_END
The position number must not exceed the end number.
Definition gs1syntaxdictionary.h:178