(String) Do not assume char is unsigned (#14168)

This commit is contained in:
Cthulhu-throwaway 2022-07-11 21:55:56 -03:00 committed by GitHub
parent 33386787b6
commit f39df40728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 27 deletions

View File

@ -268,13 +268,13 @@ extern const unsigned char lr_char_props[256];
int string_count_occurrences_single_character(char *str, char t);
/* Replaces all spaces with the given character. */
void string_replace_whitespace_with_single_character(char *str, char t);
void string_replace_whitespace_with_single_character(char *str, char c);
/* Replaces multiple spaces with a single space in a string. */
void string_replace_multi_space_with_single_space(char *str);
/* Remove all spaces from the given string. */
void string_remove_all_whitespace(char* str_trimmed, const char* str_untrimmed);
void string_remove_all_whitespace(char *str_trimmed, const char *str);
/* Retrieve the last occurance of the given character in a string. */
int string_index_last_occurance(char str[], char t);

View File

@ -545,14 +545,11 @@ int string_count_occurrences_single_character(char *str, char t)
/**
* Replaces all spaces with the given character.
*/
void string_replace_whitespace_with_single_character(char *str, char t)
void string_replace_whitespace_with_single_character(char *str, char c)
{
while (*str)
{
if (isspace(*str))
*str = t;
str++;
}
for (; *str; str++)
if (ISSPACE(*str))
*str = c;
}
/**
@ -560,33 +557,29 @@ void string_replace_whitespace_with_single_character(char *str, char t)
*/
void string_replace_multi_space_with_single_space(char *str)
{
char *dest = str;
char *str_trimmed = str;
bool prev_is_space = false;
bool curr_is_space = false;
while (*str != '\0')
for (; *str; str++)
{
while (*str == ' ' && *(str + 1) == ' ')
str++;
*dest++ = *str++;
curr_is_space = ISSPACE(*str);
if (prev_is_space && curr_is_space)
continue;
*str_trimmed++ = *str;
prev_is_space = curr_is_space;
}
*dest = '\0';
*str_trimmed = '\0';
}
/**
* Remove all spaces from the given string.
*/
void string_remove_all_whitespace(char* str_trimmed, const char* str_untrimmed)
void string_remove_all_whitespace(char *str_trimmed, const char *str)
{
while (*str_untrimmed != '\0')
{
if(!isspace(*str_untrimmed))
{
*str_trimmed = *str_untrimmed;
str_trimmed++;
}
str_untrimmed++;
}
for (; *str; str++)
if (!ISSPACE(*str))
*str_trimmed++ = *str;
*str_trimmed = '\0';
}