mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-24 08:16:22 +00:00
autocomplete: Use asprintf don't calculate length twice
Through asprintf() we can get rid of malloc() + sprintf(). Also we don't need to calculate the strlen() again since asprintf() returns the bytes printes. Only non UTF-8 characters. But that was true before already.
This commit is contained in:
@@ -33,6 +33,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _GNU_SOURCE
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -303,11 +304,15 @@ autocomplete_complete(Autocomplete ac, const gchar* search_str, gboolean quote,
|
|||||||
static char*
|
static char*
|
||||||
_autocomplete_param_common(const char* const input, char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context)
|
_autocomplete_param_common(const char* const input, char* command, autocomplete_func func, Autocomplete ac, gboolean quote, gboolean previous, void* context)
|
||||||
{
|
{
|
||||||
GString* auto_msg = NULL;
|
GString* auto_msg;
|
||||||
|
char* command_cpy;
|
||||||
char* result = NULL;
|
char* result = NULL;
|
||||||
char* command_cpy = malloc(strlen(command) + 2);
|
int len;
|
||||||
sprintf(command_cpy, "%s ", command);
|
|
||||||
int len = strlen(command_cpy);
|
len = asprintf(&command_cpy, "%s ", command);
|
||||||
|
if (len == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (strncmp(input, command_cpy, len) == 0) {
|
if (strncmp(input, command_cpy, len) == 0) {
|
||||||
int inp_len = strlen(input);
|
int inp_len = strlen(input);
|
||||||
|
|||||||
Reference in New Issue
Block a user