X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Fstring.c;h=edd588da1492a26a0acb2dc6d2998af4392416cf;hb=f7565b5f3238f1229608131e6358350538e262fa;hp=e6297f9eed89b0282a2b104fab360921eb723b07;hpb=e681f34ec1af4b19f9dfd9c0e8d9c29beaf3e2db;p=i3%2Fi3 diff --git a/libi3/string.c b/libi3/string.c index e6297f9e..edd588da 100644 --- a/libi3/string.c +++ b/libi3/string.c @@ -2,25 +2,26 @@ * vim:ts=4:sw=4:expandtab * * i3 - an improved dynamic tiling window manager - * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE) + * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * * string.c: Define an i3String type to automagically handle UTF-8/UCS-2 * conversions. Some font backends need UCS-2 (X core fonts), * others want UTF-8 (Pango). * */ +#include "libi3.h" #include #include -#include "libi3.h" +#include struct _i3String { char *utf8; xcb_char2b_t *ucs2; size_t num_glyphs; size_t num_bytes; - bool is_markup; + bool pango_markup; }; /* @@ -29,7 +30,7 @@ struct _i3String { * */ i3String *i3string_from_utf8(const char *from_utf8) { - i3String *str = scalloc(sizeof(i3String)); + i3String *str = scalloc(1, sizeof(i3String)); /* Get the text */ str->utf8 = sstrdup(from_utf8); @@ -48,7 +49,7 @@ i3String *i3string_from_markup(const char *from_markup) { i3String *str = i3string_from_utf8(from_markup); /* Set the markup flag */ - str->is_markup = true; + str->pango_markup = true; return str; } @@ -60,10 +61,10 @@ i3String *i3string_from_markup(const char *from_markup) { * */ i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes) { - i3String *str = scalloc(sizeof(i3String)); + i3String *str = scalloc(1, sizeof(i3String)); /* Copy the actual text to our i3String */ - str->utf8 = scalloc(sizeof(char) * (num_bytes + 1)); + str->utf8 = scalloc(num_bytes + 1, 1); strncpy(str->utf8, from_utf8, num_bytes); str->utf8[num_bytes] = '\0'; @@ -82,7 +83,7 @@ i3String *i3string_from_markup_with_length(const char *from_markup, size_t num_b i3String *str = i3string_from_utf8_with_length(from_markup, num_bytes); /* set the markup flag */ - str->is_markup = true; + str->pango_markup = true; return str; } @@ -93,12 +94,11 @@ i3String *i3string_from_markup_with_length(const char *from_markup, size_t num_b * */ i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) { - i3String *str = scalloc(sizeof(i3String)); + i3String *str = scalloc(1, sizeof(i3String)); /* Copy the actual text to our i3String */ - size_t num_bytes = num_glyphs * sizeof(xcb_char2b_t); - str->ucs2 = scalloc(num_bytes); - memcpy(str->ucs2, from_ucs2, num_bytes); + str->ucs2 = scalloc(num_glyphs, sizeof(xcb_char2b_t)); + memcpy(str->ucs2, from_ucs2, num_glyphs * sizeof(xcb_char2b_t)); /* Store the length */ str->num_glyphs = num_glyphs; @@ -115,7 +115,7 @@ i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) { */ i3String *i3string_copy(i3String *str) { i3String *copy = i3string_from_utf8(i3string_as_utf8(str)); - copy->is_markup = str->is_markup; + copy->pango_markup = str->pango_markup; return copy; } @@ -175,14 +175,25 @@ size_t i3string_get_num_bytes(i3String *str) { * Whether the given i3String is in Pango markup. */ bool i3string_is_markup(i3String *str) { - return str->is_markup; + return str->pango_markup; } /* * Set whether the i3String should use Pango markup. */ -void i3string_set_markup(i3String *str, bool is_markup) { - str->is_markup = is_markup; +void i3string_set_markup(i3String *str, bool pango_markup) { + str->pango_markup = pango_markup; +} + +/* + * Escape pango markup characters in the given string. + */ +i3String *i3string_escape_markup(i3String *str) { + const char *text = i3string_as_utf8(str); + char *escaped = g_markup_escape_text(text, -1); + i3String *result = i3string_from_utf8(escaped); + free(escaped); + return result; } /*