| 353 | } |
| 354 | |
| 355 | const struct strset *strset_prefix(const struct strset *set, const char *prefix) |
| 356 | { |
| 357 | const struct strset *n, *top; |
| 358 | size_t len = strlen(prefix); |
| 359 | const u8 *bytes = (const u8 *)prefix; |
| 360 | |
| 361 | /* Empty set -> return empty set. */ |
| 362 | if (!set->u.n) |
| 363 | return set; |
| 364 | |
| 365 | top = n = set; |
| 366 | |
| 367 | /* We walk to find the top, but keep going to check prefix matches. */ |
| 368 | while (!n->u.s[0]) { |
| 369 | u8 c = 0, direction; |
| 370 | |
| 371 | /* Special node which represents the empty string. */ |
| 372 | if (unlikely(n->u.n->byte_num == (size_t)-1)) { |
| 373 | n = &n->u.n->child[0]; |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | if (n->u.n->byte_num < len) |
| 378 | c = bytes[n->u.n->byte_num]; |
| 379 | |
| 380 | direction = (c >> n->u.n->bit_num) & 1; |
| 381 | n = &n->u.n->child[direction]; |
| 382 | if (c) |
| 383 | top = n; |
| 384 | } |
| 385 | |
| 386 | if (!strstarts(n->u.s, prefix)) { |
| 387 | /* Convenient return for prefixes which do not appear in set. */ |
| 388 | static const struct strset empty_set; |
| 389 | return &empty_set; |
| 390 | } |
| 391 | |
| 392 | return top; |
| 393 | } |
| 394 | |
| 395 | /* Recursive fallback for strset_clear's OOM path. */ |
| 396 | static void clear(struct strset n) |