ldmc/morphisms/value_delim.morphism-base

97 lines
2.9 KiB
Text
Raw Permalink Normal View History

```
#include <array/length-prefix.h>
#include <stdlib.h>
```
morph_valsep_delim (T: Type, SrcDelim: T, DstDelim: T)
< Seq <Seq T> >
~ < ValueSep SrcDelim T >
2025-03-20 16:28:16 +01:00
~ < Seq~<LengthPrefix native.UInt64> T >
--> < Seq <Seq T> >
~ < ValueSep DstDelim T >
2025-03-20 16:28:16 +01:00
~ < Seq~<LengthPrefix native.UInt64> T >
```
2025-05-07 16:29:45 +02:00
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
uint8_t * dst_items = dst->items;
for( uint64_t i = 0; i < src->len; ++i ) {
if( src->items[i] == SrcDelim ) {
2025-05-07 16:29:45 +02:00
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, DstDelim );
} else if( src->items[i] == DstDelim ) {
if( DstDelim == '\n' ) {
2025-05-07 16:29:45 +02:00
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, '\\' );
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, 'n' );
} else {
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, '\\' );
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, DstDelim );
}
} else {
2025-05-07 16:29:45 +02:00
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, src->items[i] );
}
}
```
morph_seqseq_as_valsep_to_lenpfx (T: Type, Delim: T, EscKey: T)
< Seq <Seq T> >
~ < ValueSep T Delim >
2025-03-20 16:28:16 +01:00
~ < Seq~<LengthPrefix native.UInt64> T >
2025-03-20 16:28:16 +01:00
--> < Seq~<LengthPrefix native.UInt64>
<Seq~<LengthPrefix native.UInt64> T >
~ <RefMut < Seq~<LengthPrefix native.UInt64> T>>
~ native.Address
~ native.UInt64
>
```
2025-05-07 16:29:45 +02:00
length_prefix_nativeUInt64_array_nativeUInt64_clear( dst );
LENGTH_PREFIX_ARRAY_TYPE( nativeUInt64, T ) * cur_item = NULL;
T const * start = &src->items[0];
T const * cur = start;
T const * end = &src->items[src->len];
while( cur < end ) {
if( *cur == Delim || cur+1 == end ) {
uint64_t len = cur - start;
cur_item = malloc( sizeof(uint64_t) + sizeof(T) * len );
cur_item->len = len;
memcpy( cur_item->items, start, len );
2025-05-07 16:29:45 +02:00
length_prefix_nativeUInt64_array_nativeUInt64_push( dst, (uint64_t)cur_item );
start = ++cur;
} else {
cur++;
}
}
```
2025-03-19 17:28:26 +01:00
morph_seqeq_as_lenpfx_to_valsep (T: Type, Delim: T, EscKey: T)
2025-03-20 16:28:16 +01:00
< Seq~<LengthPrefix native.UInt64>
<Seq~<LengthPrefix native.UInt64> T >
~ <RefMut < Seq~<LengthPrefix native.UInt64> T>>
~ native.Address
~ native.UInt64
2025-03-19 17:28:26 +01:00
>
--> < Seq <Seq T> >
~ < ValueSep T Delim >
2025-03-20 16:28:16 +01:00
~ < Seq~<LengthPrefix native.UInt64> T >
2025-03-19 17:28:26 +01:00
```
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
2025-03-19 17:28:26 +01:00
for( uint64_t i = 0; i < src->len; ++i ) {
LENGTH_PREFIX_ARRAY_TYPE( nativeUInt64, T ) * item = src->items[i];
2025-03-19 17:28:26 +01:00
for( uint64_t j = 0; j < item->len; ++j ) {
PRESCAN_LENGTH_PREFIX_CALL( nativeUInt64, T, push )( items->items[j] );
2025-03-19 17:28:26 +01:00
}
if( i+1 < src->len ) {
PRESCAN_LENGTH_PREFIX_CALL( nativeUInt64, T, push )( Delim );
2025-03-19 17:28:26 +01:00
}
}
```