cmpForward: assume (and assert) p2 is upper-case

This commit is contained in:
Justin Viiret 2016-04-21 13:39:16 +10:00 committed by Matthew Barr
parent 8eec61445f
commit 31b1114f76

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Intel Corporation * Copyright (c) 2015-2016, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -97,9 +97,10 @@ u64a theirtoupper64(const u64a x) {
static really_inline static really_inline
int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) { int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) {
const u8 *pEnd = (const u8 *)p1 + len; const u8 *pEnd = p1 + len;
for (; p1 < pEnd; p1++, p2++) { for (; p1 < pEnd; p1++, p2++) {
if (mytolower(*p1) != mytolower(*p2)) { assert(!ourisalpha(*p2) || myisupper(*p2)); // Already upper-case.
if ((u8)mytoupper(*p1) != *p2) {
return 1; return 1;
} }
} }
@ -108,7 +109,7 @@ int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) {
static really_inline static really_inline
int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) { int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) {
const u8 *pEnd = (const u8 *)p1 + len; const u8 *pEnd = p1 + len;
for (; p1 < pEnd; p1++, p2++) { for (; p1 < pEnd; p1++, p2++) {
if (*p1 != *p2) { if (*p1 != *p2) {
return 1; return 1;
@ -129,6 +130,11 @@ int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) {
#define CMP_SIZE sizeof(CMP_T) #define CMP_SIZE sizeof(CMP_T)
/**
* \brief Compare two strings, optionally caselessly.
*
* Note: If nocase is true, p2 is assumed to be already upper-case.
*/
#if defined(ARCH_IA32) #if defined(ARCH_IA32)
static UNUSED never_inline static UNUSED never_inline
#else #else
@ -145,11 +151,13 @@ int cmpForward(const u8 *p1, const u8 *p2, size_t len, char nocase) {
if (nocase) { // Case-insensitive version. if (nocase) { // Case-insensitive version.
for (; p1 < p1_end; p1 += CMP_SIZE, p2 += CMP_SIZE) { for (; p1 < p1_end; p1 += CMP_SIZE, p2 += CMP_SIZE) {
if (TOUPPER(ULOAD(p1)) != TOUPPER(ULOAD(p2))) { assert(ULOAD(p2) == TOUPPER(ULOAD(p2))); // Already upper-case.
if (TOUPPER(ULOAD(p1)) != ULOAD(p2)) {
return 1; return 1;
} }
} }
if (TOUPPER(ULOAD(p1_end)) != TOUPPER(ULOAD(p2_end))) { assert(ULOAD(p2_end) == TOUPPER(ULOAD(p2_end))); // Already upper-case.
if (TOUPPER(ULOAD(p1_end)) != ULOAD(p2_end)) {
return 1; return 1;
} }
} else { // Case-sensitive version. } else { // Case-sensitive version.