diff --git a/src/userland/buffersTests/include/buffers.h b/src/userland/buffersTests/include/buffers.h index 39995d6..4ec2b12 100644 --- a/src/userland/buffersTests/include/buffers.h +++ b/src/userland/buffersTests/include/buffers.h @@ -104,7 +104,7 @@ #define EXPAND1(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__)))) #define EXPAND(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__)))) -#define STRING_Length(x) 1+strlen(x) +#define STRING_Length(x) stringLength(strlen(x)) #define ONE(...) 1 @@ -122,8 +122,11 @@ #define _INTEGER_WRITE(x, type, ...) buffer = integerWrite(buffer, x, type); #define INTEGER_Write(x, ...) _INTEGER_WRITE(x, ##__VA_ARGS__ , Unsigned) +#define STRING_Write(x) buffer = stringWrite(buffer, x); + #define INTEGER_Write_id() INTEGER_Write #define ARRAY_Write_id() ARRAY_Write +#define STRING_Write_id() STRING_Write #define ARRAY_Write(contents) buffer = arrayWrite(buffer, contents(ONE, +)); contents(WRITE, NOTHING) @@ -139,6 +142,4 @@ EXPAND(definition(WRITE)) \ } -// TODO: assign values here! - #endif // BUFFERS_H \ No newline at end of file diff --git a/src/userland/buffersTests/include/buffers.h b/src/userland/buffersTests/include/buffers.h index 39995d6..4ec2b12 100644 --- a/src/userland/buffersTests/include/buffers.h +++ b/src/userland/buffersTests/include/buffers.h @@ -104,7 +104,7 @@ #define EXPAND1(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__)))) #define EXPAND(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__)))) -#define STRING_Length(x) 1+strlen(x) +#define STRING_Length(x) stringLength(strlen(x)) #define ONE(...) 1 @@ -122,8 +122,11 @@ #define _INTEGER_WRITE(x, type, ...) buffer = integerWrite(buffer, x, type); #define INTEGER_Write(x, ...) _INTEGER_WRITE(x, ##__VA_ARGS__ , Unsigned) +#define STRING_Write(x) buffer = stringWrite(buffer, x); + #define INTEGER_Write_id() INTEGER_Write #define ARRAY_Write_id() ARRAY_Write +#define STRING_Write_id() STRING_Write #define ARRAY_Write(contents) buffer = arrayWrite(buffer, contents(ONE, +)); contents(WRITE, NOTHING) @@ -139,6 +142,4 @@ EXPAND(definition(WRITE)) \ } -// TODO: assign values here! - #endif // BUFFERS_H \ No newline at end of file diff --git a/src/userland/buffersTests/main.c b/src/userland/buffersTests/main.c index aff8951..483eaf2 100644 --- a/src/userland/buffersTests/main.c +++ b/src/userland/buffersTests/main.c @@ -151,6 +151,46 @@ FORMATS(FILL_SPOTS_X, NOTHING); } +uint32_t stringLength(uint32_t strlength) { + if ((strlength & 0x1F) == strlength) { + // fixstr + return 1 + strlength; + } + if ((strlength & 0xFF) == strlength) { + // str8 + return 2 + strlength; + } + if ((strlength & 0xFFFF) == strlength) { + // str16 + return 3 + strlength; + } + // str32 + return 5 + strlength; +} + +void *stringWrite(void *buffer, char *string) { + uint32_t length = strlen(string); + uint8_t *bufferByte = buffer; + if ((length & 0x1F) == length) { + *bufferByte = formatInfo[FORMAT_FIXSTR].min + (uint8_t) length; + buffer++; + } else if ((length & 0xFF) == length) { + *bufferByte = formatInfo[FORMAT_STR8].min; + *(uint8_t *)(buffer + 1) = (uint8_t) length; + buffer += 2; + } else if ((length & 0xFFFF) == length) { + *bufferByte = formatInfo[FORMAT_STR16].min; + *(uint16_t *)(buffer + 1) = (uint16_t) length; + buffer += 3; + } else { + *bufferByte = formatInfo[FORMAT_STR32].min; + *(uint32_t *)(buffer + 1) = (uint32_t) length; + buffer += 5; + } + memcpy(string, buffer, length); + return buffer + length; +} + uint32_t integerLength(int32_t value, IntegerType integerType) { if ((value & 0x7F) == value || ((~value) & 0x1F) == ~value) { // fixint @@ -267,8 +307,8 @@ #define SAMPLE_2_ARRAY_CONTENT(X, S) \ X(INTEGER, 1) S \ + X(STRING, "hi") S \ X(INTEGER, 500, Signed) - // X(STRING, "hi") \ #define SAMPLE_2(X) \ X(ARRAY, SAMPLE_2_ARRAY_CONTENT)