| 1260 | } expansion_item; |
| 1261 | |
| 1262 | static LIST * expand( expansion_item * items, int32_t const length ) |
| 1263 | { |
| 1264 | LIST * result = L0; |
| 1265 | string buf[ 1 ]; |
| 1266 | int32_t size = 0; |
| 1267 | int32_t i; |
| 1268 | |
| 1269 | assert( length > 0 ); |
| 1270 | for ( i = 0; i < length; ++i ) |
| 1271 | { |
| 1272 | LISTITER iter = list_begin( items[ i ].values ); |
| 1273 | LISTITER const end = list_end( items[ i ].values ); |
| 1274 | |
| 1275 | /* If any of the items has no values - the result is an empty list. */ |
| 1276 | if ( iter == end ) return L0; |
| 1277 | |
| 1278 | /* Set each item's 'current' to its first listed value. This indicates |
| 1279 | * each item's next value to be used when constructing the list of all |
| 1280 | * possible concatenated values. |
| 1281 | */ |
| 1282 | items[ i ].current = iter; |
| 1283 | |
| 1284 | /* Calculate the longest concatenated string length - to know how much |
| 1285 | * memory we need to allocate as a buffer for holding the concatenated |
| 1286 | * strings. |
| 1287 | */ |
| 1288 | { |
| 1289 | int32_t max = 0; |
| 1290 | for ( ; iter != end; iter = list_next( iter ) ) |
| 1291 | { |
| 1292 | int32_t const len = int32_t(strlen( object_str( list_item( iter ) ) )); |
| 1293 | if ( len > max ) max = len; |
| 1294 | } |
| 1295 | size += max; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | string_new( buf ); |
| 1300 | string_reserve( buf, size ); |
| 1301 | |
| 1302 | i = 0; |
| 1303 | while ( i >= 0 ) |
| 1304 | { |
| 1305 | for ( ; i < length; ++i ) |
| 1306 | { |
| 1307 | items[ i ].size = buf->size; |
| 1308 | string_append( buf, object_str( list_item( items[ i ].current ) ) ); |
| 1309 | } |
| 1310 | result = list_push_back( result, object_new( buf->value ) ); |
| 1311 | while ( --i >= 0 ) |
| 1312 | { |
| 1313 | if ( list_next( items[ i ].current ) != list_end( items[ i ].values |
| 1314 | ) ) |
| 1315 | { |
| 1316 | items[ i ].current = list_next( items[ i ].current ); |
| 1317 | string_truncate( buf, items[ i ].size ); |
| 1318 | break; |
| 1319 | } |
no test coverage detected