Skip to content

Commit

Permalink
cleanup:search:Remove duplicate code for search_fix_spaces (#917)
Browse files Browse the repository at this point in the history
* cleanup:search:Remove duplicate code for search_fix_spaces

* Remove the static modifier to allow its usage in multiple files

* Avoid discards const qualifier from pointer target type warning

* Fix the bad redirect and force const in signature

* Add doc for search_fix_spaces

* Update description

* Handle case when the string will only contain chars that will be discarded
  • Loading branch information
aerostitch authored and hoehnp committed Nov 4, 2019
1 parent 318115f commit 045ddbe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
29 changes: 0 additions & 29 deletions navit/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,35 +681,6 @@ static void android_search_idle(struct android_search_priv *search_priv) {
dbg(lvl_info, "leave");
}

static char *search_fix_spaces(const char *str) {
int i;
int len=strlen(str);
char c,*s,*d,*ret=g_strdup(str);

for (i = 0 ; i < len ; i++) {
if (ret[i] == ',' || ret[i] == '/')
ret[i]=' ';
}
s=ret;
d=ret;
len=0;
do {
c=*s++;
if (c != ' ' || len != 0) {
*d++=c;
len++;
}
while (c == ' ' && *s == ' ')
s++;
if (c == ' ' && *s == '\0') {
d--;
len--;
}
} while (c);

return ret;
}

static void start_search(struct android_search_priv *search_priv, const char *search_string) {
dbg(lvl_debug,"enter %s", search_string);
char *str=search_fix_spaces(search_string);
Expand Down
14 changes: 12 additions & 2 deletions navit/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,20 @@ int search_list_level(enum attr_type attr_type) {
}
}

static char *search_fix_spaces(char *str) {
/**
* @brief Replaces ',' and '/' by ' ', deduplicates spaces within the string
* and strips spaces from both ends of the string
*
* @param pointer to the string to cleanup
* @return pointer to the cleaned up string
*/
char *search_fix_spaces(const char *str) {
int i;
int len=strlen(str);
char c,*s,*d,*ret=g_strdup(str);

for (i = 0 ; i < len ; i++) {
if (ret[i] == ',' || ret[i] == ',' || ret[i] == '/')
if (ret[i] == ',' || ret[i] == '/')
ret[i]=' ';
}
s=ret;
Expand All @@ -160,6 +167,9 @@ static char *search_fix_spaces(char *str) {
len--;
}
} while (c);
// Make sure the string is terminated at current position even if nothing has been added to it.
// This case happen when you use a string containing only chars that will be discarded.
*d='\0';
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions navit/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ char *search_list_get_unique(struct search_list *this_, char *unique);
struct search_list_result *search_list_get_result(struct search_list *this_);
void search_list_destroy(struct search_list *this_);
void search_init(void);
char *search_fix_spaces(const char *str);
/* end of prototypes */
#ifdef __cplusplus
}
Expand Down

0 comments on commit 045ddbe

Please sign in to comment.