diff --git a/qstring.c b/qstring.c index ad1776949d..740a1067d2 100644 --- a/qstring.c +++ b/qstring.c @@ -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) diff --git a/qstring.h b/qstring.h index c065331f3c..6aaa7d5c83 100644 --- a/qstring.h +++ b/qstring.h @@ -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);