Files
Aerofoil/PortabilityLayer/PLStringCompare.cpp

61 lines
1.2 KiB
C++
Raw Normal View History

2019-12-12 23:37:40 -05:00
#include "PLStringCompare.h"
#include "MacRoman.h"
2019-12-12 23:37:40 -05:00
#include <string.h>
2019-12-31 05:20:07 -05:00
#include <algorithm>
2019-12-31 05:20:07 -05:00
namespace StrCmp
{
int Compare(const PLPasStr &string1, const PLPasStr &string2)
{
const uint8_t *chars1 = string1.UChars();
const uint8_t *chars2 = string2.UChars();
2019-12-12 23:37:40 -05:00
2019-12-31 05:20:07 -05:00
const size_t len1 = string1.Length();
const size_t len2 = string1.Length();
2019-12-12 23:37:40 -05:00
2019-12-31 05:20:07 -05:00
const size_t shorterLen = std::min(len1, len2);
2019-12-31 05:20:07 -05:00
int memcmpResult = memcmp(chars1, chars2, shorterLen);
2019-12-12 23:37:40 -05:00
2019-12-31 05:20:07 -05:00
if (memcmpResult != 0)
return memcmpResult;
if (len1 < len2)
return -1;
else if (len2 < len1)
return 1;
else
return 0;
2019-12-12 23:37:40 -05:00
}
2019-12-31 05:20:07 -05:00
int CompareCaseInsensitive(const PLPasStr &string1, const PLPasStr &string2)
{
2019-12-31 05:20:07 -05:00
const uint8_t *chars1 = string1.UChars();
const uint8_t *chars2 = string2.UChars();
2019-12-31 05:20:07 -05:00
const size_t len1 = string1.Length();
const size_t len2 = string1.Length();
2019-12-12 23:37:40 -05:00
2019-12-31 05:20:07 -05:00
const size_t shorterLen = std::min(len1, len2);
2019-12-31 05:20:07 -05:00
for (size_t i = 0; i < shorterLen; i++)
{
const uint8_t c1 = PortabilityLayer::MacRoman::g_toLower[chars1[i]];
const uint8_t c2 = PortabilityLayer::MacRoman::g_toLower[chars2[i]];
2019-12-12 23:37:40 -05:00
2019-12-31 05:20:07 -05:00
if (c1 < c2)
return -1;
if (c2 < c1)
return 1;
}
2019-12-31 05:20:07 -05:00
if (len1 < len2)
return -1;
else if (len2 < len1)
return 1;
else
return 0;
}
2019-12-12 23:37:40 -05:00
}