61 lines
1.7 KiB
Text
61 lines
1.7 KiB
Text
```
|
||
#include <stdio.h>
|
||
#include <array/length-prefix.h>
|
||
```
|
||
|
||
morph_array_as_static_to_lenpfx (Len: ℤ, T: Type)
|
||
<Seq~<StaticLength Len> T>
|
||
--> <Seq~<LengthPrefix native.UInt64> T>
|
||
```
|
||
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
|
||
for( nativeUInt64 i = 0; i < Len; ++i )
|
||
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, src->items[i] );
|
||
```
|
||
|
||
morph_array_as_lenpfx_to_static (Len: ℤ, T: Type)
|
||
<Seq~<LengthPrefix native.UInt64> T>
|
||
--> <Seq~<StaticLength Len> T>
|
||
```
|
||
nativeUInt64 i;
|
||
for( i = 0; i < Len && i < src->len; ++i )
|
||
dst->items[i] = src->items[i];
|
||
|
||
if( i < Len )
|
||
memset( &dst[i], 0, (Len-i) * sizeof(T) );
|
||
```
|
||
|
||
morph_array_as_valterm_to_lenpfx (T: Type, Terminator:T)
|
||
<Seq~<ValueTerminated Terminator> T>
|
||
--> <Seq~<LengthPrefix native.UInt64> T>
|
||
```
|
||
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst );
|
||
while( *src != Terminator )
|
||
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, *src++ );
|
||
```
|
||
|
||
morph_array_as_lenpfx_to_valterm (T: Type, Terminator: T)
|
||
<Seq~<LengthPrefix native.UInt64> T>
|
||
--> <Seq~<ValueTerminated Terminator> T>
|
||
```
|
||
for( uint64_t i = 0; i < src->len; ++i )
|
||
*dst++ = src->items[i];
|
||
|
||
*dst = Terminator;
|
||
```
|
||
|
||
morph_array_as_lenpfx_to_continuation_bit (T:Type)
|
||
<Seq~<LengthPrefix native.UInt64> T>
|
||
--> <Seq~MsbCont T>
|
||
```
|
||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||
const size_t n_bits = 8*sizeof(T);
|
||
if( src->items[i] & ((uint64_t)1<<(n_bits-1)) ) {
|
||
fprintf(stderr, "error: value has MSB set, while being used in MsbContinuation sequence!\n");
|
||
return -1;
|
||
}
|
||
|
||
dst[i] = src->items[i];
|
||
if( i+1 < src->len )
|
||
dst[i] |= ((uint64_t)1<<(n_bits-1));
|
||
}
|
||
```
|