core/string: Remove string :: utos()
I replace it with calls to g_strdup_printf(). This has the additional benefit that I can do uint conversions at the same time as other formatting options, so this seems like a win. The only downside is that I have to manually free the memory that glib allocates, but that's easy enough. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
d36457fd61
commit
82d1da491b
|
@ -2,18 +2,13 @@
|
|||
* Copyright 2015 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <core/string.h>
|
||||
#include <glib.h>
|
||||
#include <sstream>
|
||||
|
||||
#define O_MINUTES (60)
|
||||
#define O_HOURS (60 * O_MINUTES)
|
||||
#define O_DAYS (24 * O_HOURS)
|
||||
|
||||
const std::string string :: utos(unsigned int u)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << u;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
const std::string string :: sec2str(unsigned int sec)
|
||||
{
|
||||
|
@ -33,14 +28,16 @@ static const std::string _time_detail(unsigned int value,
|
|||
const std::string &field)
|
||||
{
|
||||
std::string res;
|
||||
gchar *g_res;
|
||||
|
||||
if (value > 0) {
|
||||
res += string :: utos(value) + " " + field;
|
||||
if (value > 1)
|
||||
res += "s";
|
||||
if (remaining > 0)
|
||||
res += ", ";
|
||||
g_res = g_strdup_printf("%u %s%s%s", value, field.c_str(),
|
||||
(value > 1) ? "s" : "",
|
||||
(remaining > 0) ? ", " : "");
|
||||
res = g_res;
|
||||
g_free(g_res);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,13 +3,18 @@
|
|||
*/
|
||||
#include <core/string.h>
|
||||
#include <core/tags/album.h>
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
static Database<Album> album_db("album.db", true);
|
||||
|
||||
static const std::string make_key(const std::string &name, unsigned int year)
|
||||
{
|
||||
return string :: utos(year) + "/" + name;
|
||||
gchar *g_res = g_strdup_printf("%u/%s", year, name.c_str());
|
||||
std :: string res = g_res;
|
||||
|
||||
g_free(g_res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <core/string.h>
|
||||
#include <core/tags/track.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
@ -79,9 +80,16 @@ const std::string Track :: path() const
|
|||
|
||||
const std::string Track :: primary_key() const
|
||||
{
|
||||
if (_library)
|
||||
return string :: utos(_library->index()) + "/" + _path;
|
||||
return "";
|
||||
std :: string res;
|
||||
|
||||
if (_library) {
|
||||
gchar *g_res = g_strdup_printf("%u/%s", _library->index(),
|
||||
_path.c_str());
|
||||
res = g_res;
|
||||
g_free(g_res);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Track :: played()
|
||||
|
|
|
@ -147,7 +147,9 @@ bool QueueTab :: on_key_press_event(const std::string &key)
|
|||
|
||||
void QueueTab :: queue_set_number(unsigned int num)
|
||||
{
|
||||
q_label->temp_number->set_text(string :: utos(num) + ". ");
|
||||
gchar *text = g_strdup_printf("%u. ", num);
|
||||
q_label->temp_number->set_text(text);
|
||||
g_free(text);
|
||||
}
|
||||
|
||||
void QueueTab :: on_tab_reordered()
|
||||
|
|
|
@ -33,7 +33,9 @@ CollectionLabel :: CollectionLabel(BaseObjectType *cobject,
|
|||
|
||||
void CollectionLabel :: set_size()
|
||||
{
|
||||
collection_size->set_text(string :: utos(_queue->size()));
|
||||
gchar *size = g_strdup_printf("%u", _queue->size());
|
||||
collection_size->set_text(size);
|
||||
g_free(size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -47,7 +49,9 @@ HistoryLabel :: HistoryLabel(BaseObjectType *cobject,
|
|||
|
||||
void HistoryLabel :: set_size()
|
||||
{
|
||||
history_size->set_text(string :: utos(_queue->size()));
|
||||
gchar *size = g_strdup_printf("%u", _queue->size());
|
||||
history_size->set_text(size);
|
||||
g_free(size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -77,5 +81,7 @@ void TempLabel :: set_sensitive(bool sensitive)
|
|||
|
||||
void TempLabel :: set_size()
|
||||
{
|
||||
temp_size->set_text(string :: utos(_queue->size()));
|
||||
gchar *size = g_strdup_printf("%u", _queue->size());
|
||||
temp_size->set_text(size);
|
||||
g_free(size);
|
||||
}
|
||||
|
|
|
@ -12,13 +12,6 @@
|
|||
namespace string
|
||||
{
|
||||
|
||||
/**
|
||||
* Convert an unsigned integer into a string.
|
||||
* @param u Integer to be converted.
|
||||
* @return u, in string form.
|
||||
*/
|
||||
const std::string utos(unsigned int);
|
||||
|
||||
/**
|
||||
* Convert from seconds to a time string.
|
||||
* @param sec Number of seconds.
|
||||
|
@ -43,4 +36,5 @@ namespace string
|
|||
const std::string lowercase(const std::string &);
|
||||
}
|
||||
|
||||
|
||||
#endif /* OCARINA_CORE_STRING_H */
|
||||
|
|
|
@ -21,7 +21,10 @@ public:
|
|||
|
||||
const std::string primary_key() const
|
||||
{
|
||||
return string :: utos(val);
|
||||
gchar *g_val = g_strdup_printf("%u", val);
|
||||
std::string res = g_val;
|
||||
g_free(g_val);
|
||||
return res;
|
||||
}
|
||||
|
||||
void write(File &f) { f << val; }
|
||||
|
|
|
@ -5,13 +5,6 @@
|
|||
#include <core/string.h>
|
||||
#include "test.h"
|
||||
|
||||
void test_utos()
|
||||
{
|
||||
test_equal(string :: utos(0), "0");
|
||||
test_equal(string :: utos(1), "1");
|
||||
test_equal(string :: utos(42), "42");
|
||||
test_equal(string :: utos(999), "999");
|
||||
}
|
||||
|
||||
void test_sec2str()
|
||||
{
|
||||
|
@ -71,7 +64,6 @@ void test_lowercase()
|
|||
}
|
||||
|
||||
DECLARE_UNIT_TESTS(
|
||||
UNIT_TEST("Unsigned to String", test_utos),
|
||||
UNIT_TEST("Seconds to String", test_sec2str),
|
||||
UNIT_TEST("Seconds to String (Detailed)", test_sec2str_detailed),
|
||||
UNIT_TEST("String Lowercase", test_lowercase),
|
||||
|
|
Loading…
Reference in New Issue