54
56
return BLI_strdupn(str, strlen(str));
57
char *BLI_strdupcat(const char *str1, const char *str2)
59
char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
62
len= strlen(str1)+strlen(str2);
63
n= MEM_mallocN(len+1, "strdupcat");
64
len = strlen(str1) + strlen(str2);
65
n = MEM_mallocN(len + 1, "strdupcat");
70
char *BLI_strncpy(char *dst, const char *src, const size_t maxncpy)
72
char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
72
size_t srclen= strlen(src);
73
size_t cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
74
size_t srclen = strlen(src);
75
size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen;
76
BLI_assert(maxncpy != 0);
75
78
memcpy(dst, src, cpylen);
81
size_t BLI_snprintf(char *buffer, size_t count, const char *format, ...)
84
size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg)
86
va_start(arg, format);
88
BLI_assert(buffer != NULL);
89
BLI_assert(count > 0);
90
BLI_assert(format != NULL);
87
92
n = vsnprintf(buffer, count, format, arg);
89
94
if (n != -1 && n < count) {
93
buffer[count-1] = '\0';
98
buffer[count - 1] = '\0';
104
size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...)
109
va_start(arg, format);
110
n = BLI_vsnprintf(buffer, count, format, arg);
100
char *BLI_sprintfN(const char *format, ...)
116
char *BLI_sprintfN(const char *__restrict format, ...)
122
BLI_assert(format != NULL);
106
124
va_start(arg, format);
108
ds= BLI_dynstr_new();
126
ds = BLI_dynstr_new();
109
127
BLI_dynstr_vappendf(ds, format, arg);
110
n= BLI_dynstr_get_cstring(ds);
128
n = BLI_dynstr_get_cstring(ds);
111
129
BLI_dynstr_free(ds);
121
139
* TODO: support more fancy string escaping. current code is primitive
122
140
* this basically is an ascii version of PyUnicode_EncodeUnicodeEscape()
123
141
* which is a useful reference. */
124
size_t BLI_strescape(char *dst, const char *src, const size_t maxlen)
142
size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
127
while (len < maxlen) {
146
BLI_assert(maxncpy != 0);
148
while (len < maxncpy) {
130
151
goto escape_finish;
134
/* less common but should also be support */
155
/* less common but should also be support */
138
if (len + 1 < maxlen) {
159
if (len + 1 < maxncpy) {
163
184
/* Makes a copy of the text within the "" that appear after some text 'blahblah'
164
185
* i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
166
* - str: is the entire string to chop
187
* - str: is the entire string to chop
167
188
* - prefix: is the part of the string to leave out
169
190
* Assume that the strings returned must be freed afterwards, and that the inputs will contain
170
191
* data we want...
193
* TODO, return the offset and a length so as to avoid doing an allocation.
172
char *BLI_getQuotedStr (const char *str, const char *prefix)
195
char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix)
174
197
size_t prefixLen = strlen(prefix);
175
198
char *startMatch, *endMatch;
177
200
/* get the starting point (i.e. where prefix starts, and add prefixLen+1 to it to get be after the first " */
178
startMatch= strstr(str, prefix) + prefixLen + 1;
180
/* get the end point (i.e. where the next occurance of " is after the starting point) */
181
endMatch= strchr(startMatch, '"'); // " NOTE: this comment here is just so that my text editor still shows the functions ok...
183
/* return the slice indicated */
184
return BLI_strdupn(startMatch, (size_t)(endMatch-startMatch));
201
startMatch = strstr(str, prefix) + prefixLen + 1;
203
/* get the end point (i.e. where the next occurance of " is after the starting point) */
204
endMatch = strchr(startMatch, '"'); /* " NOTE: this comment here is just so that my text editor still shows the functions ok... */
207
/* return the slice indicated */
208
return BLI_strdupn(startMatch, (size_t)(endMatch - startMatch));
210
return BLI_strdupn("", 0);
187
213
/* Replaces all occurrences of oldText with newText in str, returning a new string that doesn't
188
214
* contain the 'replaced' occurrences.
190
// A rather wasteful string-replacement utility, though this shall do for now...
191
// Feel free to replace this with an even safe + nicer alternative
192
char *BLI_replacestr(char *str, const char *oldText, const char *newText)
217
/* A rather wasteful string-replacement utility, though this shall do for now...
218
* Feel free to replace this with an even safe + nicer alternative */
219
char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const char *__restrict newText)
195
size_t lenOld= strlen(oldText);
222
size_t lenOld = strlen(oldText);
198
225
/* sanity checks */
199
if ((str == NULL) || (str[0]==0))
226
if ((str == NULL) || (str[0] == 0))
201
else if ((oldText == NULL) || (newText == NULL) || (oldText[0]==0))
228
else if ((oldText == NULL) || (newText == NULL) || (oldText[0] == 0))
202
229
return BLI_strdup(str);
204
231
/* while we can still find a match for the old substring that we're searching for,
329
358
/* natural string compare, keeping numbers in order */
330
359
int BLI_natstrcmp(const char *s1, const char *s2)
361
register int d1 = 0, d2 = 0;
362
register char c1, c2;
334
364
/* if both chars are numeric, to a strtol().
335
365
* then increase string deltas as long they are
336
366
* numeric, else do a tolower and char compare */
339
char c1 = tolower(s1[d1]);
340
char c2 = tolower(s2[d2]);
369
c1 = tolower(s1[d1]);
370
c2 = tolower(s2[d2]);
342
if ( isdigit(c1) && isdigit(c2) ) {
372
if (isdigit(c1) && isdigit(c2) ) {
345
val1= (int)strtol(s1+d1, (char **)NULL, 10);
346
val2= (int)strtol(s2+d2, (char **)NULL, 10);
375
val1 = (int)strtol(s1 + d1, (char **)NULL, 10);
376
val2 = (int)strtol(s2 + d2, (char **)NULL, 10);
351
else if (val1>val2) {
381
else if (val1 > val2) {
355
while ( isdigit(s1[d1]) )
385
while (isdigit(s1[d1]) )
358
while ( isdigit(s2[d2]) )
388
while (isdigit(s2[d2]) )
361
391
c1 = tolower(s1[d1]);
362
392
c2 = tolower(s2[d2]);
365
/* first check for '.' so "foo.bar" comes before "foo 1.bar" */
366
if (c1=='.' && c2!='.')
368
if (c1!='.' && c2=='.')
395
/* first check for '.' so "foo.bar" comes before "foo 1.bar" */
396
if (c1 == '.' && c2 != '.')
398
if (c1 != '.' && c2 == '.')
385
415
void BLI_timestr(double _time, char *str)
387
417
/* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
388
int hr= ( (int) _time) / (60*60);
389
int min= (((int) _time) / 60 ) % 60;
390
int sec= ( (int) (_time)) % 60;
391
int hun= ( (int) (_time * 100.0)) % 100;
418
int hr = ( (int) _time) / (60 * 60);
419
int min = (((int) _time) / 60 ) % 60;
420
int sec = ( (int) (_time)) % 60;
421
int hun = ( (int) (_time * 100.0)) % 100;
394
sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
424
sprintf(str, "%.2d:%.2d:%.2d.%.2d", hr, min, sec, hun);
397
sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);
427
sprintf(str, "%.2d:%.2d.%.2d", min, sec, hun);
403
433
/* determine the length of a fixed-size string */
404
size_t BLI_strnlen(const char *str, size_t maxlen)
434
size_t BLI_strnlen(const char *str, const size_t maxlen)
406
436
const char *end = memchr(str, '\0', maxlen);
407
437
return end ? (size_t) (end - str) : maxlen;
410
void BLI_ascii_strtolower(char *str, int len)
440
void BLI_ascii_strtolower(char *str, const size_t len)
414
for (i=0; i<len; i++)
444
for (i = 0; i < len; i++)
415
445
if (str[i] >= 'A' && str[i] <= 'Z')
416
446
str[i] += 'a' - 'A';
419
void BLI_ascii_strtoupper(char *str, int len)
449
void BLI_ascii_strtoupper(char *str, const size_t len)
423
for (i=0; i<len; i++)
453
for (i = 0; i < len; i++)
424
454
if (str[i] >= 'a' && str[i] <= 'z')
425
455
str[i] -= 'a' - 'A';