Move Yang patterns: \n match from yang parse to regex compile stage

This commit is contained in:
Olof hagsand 2021-08-05 12:53:26 +02:00
parent 3ed41c5a04
commit 9aad253f1e
4 changed files with 10 additions and 90 deletions

View file

@ -100,7 +100,6 @@ int xml_chardata_encode(char **escp, const char *fmt, ...);
#endif
int xml_chardata_cbuf_append(cbuf *cb, char *str);
int uri_percent_decode(char *enc, char **str);
int nonprint_encode(char *orig, char **encp);
const char *clicon_int2str(const map_str2int *mstab, int i);
int clicon_str2int(const map_str2int *mstab, char *str);

View file

@ -89,6 +89,7 @@
* \p{Z} Separators [slp]?
* \p{S} Symbols [mcko]?
* \p{O} Other [cfon]?
* For non-printable, \n, \t, \r see https://www.regular-expressions.info/nonprint.html
*/
int
regexp_xsd2posix(char *xsd,
@ -124,6 +125,9 @@ regexp_xsd2posix(char *xsd,
case 'i': /* initial */
cprintf(cb, "[a-zA-Z_:]");
break;
case 'n': /* non-printable \n */
cprintf(cb, "\n");
break;
case 'p': /* category escape: \p{IsCategory} */
j = i+1;
if (j+2 < strlen(xsd) &&
@ -161,12 +165,18 @@ regexp_xsd2posix(char *xsd,
}
/* if syntax error, just leave it */
break;
case 'r': /* non-printable */
cprintf(cb, "\r");
break;
case 's':
cprintf(cb, "[ \t\r\n]");
break;
case 'S':
cprintf(cb, "[^ \t\r\n]");
break;
case 't': /* non-printable */
cprintf(cb, "\t");
break;
case 'w': /* word */
//cprintf(cb, "[0-9a-zA-Z_\\\\-]")
cprintf(cb, "[^[:punct:][:space:][:cntrl:]]");

View file

@ -646,83 +646,6 @@ uri_str2cvec(char *string,
goto done;
}
/*! Translate \n and others from \\n (two chars) to \n (one char)
*
* This is needed in yang regex it seems.
* It was triggered by eg draft-wwlh-netconf-list-pagination-00 module example-social tagline
* leaf tagline {
* type string {
* length "1..80";
* pattern '.*[\n].*' {
* modifier invert-match;
* @param[in] orig Original string eg with \\n
* @param[out] enc Encoded string with \n, malloced.
*
* @see https://www.regular-expressions.info/nonprint.html
*/
int
nonprint_encode(char *orig,
char **encp)
{
int retval = -1;
char *enc = NULL;
int i;
int j;
int esc = 0;
char c;
char c1;
if (orig == NULL){
clicon_err(OE_UNIX, EINVAL, "orig is NULL");
goto done;
}
/* Encoded string is equal or shorter */
if ((enc = malloc(strlen(orig)+1)) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
j = 0;
for (i=0; i<strlen(orig); i++){
c = orig[i];
switch (c){
case '\\':
/* Look ahead */
if (i+1 < strlen(orig)){
c1 = orig[i+1];
switch (c1){
case 'n':
enc[j++] = '\n';
esc++;
break;
case 't':
enc[j++] = '\t';
esc++;
break;
case 'r':
enc[j++] = '\r';
esc++;
break;
default:
enc[j++] = c;
break;
}
}
break;
default:
if (esc)
esc = 0;
else
enc[j++] = c;
break;
}
}
enc[j++] = '\0';
*encp = enc;
retval = 0;
done:
return retval;
}
/*! Map from int to string using str2int map
* @param[in] ms String, integer map
* @param[in] i Input integer

View file

@ -1896,18 +1896,6 @@ ys_parse_sub(yang_stmt *ys,
goto done;
}
break;
case Y_PATTERN:{
char *s0;
char *s1 = NULL;
/* Replace \\n with \n */
s0 = yang_argument_get(ys);
if (nonprint_encode(s0, &s1) < 0)
goto done;
yang_argument_set(ys, s1);
if (s0)
free(s0);
break;
}
case Y_UNKNOWN:{ /* save (optional) argument in ys_cv */
if (extra == NULL)
break;