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:
parent
764c1caeb3
commit
4b5c5766d0
38
qstring.c
38
qstring.c
@ -30,6 +30,29 @@ QString *qstring_new(void)
|
|||||||
return qstring_from_str("");
|
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
|
* 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_from_str(const char *str)
|
||||||
{
|
{
|
||||||
QString *qstring;
|
return qstring_from_substr(str, 0, strlen(str) - 1);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void capacity_increase(QString *qstring, size_t len)
|
static void capacity_increase(QString *qstring, size_t len)
|
||||||
|
@ -13,6 +13,7 @@ typedef struct QString {
|
|||||||
|
|
||||||
QString *qstring_new(void);
|
QString *qstring_new(void);
|
||||||
QString *qstring_from_str(const char *str);
|
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);
|
const char *qstring_get_str(const QString *qstring);
|
||||||
void qstring_append_int(QString *qstring, int64_t value);
|
void qstring_append_int(QString *qstring, int64_t value);
|
||||||
void qstring_append(QString *qstring, const char *str);
|
void qstring_append(QString *qstring, const char *str);
|
||||||
|
Loading…
Reference in New Issue
Block a user