MCPcopy Create free account
hub / github.com/documentdb/documentdb / CompareStrings

Function CompareStrings

pg_documentdb_core/src/query/bson_compare.c:1460–1496  ·  view source on GitHub ↗

* Compares two strings with an optional collation. */

Source from the content-addressed store, hash-verified

1458 * Compares two strings with an optional collation.
1459 */
1460int
1461CompareStrings(const char *left, uint32_t leftLength, const char *right, uint32_t
1462 rightLength, const char *collationString)
1463{
1464 uint32_t minLength = leftLength < rightLength ? leftLength : rightLength;
1465 if (minLength == 0)
1466 {
1467 return leftLength - rightLength;
1468 }
1469
1470 /* simple collation also uses binary comparison */
1471 if (!IsCollationValid(collationString) ||
1472 IsSimpleCollation(collationString))
1473 {
1474 int32_t cmp = memcmp(left, right, minLength);
1475 if (cmp != 0)
1476 {
1477 return cmp;
1478 }
1479
1480 /*
1481 * memcmp only inspects the first minLength bytes; if those are equal,
1482 * the longer string sorts after the shorter one (e.g. "cafe" < "cafes").
1483 */
1484 return leftLength - rightLength;
1485 }
1486
1487 /*
1488 * ucol_strcollUTF8 already takes the full strings into account and applies
1489 * the configured collation strength, so its result is final. Applying a
1490 * byte-length tiebreaker here would incorrectly report collation-equal
1491 * strings of different byte lengths (e.g. "cafe" vs "café" at strength 1)
1492 * as unequal.
1493 */
1494 return StringCompareWithCollation(left, leftLength, right, rightLength,
1495 collationString);
1496}
1497
1498
1499/*

Callers 4

ComparePgbsonQueryFunction · 0.85
CompareBsonIterFunction · 0.85
CompareBsonValueFunction · 0.85
ProcessDollarStrCaseCmpFunction · 0.85

Calls 3

IsCollationValidFunction · 0.85
IsSimpleCollationFunction · 0.85

Tested by

no test coverage detected