From e963765d308b694fc0703bd65828ae4845084543 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 3 Feb 2023 11:50:10 -0800 Subject: [PATCH] got rid of lorem --- Makefile | 11 ++++--- src/flight.h | 10 +++---- src/hash.h | 9 ++++++ src/kjv.re.c | 8 +++--- src/lorem.c | 71 ---------------------------------------------- src/lorem.h | 7 ----- src/pair.c | 24 ++++++++++++++++ src/pair.h | 5 ++++ src/scan_kjv.re.c | 9 +++--- src/source.h | 3 +- src/source.re.c | 18 ++++++------ test/test_lorem.c | 37 ------------------------ test/test_source.c | 2 +- 13 files changed, 67 insertions(+), 147 deletions(-) create mode 100644 src/hash.h delete mode 100644 src/lorem.c delete mode 100644 src/lorem.h create mode 100644 src/pair.c create mode 100644 src/pair.h delete mode 100644 test/test_lorem.c diff --git a/Makefile b/Makefile index f933546..559b351 100644 --- a/Makefile +++ b/Makefile @@ -30,19 +30,18 @@ else CF += -g endif -projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/test-lorem bin/kjv bin/flight +projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/kjv bin/flight #docs := $(patsubst test/test_%.c, doc/%.md, $(wildcard test/test_*.c)) default: $(projects) # success bin/test-text: build/text.o build/test_text.o -bin/test-lorem: build/lorem.o build/test_lorem.o bin/test-journal: build/text.o build/journal.o build/test_journal.o -bin/test-source: build/text.o build/journal.o build/lorem.o build/source.o build/test_source.o -bin/test-kjv: build/text.o build/kjv.o build/test_kjv.o -bin/kjv: build/text.o build/journal.o build/kjv.o build/scan_kjv.o -bin/flight: build/text.o build/journal.o build/flight.o build/flighthours.o +bin/test-source: build/text.o build/pair.o build/journal.o build/source.o build/test_source.o +bin/test-kjv: build/text.o build/pair.o build/kjv.o build/test_kjv.o +bin/kjv: build/text.o build/pair.o build/journal.o build/kjv.o build/scan_kjv.o +bin/flight: build/text.o build/pair.o build/journal.o build/flight.o build/flighthours.o bin/%: @echo "\033[1;36mlinking $@\033[0m" diff --git a/src/flight.h b/src/flight.h index fcd0605..a438f10 100644 --- a/src/flight.h +++ b/src/flight.h @@ -23,19 +23,19 @@ static const char *flight_type_string[] = { FLIGHT_TYPE }; #undef X #undef FLIGHT_TYPE -#include "lorem.h" +#include "pair.h" struct glider { - struct lorem type, reg, launch; + struct pair type, reg, launch; enum launch_type how; unsigned height_ft, pilot_min, dual_min, instr_min; - struct lorem remarks; + struct pair remarks; }; struct power { - struct lorem type, reg, launch, landing, pilot, copilot; + struct pair type, reg, launch, landing, pilot, copilot; unsigned dual_min, pilot_min, ifrsim_min, ifr_min; - struct lorem remarks; + struct pair remarks; }; struct flight { diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..24d142c --- /dev/null +++ b/src/hash.h @@ -0,0 +1,9 @@ +#include + +/** djb2 */ +static uint32_t djb2(const char *s) { + const unsigned char *str = (const unsigned char *)s; + uint32_t hash = 5381, c; + while(c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + return hash; +} diff --git a/src/kjv.re.c b/src/kjv.re.c index f6e27fe..c3a5ee9 100644 --- a/src/kjv.re.c +++ b/src/kjv.re.c @@ -10,7 +10,7 @@ #define BASE #include "../src/kjv.h" /* Just the base data. */ #include "../src/text.h" -#include "../src/helper.h" +#include "../src/pair.h" #include #include #include @@ -90,7 +90,7 @@ static int looks_like_book_fn(const char *fn, unsigned *const book_no) { * { return 0; } @s0 natural @s1 [^.\x00]* ".txt" "\x00" - { return helper_natural(s0, s1, book_no); } + { return pair_to_natural(s0, s1, book_no); } */ } @@ -129,8 +129,8 @@ scan: [^[\]\n\x00]* "\n" { lex->line++; goto scan; } "\x00" { return 0; } "[" @s0 natural @s1 ":" @t0 natural @t1 "]" => verse { - if(!helper_natural(s0, s1, &lex->chapter) - || !helper_natural(t0, t1, &lex->verse)) + if(!pair_to_natural(s0, s1, &lex->chapter) + || !pair_to_natural(t0, t1, &lex->verse)) return errno = EILSEQ, lex->error = 1, 0; lex->words = 0; /*printf("%u:%u", lex->chapter, lex->verse);*/ diff --git a/src/lorem.c b/src/lorem.c deleted file mode 100644 index 95ea971..0000000 --- a/src/lorem.c +++ /dev/null @@ -1,71 +0,0 @@ -/** @license 2023 Neil Edelman, distributed under the terms of the - [MIT License](https://opensource.org/licenses/MIT). - - Global stable pool of temporary strings duplicated from substrings. Doesn't - support deletion, only growing. - - @std C89 */ - -#include "lorem.h" -#include -#include - -#define ARRAY_NAME block -#define ARRAY_TYPE char * -#include "array.h" - -static struct { - size_t capacity, size; /* Lead block. */ - struct block_array blocks; -} buffer; - -struct lorem lorem(const char *const a, const char *const b) { - struct lorem l; - assert(a && a <= b); - l.sub = a; - l.size = (size_t)(b - a); - return l; -} - -const char *lorem_dup(const struct lorem sub) { - char *string; - size_t size; - if(!sub.sub) { errno = EDOM; return 0; } /* Null substring. */ - if(sub.size == ~(size_t)0) { errno = ERANGE; return 0; } /* Unlikely. */ - if((size = sub.size + 1) > buffer.capacity - buffer.size) { - size_t c0 = buffer.capacity < 8 ? 8 : buffer.capacity; - char *block = 0, **record = 0; - while(c0 < size) { /* Grow. */ - size_t c1 = c0 + (c0 >> 1) + (c0 >> 3); - if(c0 >= c1) { c0 = ~(size_t)0; break; } /* Unlikely. */ - c0 = c1; - } - if(!(block = malloc(c0)) - || !(record = block_array_new(&buffer.blocks))) { /* Error. */ - free(block); - if(!errno) errno = ERANGE; - return 0; - } - *record = block; - buffer.capacity = c0; - buffer.size = 0; - /*printf("%s size %zu\n", orcify(block), c0);*/ - } - assert(buffer.blocks.size); - string = buffer.blocks.data[buffer.blocks.size - 1] + buffer.size; - memcpy(string, sub.sub, sub.size); - string[sub.size] = '\0'; - buffer.size += size; - return string; -} - -void lorem_(void) { - size_t i; - for(i = 0; i < buffer.blocks.size; i++) { - char *block = buffer.blocks.data[i]; - /*printf("free %s\n", orcify(block));*/ - free(block); - } - buffer.capacity = buffer.size = 0; - block_array_(&buffer.blocks); -} diff --git a/src/lorem.h b/src/lorem.h deleted file mode 100644 index ad50b84..0000000 --- a/src/lorem.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -struct lorem { const char *sub; size_t size; }; - -struct lorem lorem(const char *const a, const char *const b); -const char *lorem_dup(const struct lorem); -void lorem_(void); diff --git a/src/pair.c b/src/pair.c new file mode 100644 index 0000000..886f88e --- /dev/null +++ b/src/pair.c @@ -0,0 +1,24 @@ +#include "pair.h" +#include +#include + +/** @return Constructs `a` and `b` as a pair. */ +struct pair pair(const char *const a, const char *const b) { + struct pair p; + p.a = a, p.b = b; + return p; +} + +/** Doesn't check anything. + @return Whether it was able to parse unsigned `p` to `n`. */ +int pair_to_natural(const char *s, const char *const e, uint32_t *const n) { + uint32_t accum = 0; + while(s < e) { + unsigned next = accum * 10 + (unsigned)(*s - '0'); + if(accum >= next) return errno = ERANGE, 0; + accum = next; + s++; + } + *n = accum; + return 1; +} diff --git a/src/pair.h b/src/pair.h new file mode 100644 index 0000000..0b3d66a --- /dev/null +++ b/src/pair.h @@ -0,0 +1,5 @@ +#include + +struct pair { const char *a, *b; }; +struct pair pair(const char *const a, const char *const b); +int pair_to_natural(const char *, const char *, uint32_t *); diff --git a/src/scan_kjv.re.c b/src/scan_kjv.re.c index beb42ec..236147b 100644 --- a/src/scan_kjv.re.c +++ b/src/scan_kjv.re.c @@ -5,10 +5,11 @@ #include "../src/journal.h" #include "../src/kjv.h" -#include "../src/helper.h" +#include "../src/pair.h" #include /* C99 */ #include #include +#include #include #include @@ -136,15 +137,15 @@ static int scan(union date32 date, const char *const buffer, ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? { if(chapter || verse || verse_end) { why = "reference unrecognized"; goto catch; } - if(!helper_natural(s0, s1, &chapter) - || !helper_natural(t0, t1, &verse)) + if(!pair_to_natural(s0, s1, &chapter) + || !pair_to_natural(t0, t1, &verse)) { why = "reference numerical error"; goto catch; } continue; } "-" @s0 natural @s1 [ab]? { /* Verse range. */ if(!chapter || !verse || verse_end) { why = "range unrecognized"; goto catch; } - if(!helper_natural(s0, s1, &verse_end)) + if(!pair_to_natural(s0, s1, &verse_end)) { why = "range numerical error"; goto catch; } continue; } diff --git a/src/source.h b/src/source.h index b92dea2..f7f6486 100644 --- a/src/source.h +++ b/src/source.h @@ -1,7 +1,6 @@ #if defined BASE \ || !defined BASE && !defined GENERIC && !defined PROTO /* */ diff --git a/src/source.re.c b/src/source.re.c index d6db091..e37241b 100644 --- a/src/source.re.c +++ b/src/source.re.c @@ -4,29 +4,27 @@ #define BASE #include "../src/source.h" /* base */ #include "../src/journal.h" +#include "../src/hash.h" /* djb2 */ +#include "../src/pair.h" #include #include #include #include -/* Temporary buffer to lookup. */ -#define ARRAY_NAME char -#define ARRAY_TYPE char -#include "array.h" -/* ...wait, why would you copy? modify the table to use substring. */ +/* fixme: ...wait, why would you copy? modify the table to use substring. */ /* This is a lookup table for source strings ("2000glider") to substring the first description ("Glider pilot log book"). */ static int lookup_is_equal(const char *const x, const char *const y) { return !strcmp(x, y); } static uint32_t lookup_hash(const char *const x) { return djb2(x); } -static void lookup_to_string(const char *x, const struct lorem desc, +static void lookup_to_string(const char *x, const struct pair desc, char (*const a)[12]) { (void)desc; sprintf(*a, "%.11s", x); } -static struct lorem lookup_default = { 0, 0 }; +static struct pair lookup_default = { 0, 0 }; #define TABLE_NAME lookup #define TABLE_KEY char * #define TABLE_UINT uint32_t -#define TABLE_VALUE struct lorem +#define TABLE_VALUE struct pair #define TABLE_DEFAULT lookup_default #define TABLE_TO_STRING #include "../src/table.h" @@ -84,7 +82,7 @@ static int scan(union date32 date, const char *const buffer, * { why = "default source unrecognized"; goto catch; } @s0 keyword @s1 / "\n" => skip { - struct lorem lor = lorem(s0, s1); + struct pair p = pair(s0, s1); const char *label; printf("extracted <%.*s>\n", (int)(s1 - s0), s0); continue; @@ -123,6 +121,6 @@ struct sources sources(struct journal *const j) { catch: sources_(&s); finally: - char_array_(&temp); /* Erase the temporary buffer. */ + //char_array_(&temp); /* Erase the temporary buffer. */ return s; } diff --git a/test/test_lorem.c b/test/test_lorem.c deleted file mode 100644 index 9222b87..0000000 --- a/test/test_lorem.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "../src/lorem.h" -#include -#include - -int main(void) { - int success = EXIT_SUCCESS; - const char lorem[] = "Lorem ipsum dolor sit amet, consectetur adipiscing " - "elit. Aenean tincidunt leo neque. Integer vel bibendum lectus, a " - "vulputate dolor. Vivamus vestibulum quam ut euismod aliquet. Vivamus " - "vel pulvinar felis, eu dictum lorem. Integer scelerisque lobortis " - "orci nec tincidunt. Mauris vulputate ipsum non tempus tincidunt. " - "Pellentesque nec iaculis dolor. Curabitur bibendum pretium dui " - "euismod tincidunt. In cursus, libero et porta placerat, ante ante " - "accumsan lacus, nec sollicitudin ex elit nec lectus. Sed nisi sem, " - "rhoncus sed nulla et, faucibus feugiat eros."; - struct lorem s; - const char *a, *b, *c, *d; - s.sub = lorem + 6, s.size = 5; - if(!(a = lorem_dup(s))) goto catch; - printf("a: <%s>\n", a); - s.sub = lorem + 40, s.size = 20; - if(!(b = lorem_dup(s))) goto catch; - printf("a: <%s>, b: <%s>\n", a, b); - s.sub = lorem + 80, s.size = 60; - if(!(c = lorem_dup(s))) goto catch; - printf("a: <%s>, b: <%s>, c: <%s>\n", a, b, c); - s.sub = lorem + 200, s.size = 10; - if(!(d = lorem_dup(s))) goto catch; - printf("a: <%s>, b: <%s>, c: <%s>, d: <%s>\n", a, b, c, d); - goto finally; -catch: - success = EXIT_FAILURE; - perror("text"); -finally: - lorem_(); - return success; -} diff --git a/test/test_source.c b/test/test_source.c index 6972e0b..4448892 100644 --- a/test/test_source.c +++ b/test/test_source.c @@ -3,6 +3,7 @@ #include #include #include +#include int main(void) { int success = EXIT_SUCCESS; @@ -17,6 +18,5 @@ catch: finally: sources_(&s); journal_(&j); - lorem_(); return success; }