Skip to content

Commit

Permalink
stop performing atoi() repeatedly on config values
Browse files Browse the repository at this point in the history
Now that we store both the string and the numeric value for every config
entries, let's grab those values directly. This should save quite some
processing cycles.

Those changes were performed with the following command:

sed -e 's/atoi(get_conf_value *(\([^)]*\)))/get_conf_int(\1)/g' -i *.c *.y
  • Loading branch information
npitre committed Mar 21, 2021
1 parent 5ab0f20 commit 8657daa
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ int save_plain(FILE * fout, int r0, int c0, int rn, int cn) {
if(emptyfield){
fwprintf(fout, L"\t");
}
if (! atoi(get_conf_value("copy_to_clipboard_delimited_tab"))) {
if (! get_conf_int("copy_to_clipboard_delimited_tab")) {
pad_and_align(text, num, fwidth[col], align, 0, out);
fwprintf(fout, L"%ls", out);
} else if ( (*pp)->flags & is_valid) {
fwprintf(fout, L"%s\t", num);
} else if ( (*pp)->label) {
fwprintf(fout, L"%s\t", text);
}
} else if (! atoi(get_conf_value("copy_to_clipboard_delimited_tab"))) {
} else if (! get_conf_int("copy_to_clipboard_delimited_tab")) {
fwprintf(fout, L"%*s", fwidth[col], " ");
} else {
fwprintf(fout, L"\t");
Expand Down
14 changes: 7 additions & 7 deletions src/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ void doformat(int c1, int c2, int w, int p, int r) {
w = 1;
}

if (! atoi(get_conf_value("nocurses")) && w > COLS - rescol - 2) {
if (! get_conf_int("nocurses") && w > COLS - rescol - 2) {
sc_info("Width too large - Maximum = %d", COLS - rescol - 2);
w = COLS - rescol - 2;
}
Expand Down Expand Up @@ -950,7 +950,7 @@ void int_deleterow(int row, int mult) {
}
rebuild_graph(); //FIXME CHECK HERE WHY REBUILD IS NEEDED. See NOTE1 in shift.c
sync_refs();
//if (atoi(get_conf_value("autocalc")) && ! loading) EvalAll();
//if (get_conf_int("autocalc") && ! loading) EvalAll();
EvalAll();
maxrow--;
}
Expand Down Expand Up @@ -1218,7 +1218,7 @@ void enter_cell_content(int r, int c, char * submode, wchar_t * content) {
*/

void send_to_interp(wchar_t * oper) {
if (atoi(get_conf_value("nocurses"))) {
if (get_conf_int("nocurses")) {
int pos = -1;
if ((pos = wstr_in_wstr(oper, L"\n")) != -1)
oper[pos] = L'\0';
Expand All @@ -1231,7 +1231,7 @@ void send_to_interp(wchar_t * oper) {
yyparse();
linelim = -1;
line[0]='\0';
if (atoi(get_conf_value("autocalc")) && ! loading) EvalAll();
if (get_conf_int("autocalc") && ! loading) EvalAll();
return;
}

Expand Down Expand Up @@ -2532,7 +2532,7 @@ void pad_and_align (char * str_value, char * numeric_value, int col_width, int a
}

// If content exceedes column width, outputs n number of '*' needed to fill column width
if (str_len + num_len + padding > col_width && !atoi(get_conf_value("truncate")) && ( (! atoi(get_conf_value("overlap")))) ) {
if (str_len + num_len + padding > col_width && !get_conf_int("truncate") && ( (! get_conf_int("overlap"))) ) {
if (padding) wmemset(str_out + wcslen(str_out), L'#', padding);
wmemset(str_out + wcslen(str_out), L'*', col_width - padding);
return;
Expand Down Expand Up @@ -2573,7 +2573,7 @@ void pad_and_align (char * str_value, char * numeric_value, int col_width, int a
}

// Similar condition to max width '*' condition above, but just trims instead
if (str_len + num_len + padding > col_width && atoi(get_conf_value("truncate"))) {
if (str_len + num_len + padding > col_width && get_conf_int("truncate")) {
str_out[col_width] = '\0';
}

Expand Down Expand Up @@ -2679,7 +2679,7 @@ int is_single_command (struct block * buf, long timeout) {
else if (buf->value == L'-') result = EDITION_CMD;
else if (buf->value == L'+') result = EDITION_CMD;

else if (isdigit(buf->value) && atoi(get_conf_value("numeric")) )
else if (isdigit(buf->value) && get_conf_int("numeric") )
result = MOVEMENT_CMD; // repeat last command

else if (buf->value == L'.') result = MOVEMENT_CMD; // repeat last command
Expand Down
2 changes: 1 addition & 1 deletion src/cmds_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ void do_commandmode(struct block * sb) {
create_structures();
readfile(name, 0);

if (! atoi(get_conf_value("nocurses"))) {
if (! get_conf_int("nocurses")) {
ui_show_header();
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/cmds_normal.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void do_normalmode(struct block * buf) {
break;

case L'0':
if (atoi(get_conf_value("numeric_zero")) == 1 && atoi(get_conf_value("numeric")) == 1) goto numeric;
if (get_conf_int("numeric_zero") == 1 && get_conf_int("numeric") == 1) goto numeric;
case OKEY_HOME:
;
int freeze = freeze_ranges && (freeze_ranges->type == 'c' || freeze_ranges->type == 'a') ? 1 : 0;
Expand Down Expand Up @@ -262,7 +262,7 @@ void do_normalmode(struct block * buf) {
case OKEY_PGDOWN:
{
int n = LINES - RESROW - 1;
if (atoi(get_conf_value("half_page_scroll"))) n = n / 2;
if (get_conf_int("half_page_scroll")) n = n / 2;
lastcol = curcol;
lastrow = currow;
currow = forw_row(n)->row;
Expand All @@ -277,7 +277,7 @@ void do_normalmode(struct block * buf) {
case OKEY_PGUP:
{
int n = LINES - RESROW - 1;
if (atoi(get_conf_value("half_page_scroll"))) n = n / 2;
if (get_conf_int("half_page_scroll")) n = n / 2;
lastcol = curcol;
lastrow = currow;
currow = back_row(n)->row;
Expand Down Expand Up @@ -435,7 +435,7 @@ void do_normalmode(struct block * buf) {

// repeat last command
case L'.':
if (atoi(get_conf_value("numeric_decimal")) == 1 && atoi(get_conf_value("numeric")) == 1) goto numeric;
if (get_conf_int("numeric_decimal") == 1 && get_conf_int("numeric") == 1) goto numeric;
copybuffer(lastcmd_buffer, buf); // nose graba en lastcmd_buffer!!
cmd_multiplier = 1;
exec_mult(buf, COMPLETECMDTIMEOUT);
Expand Down Expand Up @@ -640,7 +640,7 @@ void do_normalmode(struct block * buf) {
#endif
}

//if (atoi(get_conf_value("autocalc"))) EvalAll();
//if (get_conf_int("autocalc")) EvalAll();
ui_update(TRUE);
break;
}
Expand Down Expand Up @@ -737,7 +737,7 @@ void do_normalmode(struct block * buf) {

} else if (buf->pnext->value == L'd') {
del_selected_cells();
if (atoi(get_conf_value("autocalc")) && ! loading) EvalAll();
if (get_conf_int("autocalc") && ! loading) EvalAll();
}

ui_update(TRUE);
Expand Down Expand Up @@ -920,13 +920,13 @@ void do_normalmode(struct block * buf) {

case L'H':
scroll = calc_offscr_sc_cols() - center_hidden_cols;
if (atoi(get_conf_value("half_page_scroll"))) scroll /= 2;
if (get_conf_int("half_page_scroll")) scroll /= 2;
scroll_left(scroll);
break;

case L'L':
scroll = calc_offscr_sc_cols() - center_hidden_cols;
if (atoi(get_conf_value("half_page_scroll"))) scroll /= 2;
if (get_conf_int("half_page_scroll")) scroll /= 2;
scroll_right(scroll);
break;

Expand Down Expand Up @@ -1106,7 +1106,7 @@ void do_normalmode(struct block * buf) {
sc_error("Locked cells encountered. Nothing changed");
return;
}
if (atoi(get_conf_value("numeric")) == 1) goto numeric;
if (get_conf_int("numeric") == 1) goto numeric;
struct ent * p;
#ifdef UNDO
create_undo_action();
Expand Down Expand Up @@ -1136,7 +1136,7 @@ void do_normalmode(struct block * buf) {
#ifdef UNDO
end_undo_action();
#endif
if (atoi(get_conf_value("autocalc"))) EvalAll();
if (get_conf_int("autocalc")) EvalAll();
cmd_multiplier = 0;
ui_update(TRUE);
}
Expand All @@ -1146,8 +1146,8 @@ void do_normalmode(struct block * buf) {
default:
numeric:
if ( (isdigit(buf->value) || buf->value == L'-' || buf->value == L'+' ||
( buf->value == L'.' && atoi(get_conf_value("numeric_decimal")) )) &&
atoi(get_conf_value("numeric")) ) {
( buf->value == L'.' && get_conf_int("numeric_decimal") )) &&
get_conf_int("numeric") ) {
if (locked_cell(currow, curcol)) return;
insert_edit_submode='=';
chg_mode(insert_edit_submode);
Expand Down
4 changes: 2 additions & 2 deletions src/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ int redefine_color(char * color, int r, int g, int b) {
#if defined(NCURSES) && defined(USECOLORS)
extern void sig_winchg();
if (
! atoi(get_conf_value("nocurses"))
! get_conf_int("nocurses")
&& has_colors() && can_change_color()
) {
char * s = get(d_colors_param, color);
Expand Down Expand Up @@ -604,7 +604,7 @@ int redefine_color(char * color, int r, int g, int b) {
int define_color(char * color, int r, int g, int b) {

#if defined(NCURSES) && defined(USECOLORS)
if (atoi(get_conf_value("nocurses"))) {
if (get_conf_int("nocurses")) {
// this should not be alerted.
//sc_error("Could not define color %s. Not using NCURSES.", color);
return -1;
Expand Down
14 changes: 7 additions & 7 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int file_exists(const char * fname) {
*/

int modcheck() {
if (modflg && ! atoi(get_conf_value("nocurses"))) {
if (modflg && ! get_conf_int("nocurses")) {
sc_error("File not saved since last change. Add '!' to force");
return(1);
}
Expand Down Expand Up @@ -947,7 +947,7 @@ void closefile(FILE *f, int pid, int rfd) {
} else {
close(rfd);
#ifdef NCURSES
if (! atoi(get_conf_value("nocurses"))) {
if (! get_conf_int("nocurses")) {
cbreak();
nonl();
noecho ();
Expand Down Expand Up @@ -975,7 +975,7 @@ void print_options(FILE *f) {
! rndtoeven &&
calc_order == BYROWS &&
prescale == 1.0 &&
! atoi(get_conf_value("external_functions")) &&
! get_conf_int("external_functions") &&
tbl_style == 0
)
return; // No reason to do this
Expand All @@ -985,7 +985,7 @@ void print_options(FILE *f) {
if (rndtoeven) (void) fprintf(f, " rndtoeven");
if (calc_order != BYROWS ) (void) fprintf(f, " bycols");
if (prescale != 1.0) (void) fprintf(f, " prescale");
if ( atoi(get_conf_value("external_functions")) ) (void) fprintf(f, " external_functions");
if ( get_conf_int("external_functions") ) (void) fprintf(f, " external_functions");
if (tbl_style) (void) fprintf(f, " tblstyle = %s", tbl_style == TBL ? "tbl" : tbl_style == LATEX ? "latex" : tbl_style == SLATEX ? "slatex" : tbl_style == TEX ? "tex" : tbl_style == FRAME ? "frame" : "0" );
(void) fprintf(f, "\n");
}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ int import_csv(char * fname, char d) {
char * st = str_replace (token, "\"", "''"); //replace double quotes inside string

// number import
if (strlen(st) && isnumeric(st) && ! atoi(get_conf_value("import_delimited_as_text"))
if (strlen(st) && isnumeric(st) && ! get_conf_int("import_delimited_as_text")
) {
//wide char
swprintf(line_interp, BUFFERSIZE, L"let %s%d=%s", coltoa(c), r, st);
Expand Down Expand Up @@ -1645,7 +1645,7 @@ void * do_autobackup() {
add_char(name, '.', pos+1);
sprintf(name + strlen(name), ".bak");
sprintf(namenew, "%.*s.new", PATHLEN-5, name);
//if (atoi(get_conf_value("debug"))) sc_info("doing autobackup of file:%s", name);
//if (get_conf_int("debug")) sc_info("doing autobackup of file:%s", name);

// create new version
if (! strcmp(&name[strlen(name)-7], ".sc.bak")) {
Expand Down Expand Up @@ -1684,7 +1684,7 @@ void handle_backup() {
extern struct timeval lastbackup_tv; // last backup timer
extern struct timeval current_tv; //runtime timer

int autobackup = atoi(get_conf_value ("autobackup"));
int autobackup = get_conf_int("autobackup");
if (autobackup && autobackup > 0 && (current_tv.tv_sec - lastbackup_tv.tv_sec > autobackup || (lastbackup_tv.tv_sec == 0 && lastbackup_tv.tv_usec == 0))) {
#ifdef HAVE_PTHREAD
if (pthread_exists) pthread_join (fthread, NULL);
Expand Down
8 changes: 4 additions & 4 deletions src/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ command:
| S_CELLCOLOR var_or_range STRING
{
#ifdef USECOLORS
if ( ! atoi(get_conf_value("nocurses")))
if ( ! get_conf_int("nocurses"))
color_cell($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col, $3);
#endif
scxfree($3);
Expand All @@ -652,21 +652,21 @@ command:

| S_CELLCOLOR STRING {
#ifdef USECOLORS
if ( ! atoi(get_conf_value("nocurses")))
if ( ! get_conf_int("nocurses"))
color_cell(currow, curcol, currow, curcol, $2);
#endif
scxfree($2);
}

| S_UNFORMAT var_or_range {
#ifdef USECOLORS
if ( ! atoi(get_conf_value("nocurses"))) unformat($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col);
if ( ! get_conf_int("nocurses")) unformat($2.left.vp->row, $2.left.vp->col, $2.right.vp->row, $2.right.vp->col);
#endif
}

| S_UNFORMAT {
#ifdef USECOLORS
if ( ! atoi(get_conf_value("nocurses"))) unformat(currow, curcol, currow, curcol);
if ( ! get_conf_int("nocurses")) unformat(currow, curcol, currow, curcol);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void handle_input(struct block * buffer) {
&& ( buffer->value == L'\0' || iswdigit((wchar_t) buffer->value))
&& ( curmode == NORMAL_MODE || curmode == VISUAL_MODE || curmode == EDIT_MODE )
&& ( cmd_multiplier || d != L'0' )
&& ( ! atoi(get_conf_value("numeric")))
&& ( ! get_conf_int("numeric"))
) {
cmd_multiplier *= 10;
cmd_multiplier += (int) (d - '0');
Expand Down
4 changes: 2 additions & 2 deletions src/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ char * doext(struct enode *se) {

command = seval(NULL, se->e.o.left);
value = eval(NULL, se->e.o.right);
if ( ! atoi(get_conf_value("external_functions")) ) {
if ( ! get_conf_int("external_functions") ) {
sc_error("Warning: external functions disabled; using %s value",
(se->e.o.s && *se->e.o.s) ? "previous" : "null");

Expand Down Expand Up @@ -2182,7 +2182,7 @@ void str_search(char *s, int firstrow, int firstcol, int lastrow_, int lastcol_,
regex_t preg;
int errcode;

if ( atoi(get_conf_value("ignorecase")))
if ( get_conf_int("ignorecase"))
errcode = regcomp(&preg, s, REG_EXTENDED | REG_ICASE);
else
errcode = regcomp(&preg, s, REG_EXTENDED);
Expand Down
4 changes: 2 additions & 2 deletions src/shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void shift(int r, int c, int rf, int cf, wchar_t type) {
copy_to_undostruct(deps[i].vp->row, deps[i].vp->col, deps[i].vp->row, deps[i].vp->col, 'd');
#endif
while (ic--) shift_range(-ic, 0, r, c, rf, cf);
if (atoi(get_conf_value("autocalc")) && ! loading) EvalAll();
if (get_conf_int("autocalc") && ! loading) EvalAll();
#ifdef UNDO
// update(TRUE); this is used just to make debugging easier
for (i = 0; deps != NULL && i < deps->vf; i++) // TODO here save just ents that are off the shifted range
Expand All @@ -128,7 +128,7 @@ void shift(int r, int c, int rf, int cf, wchar_t type) {
#endif
while (ic--) shift_range(0, -ic, r, c, rf, cf);

if (atoi(get_conf_value("autocalc")) && ! loading) EvalAll();
if (get_conf_int("autocalc") && ! loading) EvalAll();
//update(TRUE); // this is used just to make debugging easier
#ifdef UNDO
for (i = 0; deps != NULL && i < deps->vf; i++) {
Expand Down
Loading

0 comments on commit 8657daa

Please sign in to comment.