Skip to content

Commit

Permalink
Unsafe function removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim2266 committed Apr 13, 2020
1 parent 3b02e23 commit 583ef0e
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 73 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,6 @@ also provided:
Binary search for the given key. The input array must be sorted using `str_order_asc`.
Returns a pointer to the string matching the key, or NULL.

`size_t str_uniq(str* const array, const size_t count)`<br>
Retain only the unique strings in the given array, in-place, also releasing any memory
owned by unused strings. Input array does _not_ have to be sorted. Returns the number of
strings retained. After the call, the strings are sorted, so the array is suitable for
binary search using `str_search()` function.

#### Memory Management

`void str_free(const str s)`<br>
Expand Down
33 changes: 2 additions & 31 deletions str.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define _DEFAULT_SOURCE // for strncasecmp()

#include "str.h"

#define _DEFAULT_SOURCE // for strncasecmp()
#include <string.h>

// compatibility
Expand Down Expand Up @@ -328,36 +329,6 @@ void str_sort(const str_cmp_func cmp, str* const array, const size_t count)
qsort(array, count, sizeof(array[0]), cmp);
}

// retain unique strings only
size_t str_uniq(str* const array, const size_t count)
{
if(!array || count == 0)
return 0;

if(count == 1)
return 1;

// O(n * log n), but without memory allocation
str_sort(str_order_asc, array, count);

str* p = array;
const str* const end = array + count;

for(str* s = array + 1; ; ++s)
{
while(s < end && str_eq(*p, *s))
str_clear(s++);

if(s == end)
break;

if(++p < s)
*p = str_move(s);
}

return p + 1 - array;
}

// searching
const str* str_search(const str key, const str* const array, const size_t count)
{
Expand Down
3 changes: 0 additions & 3 deletions str.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ int str_order_desc_ci(const void* const s1, const void* const s2);
// sort array of strings
void str_sort(const str_cmp_func cmp, str* const array, const size_t count);

// retain unique strings only
size_t str_uniq(str* const array, const size_t count);

// searching
const str* str_search(const str key, const str* const array, const size_t count);

Expand Down
33 changes: 0 additions & 33 deletions str_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,38 +326,6 @@ void test_search(void)
passed;
}

static
void test_uniq(void)
{
str src[10] = { {0} };

str* p = src;

str_dup(p++, str_lit("zzz"));
str_dup(p++, str_lit("zzz"));
str_dup(p++, str_lit("aaa"));
str_dup(p++, str_lit("bbb"));
str_dup(p++, str_lit("aaa"));
str_dup(p++, str_lit("bbb"));
str_dup(p++, str_lit("bbb"));
str_dup(p++, str_lit("bbb"));
str_dup(p++, str_lit("aaa"));
str_dup(p++, str_lit("zzz"));

const size_t count = str_uniq(src, p - src);

assert(count == 3);
assert(str_eq(src[0], str_lit("aaa")));
assert(str_eq(src[1], str_lit("bbb")));
assert(str_eq(src[2], str_lit("zzz")));

// clean-up
for(size_t i = 0; i < count; ++i)
str_free(src[i]);

passed;
}

int main(void)
{
// tests
Expand All @@ -376,7 +344,6 @@ int main(void)
test_sort();
test_sort_ci();
test_search();
test_uniq();

return puts("OK.") < 0;
}

0 comments on commit 583ef0e

Please sign in to comment.