ui/common/utils.c

changeset 985
93f07ccfd997
parent 930
4ce8df2311f0
equal deleted inserted replaced
984:2cf5e6d55013 985:93f07ccfd997
27 */ 27 */
28 28
29 #include "utils.h" 29 #include "utils.h"
30 #include "properties.h" 30 #include "properties.h"
31 31
32 #include <stdio.h>
33 #include <string.h>
34
32 #include <cx/string.h> 35 #include <cx/string.h>
36 #include <cx/buffer.h>
33 37
34 UiPathElm* ui_default_pathelm_func(const char* full_path, size_t len, size_t* ret_nelm, void* data) { 38 UiPathElm* ui_default_pathelm_func(const char* full_path, size_t len, size_t* ret_nelm, void* data) {
35 cxstring *pathelms; 39 cxstring *pathelms;
36 size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len), CX_STR("/"), 4096, &pathelms); 40 size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len), CX_STR("/"), 4096, &pathelms);
37 41
81 *width = w; 85 *width = w;
82 *height = h; 86 *height = h;
83 } 87 }
84 } 88 }
85 } 89 }
90
91 // from UCX json.c
92 static cxmutstr escape_string(cxstring str, bool escape_slash) {
93 // note: this function produces the string without enclosing quotes
94 // the reason is that we don't want to allocate memory just for that
95 CxBuffer buf = {0};
96
97 bool all_printable = true;
98 for (size_t i = 0; i < str.length; i++) {
99 unsigned char c = str.ptr[i];
100 bool escape = c < 0x20 || c == '\\' || c == '"'
101 || (escape_slash && c == '/');
102
103 if (all_printable && escape) {
104 size_t capa = str.length + 32;
105 char *space = cxMallocDefault(capa);
106 if (space == NULL) return cx_mutstrn(NULL, 0);
107 cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND);
108 cxBufferWrite(str.ptr, 1, i, &buf);
109 all_printable = false;
110 }
111 if (escape) {
112 cxBufferPut(&buf, '\\');
113 if (c == '\"') {
114 cxBufferPut(&buf, '\"');
115 } else if (c == '\n') {
116 cxBufferPut(&buf, 'n');
117 } else if (c == '\t') {
118 cxBufferPut(&buf, 't');
119 } else if (c == '\r') {
120 cxBufferPut(&buf, 'r');
121 } else if (c == '\\') {
122 cxBufferPut(&buf, '\\');
123 } else if (c == '/') {
124 cxBufferPut(&buf, '/');
125 } else if (c == '\f') {
126 cxBufferPut(&buf, 'f');
127 } else if (c == '\b') {
128 cxBufferPut(&buf, 'b');
129 } else {
130 char code[6];
131 snprintf(code, sizeof(code), "u%04x", (unsigned int) c);
132 cxBufferPutString(&buf, code);
133 }
134 } else if (!all_printable) {
135 cxBufferPut(&buf, c);
136 }
137 }
138 cxmutstr ret;
139 if (all_printable) {
140 // don't copy the string when we don't need to escape anything
141 ret = cx_mutstrn((char*)str.ptr, str.length);
142 } else {
143 ret = cx_mutstrn(buf.space, buf.size);
144 }
145 cxBufferDestroy(&buf);
146 return ret;
147 }
148
149 cxmutstr ui_escape_string(cxstring str) {
150 return escape_string(str, FALSE);
151 }

mercurial