Add and use a copy-range string function

This commit is contained in:
Chris Robinson 2016-02-24 04:53:32 -08:00
parent d04970e568
commit b6824ca716
3 changed files with 22 additions and 12 deletions

View File

@ -32,6 +32,7 @@ int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2);
void al_string_copy(al_string *str, const_al_string from); void al_string_copy(al_string *str, const_al_string from);
void al_string_copy_cstr(al_string *str, const al_string_char_type *from); void al_string_copy_cstr(al_string *str, const al_string_char_type *from);
void al_string_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to);
void al_string_append_char(al_string *str, const al_string_char_type c); void al_string_append_char(al_string *str, const al_string_char_type c);
void al_string_append_cstr(al_string *str, const al_string_char_type *from); void al_string_append_cstr(al_string *str, const al_string_char_type *from);

View File

@ -735,8 +735,7 @@ vector_al_string SearchDataFiles(const char *ext, const char *subdir)
al_string_copy_cstr(&path, str); al_string_copy_cstr(&path, str);
else else
{ {
al_string_clear(&path); al_string_copy_range(&path, str, next);
al_string_append_range(&path, str, next);
++next; ++next;
} }
if(!al_string_empty(path)) if(!al_string_empty(path))
@ -861,12 +860,16 @@ extern inline const al_string_char_type *al_string_get_cstr(const_al_string str)
void al_string_clear(al_string *str) void al_string_clear(al_string *str)
{ {
/* Reserve one more character than the total size of the string. This is to if(!al_string_empty(*str))
* ensure we have space to add a null terminator in the string data so it {
* can be used as a C-style string. */ /* Reserve one more character than the total size of the string. This
VECTOR_RESERVE(*str, 1); * is to ensure we have space to add a null terminator in the string
VECTOR_RESIZE(*str, 0); * data so it can be used as a C-style string.
*VECTOR_ITER_END(*str) = 0; */
VECTOR_RESERVE(*str, 1);
VECTOR_RESIZE(*str, 0);
*VECTOR_ITER_END(*str) = 0;
}
} }
static inline int al_string_compare(const al_string_char_type *str1, size_t str1len, static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
@ -910,6 +913,15 @@ void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
*VECTOR_ITER_END(*str) = 0; *VECTOR_ITER_END(*str) = 0;
} }
void al_string_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
{
size_t len = to - from;
VECTOR_RESERVE(*str, len+1);
VECTOR_RESIZE(*str, 0);
VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
*VECTOR_ITER_END(*str) = 0;
}
void al_string_append_char(al_string *str, const al_string_char_type c) void al_string_append_char(al_string *str, const al_string_char_type c)
{ {
VECTOR_RESERVE(*str, al_string_length(*str)+2); VECTOR_RESERVE(*str, al_string_length(*str)+2);

View File

@ -583,10 +583,7 @@ static void AddFileEntry(vector_HrtfEntry *list, al_string *filename)
if(!ext) if(!ext)
al_string_copy_cstr(&entry.name, name); al_string_copy_cstr(&entry.name, name);
else else
{ al_string_copy_range(&entry.name, name, ext);
al_string_clear(&entry.name);
al_string_append_range(&entry.name, name, ext);
}
if(i != 0) if(i != 0)
{ {
char str[64]; char str[64];