Close

Merging

A project log for aStrA : Aligned Strings format with attributes

Developing a more flexible pointer and structure format that solves POSIX and Cs historical problems.

yann-guidon-ygdesYann Guidon / YGDES 11/23/2024 at 07:500 Comments

The long awaited update ! AS20241121.tgz is uploaded and the little demo works !

It can do the following:

      ASTRING8(s1,"hello ")
      ASTRING8(s2,"world\n")
FLEX_ASTRING16(s4,"0123456789", 42)
EMPTY_ASTRING8(buffer, 100)

aStrA_merge(buffer, s1, s2, s4, UPOINT('\n'),
      UPOINT(0x1F60A), ARGSTR("\n# the end.\n"));
write(0, (void*)buffer, aStrA_length_fast(buffer));

The lists are not yet handled, that will be a next milestone, but the core features are coming to life.

The ARGSTR argument type is a required hack since C and the proprocessor don't allow me to create the required anonymous structures. The temporary solution is to define another composite format: the size of the string is given as a pure number (<<3) and distinguished from NULL. This precedes the actual pointer which could be misaligned, end with \0 etc.

This is "dangerous" because there is a little "hack" to make it work : sizeof counts the ending \0 so sizeof("")=1. This ensures that the size parameter (sizeof << 3) is always different from NULL and the value is decremented just before use. I hope this hack will be removed later...

// declare a constant string in an argument
#define ARGSTR(VALUE)  sizeof(VALUE) << 3, VALUE
// No canary, no structure: it's passed directly on the call stack.

So the macro replaces the string with a pair (size then value) separated by a comma.

Another format-adapting macro adds the terminal NULL to the varargs of merge:

// Let's first make sure the list of arguments
// is ALWAYS correctly terminated:
#define aStrA_merge(DESTINATION, ARGUMENTS... ) \
        aStrA_merge_body(DESTINATION, ARGUMENTS , NULL)

// Arguments : only aStrA_t types !
int aStrA_merge_body(aStrA_t dest, ...) {

So it's a big hack so far, trying to get around C's limitations...

But it works so far and the implementation will evolve, while the user-facing features get polished.

Discussions