fix find_nth if multiple / leading separators are present

This commit is contained in:
Stefan Frederik 2022-02-13 12:12:37 +01:00
parent 13aca67a4d
commit b697ec448d
3 changed files with 21 additions and 9 deletions

View File

@ -741,7 +741,14 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
}
else if(argv[1][0] == 'f') {
if(!strcmp(argv[1],"flip"))
if(!strcmp(argv[1],"find_nth"))
{
cmd_found = 1;
if(argc > 4) {
Tcl_SetResult(interp, find_nth(argv[2], argv[3], atoi(argv[4])), TCL_VOLATILE);
}
}
else if(!strcmp(argv[1],"flip"))
{
cmd_found = 1;
if(! (xctx->ui_state & (STARTMOVE | STARTCOPY) ) ) {

View File

@ -2698,11 +2698,10 @@ int isonlydigit(const char *s)
/* find nth occurrence of substring in str separated by sep. 1st substring is position 1
* find_nth("aaa,bbb,ccc,ddd", ',', 2) --> "bbb"
*/
const char *find_nth(const char *str, char *sep, int n)
char *find_nth(const char *str, const char *sep, int n)
{
static char *result=NULL; /* safe to keep even with multiple schematic windows */
static int result_size = 0; /* safe to keep even with multiple schematic windows */
static const char *empty="";
int i, len;
char *ptr;
int count;
@ -2710,7 +2709,7 @@ const char *find_nth(const char *str, char *sep, int n)
if(!str) {
my_free(1062, &result);
result_size = 0;
return empty;
return NULL;
}
len = strlen(str) + 1;
if(len > result_size) {
@ -2718,19 +2717,25 @@ const char *find_nth(const char *str, char *sep, int n)
my_realloc(138, &result, result_size);
}
memcpy(result, str, len);
for(i=0, count=1, ptr=result; result[i] != 0; i++) {
i = 0;
while(result[i] && strchr(sep, result[i])) i++; /* strip off leading separators */
ptr = result + i;
for(count=1; result[i] != 0; i++) {
if(strchr(sep, result[i])) {
result[i]=0;
if(count==n) {
return ptr;
}
while(strchr(sep, result[++i])) ;
ptr=result+i;
while(result[++i] && strchr(sep, result[i])) ;
ptr = result + i;
count++;
}
}
if(count==n) return ptr;
else return empty;
else {
result[0] = '\0';
return result;
}
}
/* substitute given tokens in a string with their corresponding values */

View File

@ -1292,7 +1292,7 @@ extern void int_hash_free(Int_hashentry **table);
extern Int_hashentry *int_hash_lookup(Int_hashentry **table,
const char *token, const int value, int what);
extern const char *find_nth(const char *str, char *sep, int n);
extern char *find_nth(const char *str, const char *sep, int n);
extern int isonlydigit(const char *s);
extern const char *translate(int inst, const char* s);
extern const char* translate2(Lcc *lcc, int level, char* s);