QString: Introduce qstring_from_substr()

Note that we can now write qstring_from_str() as a wrapper.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Luiz Capitulino 2009-11-18 23:05:27 -02:00 committed by Anthony Liguori
parent 764c1caeb3
commit 4b5c5766d0
2 changed files with 25 additions and 14 deletions

View File

@ -30,6 +30,29 @@ QString *qstring_new(void)
return qstring_from_str("");
}
/**
* qstring_from_substr(): Create a new QString from a C string substring
*
* Return string reference
*/
QString *qstring_from_substr(const char *str, int start, int end)
{
QString *qstring;
qstring = qemu_malloc(sizeof(*qstring));
qstring->length = end - start + 1;
qstring->capacity = qstring->length;
qstring->string = qemu_malloc(qstring->capacity + 1);
memcpy(qstring->string, str + start, qstring->length);
qstring->string[qstring->length] = 0;
QOBJECT_INIT(qstring, &qstring_type);
return qstring;
}
/**
* qstring_from_str(): Create a new QString from a regular C string
*
@ -37,20 +60,7 @@ QString *qstring_new(void)
*/
QString *qstring_from_str(const char *str)
{
QString *qstring;
qstring = qemu_malloc(sizeof(*qstring));
qstring->length = strlen(str);
qstring->capacity = qstring->length;
qstring->string = qemu_malloc(qstring->capacity + 1);
memcpy(qstring->string, str, qstring->length);
qstring->string[qstring->length] = 0;
QOBJECT_INIT(qstring, &qstring_type);
return qstring;
return qstring_from_substr(str, 0, strlen(str) - 1);
}
static void capacity_increase(QString *qstring, size_t len)

View File

@ -13,6 +13,7 @@ typedef struct QString {
QString *qstring_new(void);
QString *qstring_from_str(const char *str);
QString *qstring_from_substr(const char *str, int start, int end);
const char *qstring_get_str(const QString *qstring);
void qstring_append_int(QString *qstring, int64_t value);
void qstring_append(QString *qstring, const char *str);