Compare two strings. Returns: < 0 if s1 < s2 0 if s1 == s2 > 0 if s1 > s2 - s1/n1: ptr/len for the first string - s2/n2: ptr/len for the second string - len: min(n1, n2) - this can be more cheaply passed in by the caller
| 34 | /// - s2/n2: ptr/len for the second string |
| 35 | /// - len: min(n1, n2) - this can be more cheaply passed in by the caller |
| 36 | static inline int StringCompare(const char* s1, int n1, const char* s2, int n2, int len) { |
| 37 | // memcmp has undefined behavior when called on nullptr for either pointer |
| 38 | // |
| 39 | // GCC gives a warning about overflowing the size argument of memcmp, because |
| 40 | // it thinks 'len' can be negative. 'len' is never negative, so, this just uses |
| 41 | // len <= 0 and returns 0 for that case to avoid the warning. |
| 42 | const int result = (len <= 0) ? 0 : memcmp(s1, s2, len); |
| 43 | if (result != 0) return result; |
| 44 | return n1 - n2; |
| 45 | } |
| 46 | |
| 47 | inline int StringValue::Compare(const StringValue& other) const { |
| 48 | SimpleString this_s = ToSimpleString(); |
no outgoing calls