summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dickens <christopher.a.dickens@gmail.com>2020-03-26 21:38:45 -0700
committerChris Dickens <christopher.a.dickens@gmail.com>2020-03-26 21:38:45 -0700
commit1d67425879c3ae515e97b73df94bf2e7336178b0 (patch)
tree6836e21a88e894b5a65ddf09bb677499bce0c895
parent1201bccf857aa2c126f6f568f5255b0aa6027ac2 (diff)
downloadlibusb-1d67425879c3ae515e97b73df94bf2e7336178b0.tar.gz
strerror: Micro-optimize implementation and remove use of strncasecmp()
When comparing the user-provided locale string in libusb_setlocale(), we only care about the first two characters. The lookup strings within libusb are always lowercase, so simplify the comparison by directly comparing the result of tolower(). This removes the need for strncasecmp() which was not able to be cleanly included in the first place. Rather than storing an index into the array of localized strings, store a pointer to the selected sub-array instead. This trims the generated code of libusb_strerror() by nearly half as loads can be dropped. Also constify the arrays of strings as they should not be mutable. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
-rwxr-xr-x[-rw-r--r--]libusb/strerror.c42
-rw-r--r--libusb/version_nano.h2
2 files changed, 19 insertions, 25 deletions
diff --git a/libusb/strerror.c b/libusb/strerror.c
index c74052f..920447d 100644..100755
--- a/libusb/strerror.c
+++ b/libusb/strerror.c
@@ -19,16 +19,8 @@
#include "libusbi.h"
+#include <ctype.h>
#include <string.h>
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-
-#if defined(_MSC_VER)
-#define strncasecmp _strnicmp
-#endif
-
-static size_t usbi_locale;
/** \ingroup libusb_misc
* How to add a new \ref libusb_strerror() translation:
@@ -49,15 +41,15 @@ static size_t usbi_locale;
* "Success",
* ...
* "Other error",
- * }
+ * },
* };\endcode </li>
* <li> Translate each of the English messages from the section you copied into your language </li>
* <li> Save the file (in UTF-8 format) and send it to \c libusb-devel\@lists.sourceforge.net </li>
* </ol>
*/
-static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru", "de", "hu" };
-static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
+static const char * const usbi_locale_supported[] = { "en", "nl", "fr", "ru", "de", "hu" };
+static const char * const usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUSB_ERROR_COUNT] = {
{ /* English (en) */
"Success",
"Input/Output Error",
@@ -118,7 +110,6 @@ static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUS
"Память исчерпана",
"Операция не поддерживается данной платформой",
"Неизвестная ошибка"
-
}, { /* German (de) */
"Erfolgreich",
"Eingabe-/Ausgabefehler",
@@ -149,9 +140,11 @@ static const char* usbi_localized_errors[ARRAYSIZE(usbi_locale_supported)][LIBUS
"Nincs elég memória",
"A művelet nem támogatott ezen a rendszeren",
"Általános hiba",
- }
+ },
};
+static const char * const (*usbi_error_strings)[LIBUSB_ERROR_COUNT] = &usbi_localized_errors[0];
+
/** \ingroup libusb_misc
* Set the language, and only the language, not the encoding! used for
* translatable libusb messages.
@@ -186,19 +179,20 @@ int API_EXPORTED libusb_setlocale(const char *locale)
{
size_t i;
- if ( (locale == NULL) || (strlen(locale) < 2)
- || ((strlen(locale) > 2) && (locale[2] != '-') && (locale[2] != '_') && (locale[2] != '.')) )
+ if (!locale || strlen(locale) < 2
+ || (locale[2] != '\0' && locale[2] != '-' && locale[2] != '_' && locale[2] != '.'))
return LIBUSB_ERROR_INVALID_PARAM;
- for (i=0; i<ARRAYSIZE(usbi_locale_supported); i++) {
- if (strncasecmp(usbi_locale_supported[i], locale, 2) == 0)
+ for (i = 0; i < ARRAYSIZE(usbi_locale_supported); i++) {
+ if (usbi_locale_supported[i][0] == tolower((unsigned char)locale[0])
+ && usbi_locale_supported[i][1] == tolower((unsigned char)locale[1]))
break;
}
- if (i >= ARRAYSIZE(usbi_locale_supported)) {
+
+ if (i == ARRAYSIZE(usbi_locale_supported))
return LIBUSB_ERROR_NOT_FOUND;
- }
- usbi_locale = i;
+ usbi_error_strings = &usbi_localized_errors[i];
return LIBUSB_SUCCESS;
}
@@ -216,14 +210,14 @@ int API_EXPORTED libusb_setlocale(const char *locale)
* \param errcode the error code whose description is desired
* \returns a short description of the error code in UTF-8 encoding
*/
-DEFAULT_VISIBILITY const char* LIBUSB_CALL libusb_strerror(int errcode)
+DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_strerror(int errcode)
{
int errcode_index = -errcode;
- if ((errcode_index < 0) || (errcode_index >= LIBUSB_ERROR_COUNT)) {
+ if (errcode_index < 0 || errcode_index >= LIBUSB_ERROR_COUNT) {
/* "Other Error", which should always be our last message, is returned */
errcode_index = LIBUSB_ERROR_COUNT - 1;
}
- return usbi_localized_errors[usbi_locale][errcode_index];
+ return (*usbi_error_strings)[errcode_index];
}
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index 037f308..b362d2c 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11474
+#define LIBUSB_NANO 11475