This is not the most useful way of substring.

This commit is contained in:
Neil 2023-02-03 00:05:20 -08:00
parent 4eb997d49e
commit ffcb1ce6aa
4 changed files with 34 additions and 15 deletions

View File

@ -17,15 +17,23 @@
static struct {
size_t capacity, size; /* Lead block. */
struct block_array blocks;
} lorem;
} 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) > lorem.capacity - lorem.size) {
size_t c0 = lorem.capacity < 8 ? 8 : lorem.capacity;
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);
@ -33,31 +41,31 @@ const char *lorem_dup(const struct lorem sub) {
c0 = c1;
}
if(!(block = malloc(c0))
|| !(record = block_array_new(&lorem.blocks))) { /* Error. */
|| !(record = block_array_new(&buffer.blocks))) { /* Error. */
free(block);
if(!errno) errno = ERANGE;
return 0;
}
*record = block;
lorem.capacity = c0;
lorem.size = 0;
buffer.capacity = c0;
buffer.size = 0;
/*printf("%s size %zu\n", orcify(block), c0);*/
}
assert(lorem.blocks.size);
string = lorem.blocks.data[lorem.blocks.size - 1] + lorem.size;
assert(buffer.blocks.size);
string = buffer.blocks.data[buffer.blocks.size - 1] + buffer.size;
memcpy(string, sub.sub, sub.size);
string[sub.size] = '\0';
lorem.size += size;
buffer.size += size;
return string;
}
void lorem_(void) {
size_t i;
for(i = 0; i < lorem.blocks.size; i++) {
char *block = lorem.blocks.data[i];
for(i = 0; i < buffer.blocks.size; i++) {
char *block = buffer.blocks.data[i];
/*printf("free %s\n", orcify(block));*/
free(block);
}
lorem.capacity = lorem.size = 0;
block_array_(&lorem.blocks);
buffer.capacity = buffer.size = 0;
block_array_(&buffer.blocks);
}

View File

@ -2,5 +2,6 @@
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);

View File

@ -9,6 +9,12 @@
#include <stdlib.h>
#include <assert.h>
/* 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. */
/* 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)
@ -26,13 +32,13 @@ static struct lorem lookup_default = { 0, 0 };
#include "../src/table.h"
static void source_to_string(const union line64 line, const struct lorem *u,
static void source_to_string(const union line64 line, char *const*const u,
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int source_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME source
#define TREE_KEY union line64
#define TREE_VALUE struct lorem
#define TREE_VALUE char *
#define TREE_COMPARE
#define TREE_TO_STRING
#include "../src/tree.h"
@ -78,6 +84,8 @@ static int scan(union date32 date, const char *const buffer,
<source> * { why = "default source unrecognized"; goto catch; }
<source> @s0 keyword @s1 / "\n" => skip {
struct lorem lor = lorem(s0, s1);
const char *label;
printf("extracted <%.*s>\n", (int)(s1 - s0), s0);
continue;
}
@ -115,5 +123,6 @@ struct sources sources(struct journal *const j) {
catch:
sources_(&s);
finally:
char_array_(&temp); /* Erase the temporary buffer. */
return s;
}

View File

@ -17,5 +17,6 @@ catch:
finally:
sources_(&s);
journal_(&j);
lorem_();
return success;
}