From 751d377d3ed603e541e0162e654f299715269043 Mon Sep 17 00:00:00 2001 From: rlar Date: Tue, 20 Feb 2018 21:12:14 +0100 Subject: [PATCH] pscopy(), pscopy_up(), use `char *stop' argument instead of `int length' argument --- src/frontend/numparam/general.h | 4 +- src/frontend/numparam/mystring.c | 65 +++++++++----------------------- src/frontend/numparam/spicenum.c | 8 ++-- src/frontend/numparam/xpressn.c | 12 +++--- 4 files changed, 30 insertions(+), 59 deletions(-) diff --git a/src/frontend/numparam/general.h b/src/frontend/numparam/general.h index d0ba75fa0..135dce80d 100644 --- a/src/frontend/numparam/general.h +++ b/src/frontend/numparam/general.h @@ -9,8 +9,8 @@ #include "ngspice/bool.h" -char *pscopy(SPICE_DSTRINGPTR s, const char *str, int leng); -char *pscopy_up(SPICE_DSTRINGPTR s, const char *str, int leng); +char *pscopy(SPICE_DSTRINGPTR s, const char *str, const char *stop); +char *pscopy_up(SPICE_DSTRINGPTR s, const char *str, const char *stop); void scopyd(SPICE_DSTRINGPTR a, SPICE_DSTRINGPTR b); void scopys(SPICE_DSTRINGPTR a, const char *b); void scopy_up(SPICE_DSTRINGPTR a, const char *str); diff --git a/src/frontend/numparam/mystring.c b/src/frontend/numparam/mystring.c index 31a274d6b..529e0205f 100644 --- a/src/frontend/numparam/mystring.c +++ b/src/frontend/numparam/mystring.c @@ -168,69 +168,40 @@ scopy_lower(SPICE_DSTRINGPTR dstr_p, const char *str) /* returns success flag */ char * -pscopy(SPICE_DSTRINGPTR dstr_p, const char *t, int leng) -/* partial string copy, with C-based start - Because we now have a 0 based - * start and string may copy outselves, we may need to restore the first - * character of the original dstring because resetting string will wipe - * out first character. */ +pscopy(SPICE_DSTRINGPTR dstr_p, const char *t, const char *stop) { - int i; /* counter */ - int stop; /* stop value */ - char *s_p; /* value of dynamic string */ + int i; + char *s_p; - stop = (int) strlen(t); + if (!stop) + stop = strchr(t, '\0'); - if (0 < stop) { /* nothing! */ + s_p = _spice_dstring_setlength(dstr_p, (int)(stop - t)); - if (leng > stop) - leng = stop; + for (i = 0; t < stop;) + s_p[i++] = *t++; - s_p = _spice_dstring_setlength(dstr_p, leng); - - for (i = 0; i < leng; i++) - s_p[i] = t[i]; - - s_p[leng] = '\0'; - - } else { - - s_p = _spice_dstring_setlength(dstr_p, 0); - s_p[0] = '\0'; - - } + s_p[i] = '\0'; return s_p; } char * -pscopy_up(SPICE_DSTRINGPTR dstr_p, const char *t, int leng) -/* partial string copy to upper case, with C convention for start. */ +pscopy_up(SPICE_DSTRINGPTR dstr_p, const char *t, const char *stop) { - int i; /* counter */ - int stop; /* stop value */ - char *s_p; /* value of dynamic string */ + int i; + char *s_p; - stop = (int) strlen(t); + if (!stop) + stop = strchr(t, '\0'); - if (0 < stop) { /* nothing! */ + s_p = _spice_dstring_setlength(dstr_p, (int)(stop - t)); - if (leng > stop) - leng = stop; + for (i = 0; t < stop;) + s_p[i++] = toupper_c(*t++); - s_p = _spice_dstring_setlength(dstr_p, leng); - - for (i = 0; i < leng; i++) - s_p[i] = toupper_c(t[i]); - - s_p[leng] = '\0'; - - } else { - - s_p = _spice_dstring_setlength(dstr_p, 0); - s_p[0] = '\0'; - - } + s_p[i] = '\0'; return s_p; } diff --git a/src/frontend/numparam/spicenum.c b/src/frontend/numparam/spicenum.c index 8415b5c73..1b240e634 100644 --- a/src/frontend/numparam/spicenum.c +++ b/src/frontend/numparam/spicenum.c @@ -84,7 +84,7 @@ stripsomespace(SPICE_DSTRINGPTR dstr_p, bool incontrol) i++; if ((i > 0) && s[i] && strchr(markers, s[i])) - pscopy(dstr_p, s + i, (int) strlen(s + i)); + pscopy(dstr_p, s + i, NULL); } @@ -115,7 +115,7 @@ stripbraces(SPICE_DSTRINGPTR dstr_p) j_ptr++; } - pscopy(&tstr, s, (int) (brace - s)); + pscopy(&tstr, s, brace); if (brace[-1] > ' ') cadd(&tstr, ' '); @@ -303,7 +303,7 @@ transform(dico_t *dico, SPICE_DSTRINGPTR dstr_p, bool incontrol) /* split off any "params" tail */ params = strstr(t, "PARAMS:"); if (params) - pscopy(dstr_p, s, (int)(params - t)); + pscopy(dstr_p, s, s + (params - t)); spice_dstring_free(&tstr); category = 'S'; } else if (ci_prefix(".CONTROL", s)) { @@ -705,7 +705,7 @@ nupa_copy(struct card *deck) while ((ls > 0) && (s[ls - 1] <= ' ')) ls--; - pscopy(&u, s, ls); /* strip trailing space, CrLf and so on */ + pscopy(&u, s, s + ls); /* strip trailing space, CrLf and so on */ dicoS->srcline = linenum; if ((!inexpansionS) && (linenum >= 0) && (linenum <= dynmaxline)) { diff --git a/src/frontend/numparam/xpressn.c b/src/frontend/numparam/xpressn.c index 515cd2a53..6a0d244d2 100644 --- a/src/frontend/numparam/xpressn.c +++ b/src/frontend/numparam/xpressn.c @@ -532,7 +532,7 @@ defsubckt(dico_t *dico, struct card *card, nupa_type categ) if (s_end > s) { SPICE_DSTRING ustr; /* temp user string */ spice_dstring_init(&ustr); - pscopy_up(&ustr, s, (int) (s_end - s)); + pscopy_up(&ustr, s, s_end); err = nupa_define(dico, spice_dstring_value(&ustr), ' ', categ, 0.0, w, NULL); spice_dstring_free(&ustr); } else { @@ -556,7 +556,7 @@ findsubckt(dico_t *dico, const char * const s) spice_dstring_init(&ustr); - pscopy_up(&ustr, name_b, (int) (name_e - name_b)); + pscopy_up(&ustr, name_b, name_e); entry = entrynb(dico, spice_dstring_value(&ustr)); if (entry && (entry->tp == NUPA_SUBCKT)) { @@ -1383,7 +1383,7 @@ getexpress(nupa_type *type, SPICE_DSTRINGPTR tstr_p, const char *s) tpe = NUPA_REAL; } - pscopy(tstr_p, s, (int) (p - s)); + pscopy(tstr_p, s, p); if (*p == '}') p++; @@ -1547,7 +1547,7 @@ nupa_subcktcall(dico_t *dico, char *s, char * const x, char * const inst_name) if (i2) { const char *optr, *jptr; - pscopy(&tstr, i2 + 7, (int) strlen(i2 + 7)); + pscopy(&tstr, i2 + 7, NULL); /* search identifier to the left of '=' assignments */ @@ -1652,7 +1652,7 @@ nupa_subcktcall(dico_t *dico, char *s, char * const x, char * const inst_name) if (alfanum(*kp) || *kp == '.') { /* number, identifier */ jp = skip_non_ws(kp); - pscopy(&ustr, kp, (int) (jp - kp)); + pscopy(&ustr, kp, jp); } else if (*kp == '{') { jp = getexpress(NULL, &ustr, jp); } else { @@ -1667,7 +1667,7 @@ nupa_subcktcall(dico_t *dico, char *s, char * const x, char * const inst_name) char *dollar = strchr(idlist_p, '$'); if (dollar) { /* replace dollar with expression string u */ - pscopy(&vstr, idlist_p, (int) (dollar - idlist_p)); + pscopy(&vstr, idlist_p, dollar); sadd(&vstr, u_p); sadd(&vstr, dollar + 1); scopyd(&idlist, &vstr);