Dont use static memory for the list of chars options

Saves a couple of KB of RAM and is more flexible in terms
of max number of allowed chars, although for large numbers one really
needs a hash for fast lookups.
This commit is contained in:
Kovid Goyal 2021-06-17 13:27:11 +05:30
parent 6ddbda00df
commit 397638998b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 28 additions and 16 deletions

View File

@ -124,22 +124,28 @@ url_prefixes(PyObject *up, Options *opts) {
}
}
static char_type*
list_of_chars(PyObject *chars) {
if (!PyUnicode_Check(chars)) { PyErr_SetString(PyExc_TypeError, "list_of_chars must be a string"); return NULL; }
char_type *ans = calloc(PyUnicode_GET_LENGTH(chars) + 1, sizeof(char_type));
if (ans) {
for (ssize_t i = 0; i < PyUnicode_GET_LENGTH(chars); i++) {
ans[i] = PyUnicode_READ(PyUnicode_KIND(chars), PyUnicode_DATA(chars), i);
}
}
return ans;
}
static void
url_excluded_characters(PyObject *chars, Options *opts) {
if (!PyUnicode_Check(chars)) { PyErr_SetString(PyExc_TypeError, "url_excluded_characters must be a string"); return; }
for (size_t i = 0; i < MIN((size_t)PyUnicode_GET_LENGTH(chars), arraysz(opts->url_excluded_characters)); i++) {
opts->url_excluded_characters[i] = PyUnicode_READ(PyUnicode_KIND(chars), PyUnicode_DATA(chars), i);
}
opts->url_excluded_characters_count = PyUnicode_GET_LENGTH(chars);
free(opts->url_excluded_characters);
opts->url_excluded_characters = list_of_chars(chars);
}
static void
select_by_word_characters(PyObject *chars, Options *opts) {
if (!PyUnicode_Check(chars)) { PyErr_SetString(PyExc_TypeError, "select_by_word_characters must be a string"); return; }
for (size_t i = 0; i < MIN((size_t)PyUnicode_GET_LENGTH(chars), arraysz(opts->select_by_word_characters)); i++) {
opts->select_by_word_characters[i] = PyUnicode_READ(PyUnicode_KIND(chars), PyUnicode_DATA(chars), i);
}
opts->select_by_word_characters_count = PyUnicode_GET_LENGTH(chars);
free(opts->select_by_word_characters);
opts->select_by_word_characters = list_of_chars(chars);
}
static void

View File

@ -2563,8 +2563,10 @@ screen_selection_range_for_line(Screen *self, index_type y, index_type *start, i
static inline bool
is_opt_word_char(char_type ch) {
for (size_t i = 0; i < OPT(select_by_word_characters_count); i++) {
if (OPT(select_by_word_characters[i]) == ch) return true;
if (OPT(select_by_word_characters)) {
for (const char_type *p = OPT(select_by_word_characters); *p; p++) {
if (ch == *p) return true;
}
}
return false;
}

View File

@ -1091,6 +1091,8 @@ finalize(void) {
free_bgimage(&global_state.bgimage, false);
global_state.bgimage = NULL;
free_url_prefixes();
free(OPT(select_by_word_characters)); OPT(select_by_word_characters) = NULL;
free(OPT(url_excluded_characters)); OPT(url_excluded_characters) = NULL;
}
bool

View File

@ -30,7 +30,7 @@ typedef struct {
unsigned int url_style;
unsigned int scrollback_pager_history_size;
bool scrollback_fill_enlarged_window;
char_type select_by_word_characters[256]; size_t select_by_word_characters_count;
char_type *select_by_word_characters;
color_type url_color, background, foreground, active_border_color, inactive_border_color, bell_border_color;
color_type mark1_foreground, mark1_background, mark2_foreground, mark2_background, mark3_foreground, mark3_background;
monotonic_t repaint_delay, input_delay;
@ -70,7 +70,7 @@ typedef struct {
UrlPrefix *values;
size_t num, max_prefix_len;
} url_prefixes;
char_type url_excluded_characters[256]; size_t url_excluded_characters_count;
char_type *url_excluded_characters;
bool detect_urls;
bool tab_bar_hidden;
double font_size;

View File

@ -14,8 +14,10 @@ combining_type mark_for_codepoint(char_type c);
static inline bool
is_excluded_from_url(uint32_t ch) {
for (size_t i = 0; i < OPT(url_excluded_characters_count); i++) {
if (ch == OPT(url_excluded_characters)[i]) return true;
if (OPT(url_excluded_characters)) {
for (const char_type *p = OPT(url_excluded_characters); *p; p++) {
if (ch == *p) return true;
}
}
return false;
}