trim_chars() function for various string operations

This commit is contained in:
stefan schippers 2023-10-11 16:31:42 +02:00
parent 9b4bd44fd0
commit e457292e04
5 changed files with 52 additions and 4 deletions

View File

@ -3163,8 +3163,8 @@ void draw_graph(int i, const int flags, Graph_ctx *gr, void *ct)
/* process each node given in "node" attribute, get also associated color/sweep var if any*/
while( (ntok = my_strtok_r(nptr, "\n\t ", "\"", 4, &saven)) ) {
if(strstr(ntok, ",")) {
my_strdup2(_ALLOC_ID_, &bus_msb, find_nth(ntok, ";,", "\"", 0, 2));
my_strdup2(_ALLOC_ID_, &bus_msb, find_nth(bus_msb, " ", "", 0, 1)); /* chop spaces */
/* also trim spaces */
my_strdup2(_ALLOC_ID_, &bus_msb, trim_chars(find_nth(ntok, ";,", "\"", 0, 2), " "));
}
dbg(1, "ntok=|%s|, bus_msb=|%s|\n", ntok, bus_msb ? bus_msb : "NULL");
ctok = my_strtok_r(cptr, " ", "", 0, &savec);

View File

@ -2493,7 +2493,7 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
if(argc > 6) {
int i;
if((i = get_instance(argv[2])) < 0 ) {
Tcl_SetResult(interp, "xschem pinlist: instance not found", TCL_STATIC);
Tcl_SetResult(interp, "xschem move_instance: instance not found", TCL_STATIC);
return TCL_ERROR;
}
if(undo) xctx->push_undo();
@ -4588,6 +4588,16 @@ int xschem(ClientData clientdata, Tcl_Interp *interp, int argc, const char * arg
}
}
/* trim_chars str sep
* Remove leading and trailing chars matching any character in 'sep' from str */
else if(!strcmp(argv[1], "trim_chars"))
{
if(argc > 3) {
char *s = trim_chars(argv[2], argv[3]);
Tcl_SetResult(interp, s, TCL_VOLATILE);
}
}
/* trim_wires
* Remove operlapping wires, join lines, trim wires at intersections */
else if(!strcmp(argv[1], "trim_wires"))

View File

@ -3185,6 +3185,43 @@ int isonlydigit(const char *s)
return 1;
}
/* remove leading and trailing characters specified in 'sep' */
char *trim_chars(const char *str, const char *sep)
{
static char *result = NULL;
static size_t result_size = 0;
size_t len;
char *ptr;
char *last;
if(str == NULL) {
my_free(_ALLOC_ID_, &result);
result_size = 0;
return NULL;
}
len = strlen(str) + 1;
/* allocate storage for result */
if(len > result_size) {
result_size = len + CADCHUNKALLOC;
my_realloc(_ALLOC_ID_, &result, result_size);
}
memcpy(result, str, len);
if(*str == '\0')return result;
ptr = result;
while (*ptr) {
if(!strchr(sep, *ptr)) break;
ptr++;
}
if(*ptr == '\0') return ptr;
last = ptr + strlen(ptr) -1;
while(strchr(sep, *last) && last > result) {
last--;
}
last[1] = '\0';
return ptr;
}
/* find nth field in str separated by sep. 1st field is position 1
* separators inside quotes are not considered as field separators
* if keep_quote == 1 keep quoting characters and backslashes in returned field

View File

@ -894,6 +894,7 @@ static void xwin_exit(void)
translate2(NULL, 0, NULL); /* clear static data in function */
subst_token(NULL, NULL, NULL); /* clear static data in function */
find_nth(NULL, "", "", 0, 0); /* clear static data in function */
trim_chars(NULL, ""); /* clear static data in function */
tcl_hook2(NULL); /* clear static data in function */
save_ascii_string(NULL, NULL, 0); /* clear static data in function */
dbg(1, "xwin_exit(): removing font\n");

View File

@ -1480,7 +1480,7 @@ extern void ptr_hash_init(Ptr_hashtable *hashtable, int size);
extern void ptr_hash_free(Ptr_hashtable *hashtable);
extern Ptr_hashentry *ptr_hash_lookup(Ptr_hashtable *hashtable,
const char *token, void * const value, int what);
extern char *trim_chars(const char *str, const char *sep);
extern char *find_nth(const char *str, const char *sep, const char *quote, int keep_quote, int n);
extern int isonlydigit(const char *s);
extern const char *translate(int inst, const char* s);