From a6db883d497b7a854dd97f5a6aadda693b593db9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 11 May 2015 03:14:07 -0400 Subject: [PATCH] common: Cache string length in strupper/strlower Avoids calling strlen every loop iteration. --- source/common.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/source/common.h b/source/common.h index 3f72ed6..6497751 100644 --- a/source/common.h +++ b/source/common.h @@ -23,15 +23,21 @@ typedef volatile u32 vu32; typedef volatile u64 vu64; inline char* strupper(const char* str) { - char* buffer = (char*)malloc(strlen(str) + 1); - for (int i = 0; i < strlen(str); ++i) + const size_t string_len = strlen(str); + char* buffer = (char*)malloc(string_len + 1); + + for (int i = 0; i < string_len; ++i) buffer[i] = toupper((unsigned)str[i]); + return buffer; } inline char* strlower(const char* str) { - char* buffer = (char*)malloc(strlen(str) + 1); - for (int i = 0; i < strlen(str); ++i) + const size_t string_len = strlen(str); + char* buffer = (char*)malloc(string_len + 1); + + for (int i = 0; i < string_len; ++i) buffer[i] = tolower((unsigned)str[i]); + return buffer; }