(String) Some function fixes (#14171)

This commit is contained in:
Cthulhu-throwaway 2022-07-12 17:32:58 -03:00 committed by GitHub
parent c4fb055fdb
commit 98ca6232aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 42 deletions

View File

@ -262,10 +262,8 @@ char *string_init(const char *src);
void string_set(char **string, const char *src); void string_set(char **string, const char *src);
extern const unsigned char lr_char_props[256];
/* Get the total number of occurrences of a character in the given string. */ /* Get the total number of occurrences of a character in the given string. */
int string_count_occurrences_single_character(char *str, char t); int string_count_occurrences_single_character(const char *str, char c);
/* Replaces all spaces with the given character. */ /* Replaces all spaces with the given character. */
void string_replace_whitespace_with_single_character(char *str, char c); void string_replace_whitespace_with_single_character(char *str, char c);
@ -277,13 +275,15 @@ void string_replace_multi_space_with_single_space(char *str);
void string_remove_all_whitespace(char *str_trimmed, const char *str); void string_remove_all_whitespace(char *str_trimmed, const char *str);
/* Retrieve the last occurance of the given character in a string. */ /* Retrieve the last occurance of the given character in a string. */
int string_index_last_occurance(char str[], char t); int string_index_last_occurance(const char *str, char c);
/* Find the position of a substring in a string. */ /* Find the position of a substring in a string. */
int string_find_index_substring_string(const char* str1, const char* str2); int string_find_index_substring_string(const char *str, const char *substr);
/* Strips non-ASCII characters from a string. */ /* Strips non-ASCII characters from a string. */
void string_copy_only_ascii(char *str_stripped, const char* str); void string_copy_only_ascii(char *str_stripped, const char *str);
extern const unsigned char lr_char_props[256];
RETRO_END_DECLS RETRO_END_DECLS

View File

@ -528,18 +528,15 @@ unsigned string_hex_to_unsigned(const char *str)
/** /**
* Get the total number of occurrences of a character in the given string. * Get the total number of occurrences of a character in the given string.
*/ */
int string_count_occurrences_single_character(char *str, char t) int string_count_occurrences_single_character(const char *str, char c)
{ {
int ctr = 0; int count = 0;
int i;
for (i = 0; str[i] != '\0'; ++i) for (; *str; str++)
{ if (*str == c)
if (t == str[i]) count++;
++ctr;
}
return ctr; return count;
} }
/** /**
@ -586,46 +583,34 @@ void string_remove_all_whitespace(char *str_trimmed, const char *str)
/** /**
* Retrieve the last occurance of the given character in a string. * Retrieve the last occurance of the given character in a string.
*/ */
int string_index_last_occurance(char *str, char t) int string_index_last_occurance(const char *str, char c)
{ {
const char *ret = strrchr(str, t); const char *pos = strrchr(str, c);
if (ret)
return ret-str; return pos ? (pos - str) : -1;
return -1;
} }
/** /**
* Find the position of a substring in a string. * Find the position of a substring in a string.
*/ */
int string_find_index_substring_string(const char* str1, const char* str2) int string_find_index_substring_string(const char *str, const char *substr)
{ {
if (str1[0] != '\0') if (!string_is_empty(str))
{ {
const char *pfound = strstr(str1, str2); const char *pos = strstr(str, substr);
if (pfound)
return (pfound - str1); if (pos)
return pos - str;
} }
return -1; return -1;
} }
/* Strips non-ASCII characters from a string. */ /* Strips non-ASCII characters from a string. */
void string_copy_only_ascii(char *str_stripped, const char* str) void string_copy_only_ascii(char *str_stripped, const char *str)
{ {
if (!string_is_empty(str)) for (; *str; str++)
{ if (*str > 0x1F && *str < 0x7F)
unsigned i = 0; *str_stripped++ = *str;
unsigned j = 0; *str_stripped = '\0';
for (i = 0; i < strlen(str); i++)
{
if (str[i] > 0x1F && str[i] < 0x80)
{
str_stripped[j] = str[i];
j++;
}
}
str_stripped[j] = '\0';
}
} }