MCPcopy Create free account
hub / github.com/chen3feng/toft / CompareByteString

Function CompareByteString

base/string/compare.h:306–345  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

304}
305
306inline int CompareByteString(const void* lhs, size_t lhs_len,
307 const void* rhs, size_t rhs_len, bool* inclusive,
308 size_t* common_prefix_len = NULL)
309{
310 const unsigned char* p1 = reinterpret_cast<const unsigned char*>(lhs);
311 const unsigned char* p2 = reinterpret_cast<const unsigned char*>(rhs);
312 ptrdiff_t min_len = (lhs_len <= rhs_len) ? lhs_len : rhs_len;
313 ptrdiff_t pos = 0;
314 ptrdiff_t end_pos = min_len - sizeof(size_t) + 1;
315
316 while (pos < end_pos)
317 {
318 if (GetUnaligned<size_t>(p1 + pos) == GetUnaligned<size_t>(p2 + pos))
319 pos += sizeof(size_t);
320 else
321 break;
322 }
323
324 while ((pos < min_len) && (p1[pos] == p2[pos]))
325 pos++;
326
327 *inclusive = (pos == min_len);
328
329 if (common_prefix_len != NULL)
330 *common_prefix_len = pos;
331
332 if (*inclusive)
333 {
334 if (lhs_len > rhs_len)
335 return 1;
336 else if (lhs_len == rhs_len)
337 return 0;
338 else
339 return -1;
340 }
341 else
342 {
343 return p1[pos] - p2[pos];
344 }
345}
346
347inline int CompareByteString(
348 const void* lhs, size_t lhs_len,

Callers 1

compareMethod · 0.85

Calls 1

lengthMethod · 0.45

Tested by

no test coverage detected