diff --git a/Makefile b/Makefile index acac791..9df01fc 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ CC = gcc STND=-ansi -CFLAGS = $(STND) -pedantic -O3 -g -Werror -Wall -Wextra -Wformat=2 -Wshadow -Wno-long-long \ - -Wno-overlength-strings -Wno-format-nonliteral -Wcast-align \ - -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls -Wnested-externs \ - -Wmissing-include-dirs -Wswitch-default +CFLAGS = $(STND) -pedantic -O3 -g -Werror -Wall -Wextra -Wformat=2 -Wshadow \ + -Wno-long-long -Wno-overlength-strings -Wno-format-nonliteral -Wcast-align \ + -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls \ + -Wnested-externs -Wmissing-include-dirs -Wswitch-default TESTS = $(wildcard tests/*.c) EXAMPLES = $(wildcard examples/*.c) diff --git a/README.md b/README.md index f8a46e6..be6eb19 100644 --- a/README.md +++ b/README.md @@ -582,22 +582,22 @@ Useful Parsers mpc_startswith(mpc_parser_t *a);Matches the start of input followed by a mpc_endswith(mpc_parser_t *a, mpc_dtor_t da);Matches a followed by the end of input mpc_whole(mpc_parser_t *a, mpc_dtor_t da);Matches the start of input, a, and the end of input - mpc_stripl(mpc_parser_t *a);Matches a striping any whitespace to the left - mpc_stripr(mpc_parser_t *a);Matches a striping any whitespace to the right - mpc_strip(mpc_parser_t *a);Matches a striping any surrounding whitespace - mpc_tok(mpc_parser_t *a);Matches a and strips any trailing whitespace - mpc_sym(const char *s);Matches string s and strips any trailing whitespace - mpc_total(mpc_parser_t *a, mpc_dtor_t da);Matches the whitespace stripped a, enclosed in the start and end of input + mpc_stripl(mpc_parser_t *a);Matches a first consuming any whitespace to the left + mpc_stripr(mpc_parser_t *a);Matches a then consumes any whitespace to the right + mpc_strip(mpc_parser_t *a);Matches a consuming any surrounding whitespace + mpc_tok(mpc_parser_t *a);Matches a and consumes any trailing whitespace + mpc_sym(const char *s);Matches string s and consumes any trailing whitespace + mpc_total(mpc_parser_t *a, mpc_dtor_t da);Matches the whitespace consumed a, enclosed in the start and end of input mpc_between(mpc_parser_t *a, mpc_dtor_t ad,
const char *o, const char *c);
Matches a between strings o and c mpc_parens(mpc_parser_t *a, mpc_dtor_t ad);Matches a between "(" and ")" mpc_braces(mpc_parser_t *a, mpc_dtor_t ad);Matches a between "<" and ">" mpc_brackets(mpc_parser_t *a, mpc_dtor_t ad);Matches a between "{" and "}" mpc_squares(mpc_parser_t *a, mpc_dtor_t ad);Matches a between "[" and "]" mpc_tok_between(mpc_parser_t *a, mpc_dtor_t ad,
const char *o, const char *c);
Matches a between o and c, where o and c have their trailing whitespace striped. - mpc_tok_parens(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace stripped "(" and ")" - mpc_tok_braces(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace stripped "<" and ">" - mpc_tok_brackets(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace stripped "{" and "}" - mpc_tok_squares(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace stripped "[" and "]" + mpc_tok_parens(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace consumed "(" and ")" + mpc_tok_braces(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace consumed "<" and ">" + mpc_tok_brackets(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace consumed "{" and "}" + mpc_tok_squares(mpc_parser_t *a, mpc_dtor_t ad);Matches a between trailing whitespace consumed "[" and "]" diff --git a/mpc.c b/mpc.c index f9cced1..fce1f0c 100644 --- a/mpc.c +++ b/mpc.c @@ -2096,12 +2096,33 @@ mpc_val_t *mpcf_oct(mpc_val_t *x) { } mpc_val_t *mpcf_float(mpc_val_t *x) { - float* y = malloc(sizeof(float)); + float *y = malloc(sizeof(float)); *y = strtod(x, NULL); free(x); return y; } +mpc_val_t *mpcf_strtriml(mpc_val_t *x) { + char *s = x; + while (isspace(*s)) { + memmove(s, s+1, strlen(s)); + } + return s; +} + +mpc_val_t *mpcf_strtrimr(mpc_val_t *x) { + char *s = x; + size_t l = strlen(s); + while (isspace(s[l-1])) { + s[l-1] = '\0'; l--; + } + return s; +} + +mpc_val_t *mpcf_strtrim(mpc_val_t *x) { + return mpcf_strtriml(mpcf_strtrimr(x)); +} + static const char mpc_escape_input_c[] = { '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\'', '\"', '\0'}; diff --git a/mpc.h b/mpc.h index 9ea77dd..e75685c 100644 --- a/mpc.h +++ b/mpc.h @@ -16,6 +16,7 @@ #include #include #include +#include /* ** State Type @@ -218,6 +219,9 @@ mpc_val_t *mpcf_int(mpc_val_t *x); mpc_val_t *mpcf_hex(mpc_val_t *x); mpc_val_t *mpcf_oct(mpc_val_t *x); mpc_val_t *mpcf_float(mpc_val_t *x); +mpc_val_t *mpcf_strtriml(mpc_val_t *x); +mpc_val_t *mpcf_strtrimr(mpc_val_t *x); +mpc_val_t *mpcf_strtrim(mpc_val_t *x); mpc_val_t *mpcf_escape(mpc_val_t *x); mpc_val_t *mpcf_escape_regex(mpc_val_t *x); diff --git a/tests/core.c b/tests/core.c index abb2ea8..516e2ae 100644 --- a/tests/core.c +++ b/tests/core.c @@ -6,8 +6,8 @@ static int int_eq(const void* x, const void* y) { return (*(int*)x == *(int*)y); } static void int_print(const void* x) { printf("'%i'", *((int*)x)); } -static int string_eq(const void* x, const void* y) { return (strcmp(x, y) == 0); } -static void string_print(const void* x) { printf("'%s'", (char*)x); } +static int streq(const void* x, const void* y) { return (strcmp(x, y) == 0); } +static void strprint(const void* x) { printf("'%s'", (char*)x); } void test_ident(void) { @@ -21,13 +21,13 @@ void test_ident(void) { free ); - PT_ASSERT(mpc_test_pass(Ident, "test", "test", string_eq, free, string_print)); - PT_ASSERT(mpc_test_fail(Ident, " blah", "", string_eq, free, string_print)); - PT_ASSERT(mpc_test_pass(Ident, "anoth21er", "anoth21er", string_eq, free, string_print)); - PT_ASSERT(mpc_test_pass(Ident, "du__de", "du__de", string_eq, free, string_print)); - PT_ASSERT(mpc_test_fail(Ident, "some spaces", "", string_eq, free, string_print)); - PT_ASSERT(mpc_test_fail(Ident, "", "", string_eq, free, string_print)); - PT_ASSERT(mpc_test_fail(Ident, "18nums", "", string_eq, free, string_print)); + PT_ASSERT(mpc_test_pass(Ident, "test", "test", streq, free, strprint)); + PT_ASSERT(mpc_test_fail(Ident, " blah", "", streq, free, strprint)); + PT_ASSERT(mpc_test_pass(Ident, "anoth21er", "anoth21er", streq, free, strprint)); + PT_ASSERT(mpc_test_pass(Ident, "du__de", "du__de", streq, free, strprint)); + PT_ASSERT(mpc_test_fail(Ident, "some spaces", "", streq, free, strprint)); + PT_ASSERT(mpc_test_fail(Ident, "", "", streq, free, strprint)); + PT_ASSERT(mpc_test_fail(Ident, "18nums", "", streq, free, strprint)); mpc_delete(Ident); @@ -69,7 +69,24 @@ void test_maths(void) { mpc_cleanup(4, Expr, Factor, Term, Maths); } +void test_strip(void) { + + mpc_parser_t *Stripperl = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtriml); + mpc_parser_t *Stripperr = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtrimr); + mpc_parser_t *Stripper = mpc_apply(mpc_many(mpcf_strfold, mpc_any()), mpcf_strtrim); + + PT_ASSERT(mpc_test_pass(Stripperl, " asdmlm dasd ", "asdmlm dasd ", streq, free, strprint)); + PT_ASSERT(mpc_test_pass(Stripperr, " asdmlm dasd ", " asdmlm dasd", streq, free, strprint)); + PT_ASSERT(mpc_test_pass(Stripper, " asdmlm dasd ", "asdmlm dasd", streq, free, strprint)); + + mpc_delete(Stripperl); + mpc_delete(Stripperr); + mpc_delete(Stripper); + +} + void suite_core(void) { pt_add_test(test_ident, "Test Ident", "Suite Core"); pt_add_test(test_maths, "Test Maths", "Suite Core"); + pt_add_test(test_strip, "Test Strip", "Suite Core"); }