From 31b1114f76b7dd80d6d277e9d79b8f7f047dd500 Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Thu, 21 Apr 2016 13:39:16 +1000 Subject: [PATCH] cmpForward: assume (and assert) p2 is upper-case --- src/util/compare.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/util/compare.h b/src/util/compare.h index 11c01f08..eaa717a4 100644 --- a/src/util/compare.h +++ b/src/util/compare.h @@ -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 * modification, are permitted provided that the following conditions are met: @@ -97,9 +97,10 @@ u64a theirtoupper64(const u64a x) { static really_inline 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++) { - if (mytolower(*p1) != mytolower(*p2)) { + assert(!ourisalpha(*p2) || myisupper(*p2)); // Already upper-case. + if ((u8)mytoupper(*p1) != *p2) { return 1; } } @@ -108,7 +109,7 @@ int cmpNocaseNaive(const u8 *p1, const u8 *p2, size_t len) { static really_inline 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++) { if (*p1 != *p2) { return 1; @@ -129,6 +130,11 @@ int cmpCaseNaive(const u8 *p1, const u8 *p2, size_t len) { #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) static UNUSED never_inline #else @@ -145,11 +151,13 @@ int cmpForward(const u8 *p1, const u8 *p2, size_t len, char nocase) { if (nocase) { // Case-insensitive version. 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; } } - 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; } } else { // Case-sensitive version.