1
0
Fork 0
forked from len0rd/rockbox

add itoa(), replace snprintf("%d") calls

we can save some space and still end up with a 20% faster function

Change-Id: Ia58900122944b8527ef01a673afe18ea794acb41
This commit is contained in:
William Wilgus 2025-02-03 01:05:21 -05:00 committed by William Wilgus
parent 9e61d53c7c
commit f55fe21f66
9 changed files with 98 additions and 41 deletions

View file

@ -256,7 +256,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
if (id3->disc_string)
return id3->disc_string;
if (id3->discnum) {
snprintf(buf, buf_size, "%d", id3->discnum);
itoa(buf, buf_size, id3->discnum);
return buf;
}
return NULL;
@ -264,7 +264,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
if (id3->track_string)
return id3->track_string;
if (id3->tracknum) {
snprintf(buf, buf_size, "%d", id3->tracknum);
itoa(buf, buf_size, id3->tracknum);
return buf;
}
return NULL;
@ -291,7 +291,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
if( id3->year_string )
return id3->year_string;
if (id3->year) {
snprintf(buf, buf_size, "%d", id3->year);
itoa(buf, buf_size, id3->year);
return buf;
}
return NULL;
@ -299,7 +299,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
return id3->comment;
case SKIN_TOKEN_FILE_BITRATE:
if(id3->bitrate)
snprintf(buf, buf_size, "%d", id3->bitrate);
itoa(buf, buf_size, id3->bitrate);
else
return "?";
return buf;
@ -360,12 +360,12 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
return get_codectype(id3);
case SKIN_TOKEN_FILE_FREQUENCY:
snprintf(buf, buf_size, "%ld", id3->frequency);
itoa(buf, buf_size, id3->frequency);
return buf;
case SKIN_TOKEN_FILE_FREQUENCY_KHZ:
/* ignore remainders < 100, so 22050 Hz becomes just 22k */
if ((id3->frequency % 1000) < 100)
snprintf(buf, buf_size, "%ld", id3->frequency / 1000);
itoa(buf, buf_size, id3->frequency / 1000);
else
snprintf(buf, buf_size, "%ld.%lu",
id3->frequency / 1000,
@ -374,24 +374,24 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
case SKIN_TOKEN_FILE_VBR:
return (id3->vbr) ? "(avg)" : NULL;
case SKIN_TOKEN_FILE_SIZE:
snprintf(buf, buf_size, "%ld", id3->filesize / 1024);
itoa(buf, buf_size, id3->filesize / 1024);
return buf;
#ifdef HAVE_TAGCACHE
case SKIN_TOKEN_DATABASE_PLAYCOUNT:
if (intval)
*intval = id3->playcount + 1;
snprintf(buf, buf_size, "%ld", id3->playcount);
itoa(buf, buf_size, id3->playcount);
return buf;
case SKIN_TOKEN_DATABASE_RATING:
if (intval)
*intval = id3->rating + 1;
snprintf(buf, buf_size, "%d", id3->rating);
itoa(buf, buf_size, id3->rating);
return buf;
case SKIN_TOKEN_DATABASE_AUTOSCORE:
if (intval)
*intval = id3->score + 1;
snprintf(buf, buf_size, "%d", id3->score);
itoa(buf, buf_size, id3->score);
return buf;
#endif
@ -473,7 +473,7 @@ const char *get_radio_token(struct wps_token *token, int preset_offset,
region_data->freq_step, buf, buf_size);
#ifdef HAVE_RADIO_RSSI
case SKIN_TOKEN_TUNER_RSSI:
snprintf(buf, buf_size, "%d",tuner_get(RADIO_RSSI));
itoa(buf, buf_size,tuner_get(RADIO_RSSI));
if (intval)
{
int val = tuner_get(RADIO_RSSI);
@ -490,10 +490,10 @@ const char *get_radio_token(struct wps_token *token, int preset_offset,
}
return buf;
case SKIN_TOKEN_TUNER_RSSI_MIN:
snprintf(buf, buf_size, "%d",tuner_get(RADIO_RSSI_MIN));
itoa(buf, buf_size,tuner_get(RADIO_RSSI_MIN));
return buf;
case SKIN_TOKEN_TUNER_RSSI_MAX:
snprintf(buf, buf_size, "%d",tuner_get(RADIO_RSSI_MAX));
itoa(buf, buf_size,tuner_get(RADIO_RSSI_MAX));
return buf;
#endif
case SKIN_TOKEN_PRESET_NAME:
@ -515,11 +515,11 @@ const char *get_radio_token(struct wps_token *token, int preset_offset,
format_freq_MHz(radio_get_preset_freq(preset),
region_data->freq_step, buf, buf_size);
else
snprintf(buf, buf_size, "%d", preset + 1);
itoa(buf, buf_size, preset + 1);
return buf;
}
case SKIN_TOKEN_PRESET_COUNT:
snprintf(buf, buf_size, "%d", radio_preset_count());
itoa(buf, buf_size, radio_preset_count());
if (intval)
*intval = radio_preset_count();
return buf;
@ -777,7 +777,7 @@ static const char* get_rtc_token_value(struct wps_token *token,
case SKIN_TOKEN_RTC_12HOUR_CFG:
snprintf(buf, buf_size, "%d", global_settings.timeformat);
itoa(buf, buf_size, global_settings.timeformat);
numeric_ret = global_settings.timeformat + 1;
break;
case SKIN_TOKEN_RTC_DAY_OF_MONTH:
@ -1110,14 +1110,14 @@ const char *get_token_value(struct gui_wps *gwps,
case SKIN_TOKEN_PLAYLIST_ENTRIES:
numeric_ret = playlist_amount();
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
case SKIN_TOKEN_LIST_TITLE_TEXT:
return sb_get_title(gwps->display->screen_type);
case SKIN_TOKEN_LIST_TITLE_ICON:
numeric_ret = sb_get_icon(gwps->display->screen_type);
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
case SKIN_TOKEN_LIST_ITEM_TEXT:
@ -1128,17 +1128,17 @@ const char *get_token_value(struct gui_wps *gwps,
}
case SKIN_TOKEN_LIST_ITEM_ROW:
numeric_ret = skinlist_get_item_row() + 1;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
case SKIN_TOKEN_LIST_ITEM_COLUMN:
numeric_ret = skinlist_get_item_column() + 1;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
case SKIN_TOKEN_LIST_ITEM_NUMBER:
numeric_ret = skinlist_get_item_number() + 1;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
case SKIN_TOKEN_LIST_ITEM_IS_SELECTED:
@ -1149,7 +1149,7 @@ const char *get_token_value(struct gui_wps *gwps,
if (!li) return NULL;
int icon = skinlist_get_item_icon(li->offset, li->wrap);
numeric_ret = icon;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
}
@ -1160,7 +1160,7 @@ const char *get_token_value(struct gui_wps *gwps,
case SKIN_TOKEN_PLAYLIST_POSITION:
numeric_ret = playlist_get_display_index()+offset;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
@ -1248,7 +1248,7 @@ const char *get_token_value(struct gui_wps *gwps,
}
if (l > -1) {
snprintf(buf, buf_size, "%d", l);
itoa(buf, buf_size, l);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
} else {
@ -1327,13 +1327,13 @@ const char *get_token_value(struct gui_wps *gwps,
break;
}
snprintf(buf, buf_size, "%d", numeric_ret-1);
itoa(buf, buf_size, numeric_ret-1);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
}
case SKIN_TOKEN_REPEAT_MODE:
snprintf(buf, buf_size, "%d", global_settings.repeat_mode);
itoa(buf, buf_size, global_settings.repeat_mode);
numeric_ret = global_settings.repeat_mode + 1;
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
@ -1354,7 +1354,7 @@ const char *get_token_value(struct gui_wps *gwps,
left : right;
val = peak_meter_scale_value(val, limit==1 ? MAX_PEAK : limit);
numeric_ret = val;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
data->peak_meter_enabled = true;
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
@ -1362,10 +1362,10 @@ const char *get_token_value(struct gui_wps *gwps,
case SKIN_TOKEN_CROSSFADE:
#ifdef HAVE_CROSSFADE
snprintf(buf, buf_size, "%d", global_settings.crossfade);
itoa(buf, buf_size, global_settings.crossfade);
numeric_ret = global_settings.crossfade + 1;
#else
snprintf(buf, buf_size, "%d", 0);
itoa(buf, buf_size, 0);
#endif
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
@ -1753,7 +1753,7 @@ const char *get_token_value(struct gui_wps *gwps,
{
int curr_screen = get_current_activity();
numeric_ret = curr_screen;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
}
@ -1767,7 +1767,7 @@ const char *get_token_value(struct gui_wps *gwps,
char *skin_base = get_skin_buffer(data);
struct skin_var* var = SKINOFFSETTOPTR(skin_base, token->value.data);
numeric_ret = var->value;
snprintf(buf, buf_size, "%d", numeric_ret);
itoa(buf, buf_size, numeric_ret);
numeric_buf = buf;
goto gtv_ret_numeric_tag_info;
}

View file

@ -18,12 +18,12 @@
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include "config.h"
#include "font.h"
#include "kernel.h"
#include "string.h" /* for memcmp oO*/
#include "string-extra.h" /* for itoa */
#include "sound.h"
#include "settings.h"
#include "viewport.h"
@ -611,7 +611,7 @@ static int write_bitmap_number(struct screen * display, int value,
int x, int y)
{
char buf[12], *ptr;
snprintf(buf, sizeof(buf), "%d", value);
itoa(buf, sizeof(buf), value);
for (ptr = buf; *ptr != '\0'; ptr++, x += BM_GLYPH_WIDTH)
display->mono_bitmap(bitmap_glyphs_4x8[*ptr - '0'], x, y,

View file

@ -519,7 +519,7 @@ static const char * id3_get_or_speak_info(int selected_item, void* data,
case LANG_TAGNAVI_ALL_TRACKS:
if (info->track_ct <= 1)
return NULL;
snprintf(buffer, buffer_len, "%d", info->track_ct);
itoa(buffer, buffer_len, info->track_ct);
val = buffer;
if(say_it)
talk_number(info->track_ct, true);
@ -558,7 +558,7 @@ static const char * id3_get_or_speak_info(int selected_item, void* data,
}
else if (id3->discnum)
{
snprintf(buffer, buffer_len, "%d", id3->discnum);
itoa(buffer, buffer_len, id3->discnum);
val = buffer;
if(say_it)
talk_number(id3->discnum, true);
@ -573,7 +573,7 @@ static const char * id3_get_or_speak_info(int selected_item, void* data,
}
else if (id3->tracknum)
{
snprintf(buffer, buffer_len, "%d", id3->tracknum);
itoa(buffer, buffer_len, id3->tracknum);
val = buffer;
if(say_it)
talk_number(id3->tracknum, true);
@ -602,7 +602,7 @@ static const char * id3_get_or_speak_info(int selected_item, void* data,
}
else if (id3->year)
{
snprintf(buffer, buffer_len, "%d", id3->year);
itoa(buffer, buffer_len, id3->year);
val = buffer;
if(say_it)
talk_value(id3->year, UNIT_DATEYEAR, true);

View file

@ -456,7 +456,7 @@ void cfg_to_string(const struct settings_list *setting, char* buf, int buf_len)
break; /* we got a value */
}
snprintf(buf, buf_len, "%d", *(int*)setting->setting);
itoa(buf, buf_len, *(int*)setting->setting);
break;
case F_T_BOOL:
cfg_int_to_string(setting, *(bool*)setting->setting, buf, buf_len);

View file

@ -806,7 +806,7 @@ static void volume_limit_load_from_cfg(void* var, char*value)
static char* volume_limit_write_to_cfg(void* setting, char*buf, int buf_len)
{
int current = *(int*)setting;
snprintf(buf, buf_len, "%d", current);
itoa(buf, buf_len, current);
return buf;
}
static bool volume_limit_is_changed(void* setting, void* defaultval)

View file

@ -239,6 +239,15 @@ static const char * const tag_type_str[] = {
#define logf_clauses logf
#endif /* ndef LOGF_ENABLE */
#if !defined(itoa)
char *itoa(char *buf, size_t bufsz, long int i)
{
snprintf(buf, bufsz, "%ld", i);
return buf;
}
#endif
/* Status information of the tagcache. */
static struct tagcache_stat tc_stat;
@ -1912,7 +1921,7 @@ static bool get_next(struct tagcache_search *tcs, bool is_numeric, char *buf, lo
if (is_numeric)
{
snprintf(buf, bufsz, "%ld", tcs->position);
itoa(buf, bufsz, tcs->position);
tcs->result = buf;
tcs->result_len = strlen(buf) + 1;
return true;
@ -3982,7 +3991,7 @@ bool tagcache_create_changelog(struct tagcache_search *tcs)
{
if (TAGCACHE_IS_NUMERIC(j))
{
snprintf(temp, sizeof temp, "%d", (int)idx.tag_seek[j]);
itoa(temp, sizeof temp, (int)idx.tag_seek[j]);
write_tag(clfd, tagcache_tag_to_str(j), temp);
continue;
}

View file

@ -219,6 +219,7 @@ target/hosted/maemo/maemo-thread.c
#ifndef BOOTLOADER
chunk_alloc.c
common/strptokspn.c
common/itoa.c
common/ap_int.c
#endif
common/version.c

45
firmware/common/itoa.c Normal file
View file

@ -0,0 +1,45 @@
#include <stddef.h>
#include "string.h"
#include "strmemccpy.h"
/* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
Written by James Clark (jjc@jclark.com)
This file was part of groff.
groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with groff; see the file COPYING. If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#define INT_DIGITS 19 /* enough for 64 bit integer */
char *itoa(char *buf, size_t bufsz, long int i)
{
/* Room for INT_DIGITS digits, - and '\0' */
static char intbuf[INT_DIGITS + 2];
char *p = intbuf + INT_DIGITS + 1; /* points to terminating '\0' */
if (i >= 0) {
do {
*--p = '0' + (i % 10);
i /= 10;
} while (i != 0);
}
else { /* i < 0 */
do {
*--p = '0' - (i % 10);
i /= 10;
} while (i != 0);
*--p = '-';
}
strmemccpy(buf, p, bufsz);
return buf;
}

View file

@ -70,4 +70,6 @@ static inline char * strmemcpy(char *dst, const char *src, size_t len)
strmemdupa(__s, MIN(__n, __len)); })
#endif /* strndupa */
char *itoa(char *buf, size_t bufsz, long int i); /* Not std */
#endif /* STRING_EXTRA_H */