* Convenience function to generate a collation aware sortkey that can be used in strcmp(). * * Two calls to ucol_getSortKey() is a pattern used in pg code. This is to know the expected size * of the sort key so that we allocate a larger buffer if needed. * Reference: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/ucol_8h.html#a58be2c76d01184cb1821ff0af28081c2 * Reference: https:
| 531 | * Reference: https://unicode-org.github.io/icu/userguide/collation/api.html |
| 532 | */ |
| 533 | inline char * |
| 534 | GetCollationSortKey(const char *collationString, char *key, int keyLength) |
| 535 | { |
| 536 | ucollator_cache_entry *collation_entry = LookupUCollatorCache(collationString); |
| 537 | |
| 538 | uint8_t *sortKeyPtr = palloc(DEFAULT_ICU_COLLATION_SORT_KEY_LENGTH); |
| 539 | UChar *uchar; |
| 540 | int32_t ulen; |
| 541 | |
| 542 | ulen = icu_to_uchar_core(&uchar, key, keyLength); |
| 543 | Size expectedLength = ucol_getSortKey(collation_entry->collator, uchar, ulen, |
| 544 | sortKeyPtr, |
| 545 | DEFAULT_ICU_COLLATION_SORT_KEY_LENGTH); |
| 546 | if (expectedLength > DEFAULT_ICU_COLLATION_SORT_KEY_LENGTH) |
| 547 | { |
| 548 | sortKeyPtr = repalloc(sortKeyPtr, expectedLength); |
| 549 | ucol_getSortKey(collation_entry->collator, uchar, ulen, sortKeyPtr, |
| 550 | expectedLength); |
| 551 | } |
| 552 | |
| 553 | pfree(uchar); |
| 554 | return (char *) sortKeyPtr; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | /* |
no test coverage detected