Skip to content

Commit

Permalink
Allocate methods list using malloc, in case there are many methods
Browse files Browse the repository at this point in the history
  • Loading branch information
simonster committed Sep 26, 2013
1 parent 320acab commit ad4b77c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ui/repl-readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ static int space_callback(int count, int key)
return 0;
}

#define MAX_METHODS 100
int complete_method_table() {
if (rl_point < 2 || rl_line_buffer[rl_point-1] != '(') return 0;

Expand All @@ -264,15 +263,20 @@ int complete_method_table() {

if (!jl_is_byte_string(result)) return 0;
char *completions = jl_string_data(result);
char *methods[MAX_METHODS+1];

int nallocmethods = 0;
size_t nchars = strlen(completions);
for (size_t i=0; i<nchars; i++) nallocmethods += completions[i] == '\n';

char **methods = malloc(sizeof(char *)*(nallocmethods+1));
methods[0] = NULL;
methods[1] = strtok_r(completions, "\n", &strtok_saveptr);
if (methods[1] == NULL) return 0;
int maxlen = strlen(methods[1]);
int nmethods = 1;

char *method;
while (nmethods < MAX_METHODS &&
while (nmethods < nallocmethods &&
(method = strtok_r(NULL, "\n", &strtok_saveptr))) {
int len = strlen(method);
if (len > maxlen) maxlen = len;
Expand All @@ -281,6 +285,7 @@ int complete_method_table() {
}

rl_display_match_list(methods, nmethods, maxlen);
free(methods);
rl_forced_update_display();
return 1;
}
Expand Down

0 comments on commit ad4b77c

Please sign in to comment.