Blender V5.3
text_format.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_listbase.hh"
14#include "BLI_path_utils.hh"
15#include "BLI_string.hh"
16#include "BLI_string_utf8.hh"
17#include "BLI_string_utils.hh"
18
19#include "DNA_space_types.h"
20#include "DNA_text_types.h"
21
22#include "ED_text.hh"
23
24#include "text_format.hh"
25
26namespace blender {
27
28/****************** flatten string **********************/
29
30static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
31{
32 int i;
33
34 if (fs->pos + len > fs->len) {
35 char *nbuf;
36 int *naccum;
37 fs->len *= 2;
38
39 nbuf = MEM_new_array_zeroed<char>(fs->len, "fs->buf");
40 memcpy(nbuf, fs->buf, sizeof(*fs->buf) * fs->pos);
41
42 naccum = MEM_new_array_zeroed<int>(fs->len, "fs->accum");
43 memcpy(naccum, fs->accum, sizeof(*fs->accum) * fs->pos);
44
45 if (fs->buf != fs->fixedbuf) {
46 MEM_delete(fs->buf);
47 MEM_delete(fs->accum);
48 }
49
50 fs->buf = nbuf;
51 fs->accum = naccum;
52 }
53
54 for (i = 0; i < len; i++) {
55 fs->buf[fs->pos + i] = c[i];
56 fs->accum[fs->pos + i] = accum;
57 }
58
59 fs->pos += len;
60}
61
62int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
63{
64 int r, i, total = 0;
65
66 memset(fs, 0, sizeof(FlattenString));
67 fs->buf = fs->fixedbuf;
68 fs->accum = fs->fixedaccum;
69 fs->len = sizeof(fs->fixedbuf);
70
71 for (r = 0, i = 0; *in; r++) {
72 if (*in == '\t') {
73 i = st->tabnumber - (total % st->tabnumber);
74 total += i;
75
76 while (i--) {
77 flatten_string_append(fs, " ", r, 1);
78 }
79
80 in++;
81 }
82 else {
85 in += len;
86 total++;
87 }
88 }
89
90 flatten_string_append(fs, "\0", r, 1);
91
92 return total;
93}
94
96{
97 if (fs->buf != fs->fixedbuf) {
98 MEM_delete(fs->buf);
99 }
100 if (fs->accum != fs->fixedaccum) {
101 MEM_delete(fs->accum);
102 }
103}
104
106{
107 const int len = (fs->pos - int(str - fs->buf)) - 1;
108 BLI_assert(strlen(str) == len);
109 return len;
110}
111
113{
114 if (line->format) {
115 if (strlen(line->format) < len) {
116 MEM_delete(line->format);
117 line->format = MEM_new_array_uninitialized<char>(len + 2, "SyntaxFormat");
118 if (!line->format) {
119 return 0;
120 }
121 }
122 }
123 else {
124 line->format = MEM_new_array_uninitialized<char>(len + 2, "SyntaxFormat");
125 if (!line->format) {
126 return 0;
127 }
128 }
129
130 return 1;
131}
132
133void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
134{
135 const char *str = *str_p;
136 char *fmt = *fmt_p;
137 int i = 0;
138
139 while (i < len) {
140 const int size = BLI_str_utf8_size_safe(str);
141 *fmt++ = type;
142
143 str += size;
144 i += 1;
145 }
146
147 str--;
148 fmt--;
149
150 BLI_assert(*str != '\0');
151
152 *str_p = str;
153 *fmt_p = fmt;
154}
155void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
156{
157 const char *str = *str_p;
158 char *fmt = *fmt_p;
159
160 memset(fmt, type, len);
161
162 str += len - 1;
163 fmt += len - 1;
164
165 BLI_assert(*str != '\0');
166
167 *str_p = str;
168 *fmt_p = fmt;
169}
170
171/* *** Registration *** */
172static ListBaseT<TextFormatType> tft_lb = {nullptr, nullptr};
177
179{
180 if (text) {
181 const char *text_ext = strchr(text->id.name + 2, '.');
182 if (text_ext) {
183 text_ext++; /* Skip the `.`. */
184 /* Check all text formats in the static list. */
185 for (TextFormatType &tft : tft_lb) {
186 /* All formats should have an ext, but just in case. */
187 const char **ext;
188 for (ext = tft.ext; *ext; ext++) {
189 /* If extension matches text name, return the matching tft. */
190 if (BLI_strcasecmp(text_ext, *ext) == 0) {
191 return &tft;
192 }
193 }
194 }
195 }
196
197 /* If we make it here we never found an extension that worked - return
198 * the "default" text format. */
199 return tft_lb.first();
200 }
201
202 /* Return the "default" text format. */
203 return tft_lb.first();
204}
205
207{
208 const TextFormatType *format = ED_text_format_get(text);
209 return format->comment_line;
210}
211
213{
214 if (text == nullptr) {
215 return false;
216 }
217
218 const char *text_ext = BLI_path_extension(text->id.name + 2);
219 if (text_ext == nullptr) {
220 /* Extensionless data-blocks are considered highlightable as Python. */
221 return true;
222 }
223 text_ext++; /* Skip the `.`. */
224 if (BLI_string_is_decimal(text_ext)) {
225 /* `Text.001` is treated as extensionless, and thus highlightable. */
226 return true;
227 }
228
229 /* Check all text formats in the static list. */
230 for (TextFormatType &tft : tft_lb) {
231 /* All formats should have an ext, but just in case. */
232 const char **ext;
233 for (ext = tft.ext; *ext; ext++) {
234 /* If extension matches text name, return the matching tft. */
235 if (BLI_strcasecmp(text_ext, *ext) == 0) {
236 return true;
237 }
238 }
239 }
240
241 /* The filename has a non-numerical extension that we could not highlight. */
242 return false;
243}
244
245int text_format_string_literal_find(const Span<const char *> string_literals, const char *text)
246{
247 auto cmp_fn = [](const char *text, const char *string_literal) {
248 return strcmp(text, string_literal) < 0;
249 };
250 const char *const *string_literal_p = std::upper_bound(
251 std::begin(string_literals), std::end(string_literals), text, cmp_fn);
252
253 if (string_literal_p != std::begin(string_literals)) {
254 const char *string = *(string_literal_p - 1);
255 const size_t string_len = strlen(string);
256 if (strncmp(string, text, string_len) == 0) {
257 return string_len;
258 }
259 }
260
261 return 0;
262}
263
264#ifndef NDEBUG
266{
267 return std::is_sorted(string_literals.begin(),
268 string_literals.end(),
269 [](const char *a, const char *b) { return strcmp(a, b) < 0; });
270}
271#endif
272
273} // namespace blender
Read Blender memory allocation and freeing.
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
#define str(s)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
bool BLI_string_is_decimal(const char *string) ATTR_NONNULL(1)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
int flatten_string(const SpaceText *st, FlattenString *fs, const char *in)
int text_check_format_len(TextLine *line, uint len)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static ListBaseT< TextFormatType > tft_lb
bool text_format_string_literals_check_sorted_array(const Span< const char * > string_literals)
TextFormatType * ED_text_format_get(Text *text)
int text_format_string_literal_find(const Span< const char * > string_literals, const char *text)
void flatten_string_free(FlattenString *fs)
int flatten_string_strlen(FlattenString *fs, const char *str)
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char * ED_text_format_comment_line_prefix(Text *text)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void ED_text_format_register(TextFormatType *tft)
static void flatten_string_append(FlattenString *fs, const char *c, int accum, int len)
bool ED_text_is_syntax_highlight_supported(Text *text)
void text_format_fill_ascii(const char **str_p, char **fmt_p, const char type, const int len)
void text_format_fill(const char **str_p, char **fmt_p, const char type, const int len)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1