minor optimization of strncmp for literal comparison

This commit is contained in:
veclavtalica
2025-01-15 04:36:00 +03:00
parent 9d0a2cab81
commit 760515c551
2 changed files with 17 additions and 14 deletions

View File

@ -22,6 +22,12 @@ static const char *audio_exts[AUDIO_FILE_TYPE_COUNT] = {
".xm", /* AUDIO_FILE_TYPE_XM */
};
static const uint8_t audio_exts_len[AUDIO_FILE_TYPE_COUNT] = {
sizeof ".ogg" - 1,
sizeof ".wav" - 1,
sizeof ".xm" - 1,
};
/* TODO: allow for vectorization and packed vectors (alignment care and alike) */
/* TODO: count frames without use, free the memory when threshold is met */
@ -59,14 +65,10 @@ static int64_t get_audio_data(const char *path, unsigned char **data) {
static AudioFileType infer_audio_file_type(const char *path) {
size_t path_len = SDL_strlen(path);
size_t const path_len = SDL_strlen(path);
for (int i = 0; i < AUDIO_FILE_TYPE_COUNT; ++i) {
size_t ext_length = SDL_strlen(audio_exts[i]);
if (path_len <= ext_length)
continue;
if (SDL_strcmp(&path[path_len - ext_length], audio_exts[i]) == 0)
if (SDL_strncmp(&path[path_len - audio_exts_len[i]], audio_exts[i], audio_exts_len[i]) == 0)
return (AudioFileType)i;
}
@ -297,17 +299,17 @@ TWN_API void audio_parameter(const char *channel, const char *param, float value
return;
}
if (SDL_strcmp(param, "repeat") == 0) {
if (SDL_strncmp(param, "repeat", sizeof "repeat" - 1) == 0) {
pair->value.repeat = (bool)value;
} else if (SDL_strcmp(param, "volume") == 0) {
} else if (SDL_strncmp(param, "volume", sizeof "volume" - 1) == 0) {
if (value > 1.0f || value < 0.0f) {
log_warn("Out of range volume for channel %s set", channel);
value = clampf(value, 0.0f, 1.0f);
}
pair->value.volume = value;
} else if (SDL_strcmp(param, "panning") == 0) {
} else if (SDL_strncmp(param, "panning", sizeof "panning" - 1) == 0) {
if (value > 1.0f || value < -1.0f) {
log_warn("Out of range panning for channel %s set", channel);
value = clampf(value, -1.0f, +1.0f);