暂无描述

rexim 1fd8f19ec1 [glob.h] Remove dependency on assert 2 年之前
.gitignore efb447b062 Add coverge tools 2 年之前
ConvertUTF.c a3175b33bc Ready. Set. Go! 2 年之前
ConvertUTF.h a3175b33bc Ready. Set. Go! 2 年之前
LICENSE a3175b33bc Ready. Set. Go! 2 年之前
README.md 9d855b28f8 Fix typo 2 年之前
coverage.sh ba954ad88d Bake ConvertUTF into glob.h 2 年之前
glob.h 1fd8f19ec1 [glob.h] Remove dependency on assert 2 年之前
test.sh 4810d08cf6 build.sh -> test.sh 2 年之前
test_glob.c ba954ad88d Bake ConvertUTF into glob.h 2 年之前

README.md

Simple Header-Only Implementation of Glob Matching

Usage

#include <stdio.h>
#include <string.h>
#define GLOB_IMPLEMENTATION
#include "glob.h"

int main(void)
{
    if (!glob_utf8("*.c", "main.c")) {
        printf("OK\n");
    } else {
        printf("FAIL\n");
    }
    return 0;
}

Custom Allocator

glob_utf8 allocates and deallocates memory on each call because it needs to decode UTF-8. If you want to avoid that you can define a custom temporary allocator.

#include <stdio.h>

char temp[1024];
size_t temp_bump = 0;

void *temp_alloc(size_t size)
{
    if (temp_bump + size > sizeof(temp)) return NULL;
    void *result = temp + temp_bump;
    temp_bump += size;
    return result;
}

#define GLOB_IMPLEMENTATION
#define GLOB_MALLOC temp_alloc
#define GLOB_FREE(...)
#include "glob.h"

int main(void)
{
    const char *texts[] = { "main.c", "test.c", "index.js", "reset.css" };
    size_t n = sizeof(texts)/sizeof(texts[0]);
    for (size_t i = 0; i < n; ++i) {
        if (!glob_utf8("*.c", texts[i])) {
            printf("%s\n", texts[i]);
        }
        temp_bump = 0;
    }
    return 0;
}

Testing

$ ./test.sh

Coverage

Very useful to see what's not tested yet. Requires clang.

$ ./coverage.sh