forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
645 lines (534 loc) · 12.9 KB
/
string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#include <stdarg.h>
json_t *w_string_to_json(w_string_t *str) {
return json_stringn_nocheck(str->buf, str->len);
}
w_string_t *w_string_slice(w_string_t *str, uint32_t start, uint32_t len)
{
w_string_t *slice;
if (start == 0 && len == str->len) {
w_string_addref(str);
return str;
}
if (start > str->len || start + len > str->len) {
errno = EINVAL;
w_log(W_LOG_FATAL,
"illegal string slice start=%" PRIu32 " len=%" PRIu32
" but str->len=%" PRIu32 "\nstring={%.*s}\n",
start, len, str->len, str->len, str->buf);
return NULL;
}
slice = calloc(1, sizeof(*str));
slice->refcnt = 1;
slice->len = len;
slice->buf = str->buf + start;
slice->slice = str;
slice->hval = w_hash_bytes(slice->buf, slice->len, 0);
w_string_addref(str);
return slice;
}
uint32_t u32_strlen(const char *str) {
size_t slen = strlen(str);
if (slen > UINT32_MAX) {
w_log(W_LOG_FATAL, "string of length %" PRIsize_t " is too damned long\n",
slen);
}
return (uint32_t)slen;
}
w_string_t *w_string_new(const char *str)
{
w_string_t *s;
uint32_t len = u32_strlen(str);
uint32_t hval;
char *buf;
hval = w_hash_bytes(str, len, 0);
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->hval = hval;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
memcpy(buf, str, len);
buf[len] = 0;
s->buf = buf;
return s;
}
#ifdef _WIN32
w_string_t *w_string_new_wchar(WCHAR *str, int len) {
char buf[WATCHMAN_NAME_MAX];
int res;
if (len == 0) {
return w_string_new("");
}
res = WideCharToMultiByte(CP_UTF8, 0, str, len, buf, sizeof(buf), NULL, NULL);
if (res == 0) {
char msgbuf[1024];
DWORD err = GetLastError();
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf, sizeof(msgbuf)-1, NULL);
w_log(W_LOG_ERR, "WideCharToMultiByte failed: 0x%x %s\n", err, msgbuf);
return NULL;
}
buf[res] = 0;
return w_string_new(buf);
}
#endif
w_string_t *w_string_make_printf(const char *format, ...)
{
w_string_t *s;
int len;
char *buf;
va_list args;
va_start(args, format);
// Get the length needed
len = vsnprintf(NULL, 0, format, args);
va_end(args);
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
va_start(args, format);
vsnprintf(buf, len + 1, format, args);
va_end(args);
s->buf = buf;
s->hval = w_hash_bytes(buf, len, 0);
return s;
}
/* return a reference to a lowercased version of a string */
w_string_t *w_string_dup_lower(w_string_t *str)
{
bool is_lower = true;
char *buf;
uint32_t i;
w_string_t *s;
for (i = 0; i < str->len; i++) {
if (tolower((uint8_t)str->buf[i]) != str->buf[i]) {
is_lower = false;
break;
}
}
if (is_lower) {
w_string_addref(str);
return str;
}
/* need to make a lowercase version */
s = malloc(sizeof(*s) + str->len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = str->len;
s->slice = NULL;
buf = (char*)(s + 1);
for (i = 0; i < str->len; i++) {
buf[i] = (char)tolower((uint8_t)str->buf[i]);
}
buf[str->len] = 0;
s->buf = buf;
s->hval = w_hash_bytes(buf, str->len, 0);
return s;
}
/* make a lowercased copy of string */
w_string_t *w_string_new_lower(const char *str)
{
w_string_t *s;
uint32_t len = u32_strlen(str);
char *buf;
uint32_t i;
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
// TODO: optionally use ICU
for (i = 0; i < len; i++) {
buf[i] = (char)tolower((uint8_t)str[i]);
}
buf[len] = 0;
s->buf = buf;
s->hval = w_hash_bytes(buf, len, 0);
return s;
}
void w_string_addref(w_string_t *str)
{
w_refcnt_add(&str->refcnt);
}
void w_string_delref(w_string_t *str)
{
if (!w_refcnt_del(&str->refcnt)) return;
if (str->slice) w_string_delref(str->slice);
free(str);
}
int w_string_compare(const w_string_t *a, const w_string_t *b)
{
if (a == b) return 0;
return strcmp(a->buf, b->buf);
}
bool w_string_equal_cstring(const w_string_t *a, const char *b)
{
uint32_t blen = u32_strlen(b);
if (a->len != blen) return false;
return memcmp(a->buf, b, a->len) == 0 ? true : false;
}
bool w_string_equal(const w_string_t *a, const w_string_t *b)
{
if (a == b) return true;
if (a->hval != b->hval) return false;
if (a->len != b->len) return false;
return memcmp(a->buf, b->buf, a->len) == 0 ? true : false;
}
bool w_string_equal_caseless(const w_string_t *a, const w_string_t *b)
{
uint32_t i;
if (a == b) return true;
if (a->len != b->len) return false;
for (i = 0; i < a->len; i++) {
if (tolower((uint8_t)a->buf[i]) != tolower((uint8_t)b->buf[i])) {
return false;
}
}
return true;
}
w_string_t *w_string_dirname(w_string_t *str)
{
int end;
/* can't use libc strXXX functions because we may be operating
* on a slice */
for (end = str->len - 1; end >= 0; end--) {
if (str->buf[end] == WATCHMAN_DIR_SEP) {
/* found the end of the parent dir */
return w_string_slice(str, 0, end);
}
}
return NULL;
}
bool w_string_suffix_match(w_string_t *str, w_string_t *suffix)
{
unsigned int base, i;
if (str->len < suffix->len + 1) {
return false;
}
base = str->len - suffix->len;
if (str->buf[base - 1] != '.') {
return false;
}
for (i = 0; i < suffix->len; i++) {
if (tolower((uint8_t)str->buf[base + i]) != suffix->buf[i]) {
return false;
}
}
return true;
}
// Return the normalized (lowercase) filename suffix
w_string_t *w_string_suffix(w_string_t *str)
{
int end;
char name_buf[128];
char *buf;
/* can't use libc strXXX functions because we may be operating
* on a slice */
for (end = str->len - 1; end >= 0; end--) {
if (str->buf[end] == '.') {
if (str->len - (end + 1) >= sizeof(name_buf) - 1) {
// Too long
return NULL;
}
buf = name_buf;
end++;
while ((unsigned)end < str->len) {
*buf = (char)tolower((uint8_t)str->buf[end]);
end++;
buf++;
}
*buf = '\0';
return w_string_new(name_buf);
}
if (str->buf[end] == WATCHMAN_DIR_SEP) {
// No suffix
return NULL;
}
}
// Has no suffix
return NULL;
}
bool w_string_startswith(w_string_t *str, w_string_t *prefix)
{
if (prefix->len > str->len) {
return false;
}
return memcmp(str->buf, prefix->buf, prefix->len) == 0;
}
bool w_string_startswith_caseless(w_string_t *str, w_string_t *prefix)
{
size_t i;
if (prefix->len > str->len) {
return false;
}
for (i = 0; i < prefix->len; i++) {
if (tolower((uint8_t)str->buf[i]) != tolower((uint8_t)prefix->buf[i])) {
return false;
}
}
return true;
}
w_string_t *w_string_canon_path(w_string_t *str)
{
int end;
int trim = 0;
for (end = str->len - 1;
end >= 0 && str->buf[end] == WATCHMAN_DIR_SEP; end--) {
trim++;
}
if (trim) {
return w_string_slice(str, 0, str->len - trim);
}
w_string_addref(str);
return str;
}
#ifdef _WIN32
#define WRONG_SEP '/'
#else
#define WRONG_SEP '\\'
#endif
// Normalize directory separators to match the platform.
// Also trims any trailing directory separators
w_string_t *w_string_normalize_separators(w_string_t *str, char target_sep) {
w_string_t *s;
char *buf;
uint32_t i, len;
len = str->len;
if (len == 0) {
w_string_addref(str);
return str;
}
// This doesn't do any special UNC or path len escape prefix handling
// on windows. We don't currently use it in a way that would require it.
// Trim any trailing dir seps
while (len > 0) {
if (str->buf[len-1] == '/' || str->buf[len-1] == '\\') {
--len;
} else {
break;
}
}
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
for (i = 0; i < len; i++) {
if (str->buf[i] == '/' || str->buf[i] == '\\') {
buf[i] = target_sep;
} else {
buf[i] = str->buf[i];
}
}
buf[len] = 0;
s->buf = buf;
s->hval = w_hash_bytes(buf, len, 0);
return s;
}
void w_string_in_place_normalize_separators(w_string_t **str, char target_sep) {
w_string_t *norm = w_string_normalize_separators(*str, target_sep);
w_string_delref(*str);
*str = norm;
}
// Compute the basename of path, return that as a string
w_string_t *w_string_new_basename(const char *path) {
const char *base;
base = path + strlen(path);
while (base > path && base[-1] != WATCHMAN_DIR_SEP) {
base--;
}
return w_string_new(base);
}
w_string_t *w_string_basename(w_string_t *str)
{
int end;
/* can't use libc strXXX functions because we may be operating
* on a slice */
for (end = str->len - 1; end >= 0; end--) {
if (str->buf[end] == WATCHMAN_DIR_SEP) {
/* found the end of the parent dir */
return w_string_slice(str, end + 1, str->len - (end + 1));
}
}
w_string_addref(str);
return str;
}
w_string_t *w_string_path_cat(w_string_t *parent, w_string_t *rhs)
{
w_string_t *s;
int len;
char *buf;
if (rhs->len == 0) {
w_string_addref(parent);
return parent;
}
len = parent->len + rhs->len + 1;
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
memcpy(buf, parent->buf, parent->len);
buf[parent->len] = WATCHMAN_DIR_SEP;
memcpy(buf + parent->len + 1, rhs->buf, rhs->len);
buf[parent->len + 1 + rhs->len] = '\0';
s->buf = buf;
s->hval = w_hash_bytes(buf, len, 0);
return s;
}
w_string_t *w_string_path_cat_cstr(w_string_t *parent, const char *rhs)
{
w_string_t *s;
int len;
char *buf;
uint32_t rhs_len = u32_strlen(rhs);
if (rhs_len == 0) {
w_string_addref(parent);
return parent;
}
len = parent->len + rhs_len + 1;
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->len = len;
s->slice = NULL;
buf = (char*)(s + 1);
memcpy(buf, parent->buf, parent->len);
buf[parent->len] = WATCHMAN_DIR_SEP;
memcpy(buf + parent->len + 1, rhs, rhs_len);
buf[parent->len + 1 + rhs_len] = '\0';
s->buf = buf;
s->hval = w_hash_bytes(buf, len, 0);
return s;
}
char *w_string_dup_buf(const w_string_t *str)
{
char *buf;
buf = malloc(str->len + 1);
if (!buf) {
return NULL;
}
memcpy(buf, str->buf, str->len);
buf[str->len] = 0;
return buf;
}
// Given a json array, concat the elements using a delimiter
w_string_t *w_string_implode(json_t *arr, const char *delim)
{
uint32_t delim_len = u32_strlen(delim);
uint32_t len = 0;
uint32_t i;
w_string_t *s;
char *buf;
if (json_array_size(arr) == 0) {
return w_string_new("");
}
if (json_array_size(arr) == 1) {
return w_string_new(json_string_value(json_array_get(arr, 0)));
}
len = ((uint32_t)json_array_size(arr) - 1) * delim_len;
for (i = 0; i < json_array_size(arr); i++) {
const char *str;
str = json_string_value(json_array_get(arr, i));
len += u32_strlen(str);
}
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->slice = NULL;
buf = (char*)(s + 1);
s->buf = buf;
for (i = 0; i < json_array_size(arr); i++) {
const char *str;
uint32_t l;
str = json_string_value(json_array_get(arr, i));
l = u32_strlen(str);
memcpy(buf, str, l);
// Final string doesn't want delimiter after it
if (i == json_array_size(arr) - 1) {
buf += l;
break;
}
memcpy(buf + l, delim, delim_len);
buf += l + delim_len;
}
*buf = '\0';
s->len = (uint32_t)(buf - s->buf);
s->hval = w_hash_bytes(s->buf, s->len, 0);
return s;
}
// Given a string, return a shell-escaped copy
w_string_t *w_string_shell_escape(const w_string_t *str)
{
// Worst case expansion for a char is 4x, plus quoting either end
uint32_t len = 2 + (str->len * 4);
w_string_t *s;
char *buf;
const char *src, *end;
s = malloc(sizeof(*s) + len + 1);
if (!s) {
perror("no memory available");
abort();
}
s->refcnt = 1;
s->slice = NULL;
buf = (char*)(s + 1);
s->buf = buf;
src = str->buf;
end = src + str->len;
*buf = '\'';
buf++;
while (src < end) {
if (*src == '\'') {
memcpy(buf, "'\\''", 4);
buf += 4;
} else {
*buf = *src;
buf++;
}
src++;
}
*buf = '\'';
buf++;
*buf = 0;
s->len = (uint32_t)(buf - s->buf);
s->hval = w_hash_bytes(s->buf, s->len, 0);
return s;
}
/* vim:ts=2:sw=2:et:
*/