]> git.sur5r.net Git - i3/i3/blob - libi3/string.c
Merge branch 'master' into next
[i3/i3] / libi3 / string.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * string.c: Define an i3String type to automagically handle UTF-8/UCS-2
8  *           conversions. Some font backends need UCS-2 (X core fonts),
9  *           others want UTF-8 (Pango).
10  *
11  */
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #if PANGO_SUPPORT
17 #include <glib.h>
18 #endif
19
20 #include "libi3.h"
21
22 struct _i3String {
23     char *utf8;
24     xcb_char2b_t *ucs2;
25     size_t num_glyphs;
26     size_t num_bytes;
27     bool is_markup;
28 };
29
30 /*
31  * Build an i3String from an UTF-8 encoded string.
32  * Returns the newly-allocated i3String.
33  *
34  */
35 i3String *i3string_from_utf8(const char *from_utf8) {
36     i3String *str = scalloc(1, sizeof(i3String));
37
38     /* Get the text */
39     str->utf8 = sstrdup(from_utf8);
40
41     /* Compute and store the length */
42     str->num_bytes = strlen(str->utf8);
43
44     return str;
45 }
46
47 /*
48  * Build an i3String from an UTF-8 encoded string in Pango markup.
49  *
50  */
51 i3String *i3string_from_markup(const char *from_markup) {
52     i3String *str = i3string_from_utf8(from_markup);
53
54     /* Set the markup flag */
55     str->is_markup = true;
56
57     return str;
58 }
59
60 /*
61  * Build an i3String from an UTF-8 encoded string with fixed length.
62  * To be used when no proper NUL-terminaison is available.
63  * Returns the newly-allocated i3String.
64  *
65  */
66 i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes) {
67     i3String *str = scalloc(1, sizeof(i3String));
68
69     /* Copy the actual text to our i3String */
70     str->utf8 = scalloc(num_bytes + 1, 1);
71     strncpy(str->utf8, from_utf8, num_bytes);
72     str->utf8[num_bytes] = '\0';
73
74     /* Store the length */
75     str->num_bytes = num_bytes;
76
77     return str;
78 }
79
80 /*
81  * Build an i3String from an UTF-8 encoded string in Pango markup with fixed
82  * length.
83  *
84  */
85 i3String *i3string_from_markup_with_length(const char *from_markup, size_t num_bytes) {
86     i3String *str = i3string_from_utf8_with_length(from_markup, num_bytes);
87
88     /* set the markup flag */
89     str->is_markup = true;
90
91     return str;
92 }
93
94 /*
95  * Build an i3String from an UCS-2 encoded string.
96  * Returns the newly-allocated i3String.
97  *
98  */
99 i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) {
100     i3String *str = scalloc(1, sizeof(i3String));
101
102     /* Copy the actual text to our i3String */
103     str->ucs2 = scalloc(num_glyphs, sizeof(xcb_char2b_t));
104     memcpy(str->ucs2, from_ucs2, num_glyphs * sizeof(xcb_char2b_t));
105
106     /* Store the length */
107     str->num_glyphs = num_glyphs;
108
109     str->utf8 = NULL;
110     str->num_bytes = 0;
111
112     return str;
113 }
114
115 /**
116  * Copies the given i3string.
117  * Note that this will not free the source string.
118  */
119 i3String *i3string_copy(i3String *str) {
120     i3String *copy = i3string_from_utf8(i3string_as_utf8(str));
121     copy->is_markup = str->is_markup;
122     return copy;
123 }
124
125 /*
126  * Free an i3String.
127  *
128  */
129 void i3string_free(i3String *str) {
130     if (str == NULL)
131         return;
132     free(str->utf8);
133     free(str->ucs2);
134     free(str);
135 }
136
137 static void i3string_ensure_utf8(i3String *str) {
138     if (str->utf8 != NULL)
139         return;
140     if ((str->utf8 = convert_ucs2_to_utf8(str->ucs2, str->num_glyphs)) != NULL)
141         str->num_bytes = strlen(str->utf8);
142 }
143
144 static void i3string_ensure_ucs2(i3String *str) {
145     if (str->ucs2 != NULL)
146         return;
147     str->ucs2 = convert_utf8_to_ucs2(str->utf8, &str->num_glyphs);
148 }
149
150 /*
151  * Returns the UTF-8 encoded version of the i3String.
152  *
153  */
154 const char *i3string_as_utf8(i3String *str) {
155     i3string_ensure_utf8(str);
156     return str->utf8;
157 }
158
159 /*
160  * Returns the UCS-2 encoded version of the i3String.
161  *
162  */
163 const xcb_char2b_t *i3string_as_ucs2(i3String *str) {
164     i3string_ensure_ucs2(str);
165     return str->ucs2;
166 }
167
168 /*
169  * Returns the number of bytes (UTF-8 encoded) in an i3String.
170  *
171  */
172 size_t i3string_get_num_bytes(i3String *str) {
173     i3string_ensure_utf8(str);
174     return str->num_bytes;
175 }
176
177 /*
178  * Whether the given i3String is in Pango markup.
179  */
180 bool i3string_is_markup(i3String *str) {
181     return str->is_markup;
182 }
183
184 /*
185  * Set whether the i3String should use Pango markup.
186  */
187 void i3string_set_markup(i3String *str, bool is_markup) {
188     str->is_markup = is_markup;
189 }
190
191 /*
192  * Escape pango markup characters in the given string.
193  */
194 i3String *i3string_escape_markup(i3String *str) {
195 #if PANGO_SUPPORT
196     const char *text = i3string_as_utf8(str);
197     char *escaped = g_markup_escape_text(text, -1);
198     i3String *result = i3string_from_utf8(escaped);
199     free(escaped);
200     return result;
201 #else
202     return str;
203 #endif
204 }
205
206 /*
207  * Returns the number of glyphs in an i3String.
208  *
209  */
210 size_t i3string_get_num_glyphs(i3String *str) {
211     i3string_ensure_ucs2(str);
212     return str->num_glyphs;
213 }