Repository navigation
Expand file tree
/
Copy pathcompression.diff
More file actions
1099 lines (1075 loc) · 36.7 KB
/
Copy pathcompression.diff
File metadata and controls
1099 lines (1075 loc) · 36.7 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- input/input.c
+++ output/output.c
@@ -45,48 +45,17 @@
static inline int sdsHdrSize(char type) {
switch(type&SDS_TYPE_MASK) {
case SDS_TYPE_5:
- return sizeof(struct sdshdr5);
- case SDS_TYPE_8:
- return sizeof(struct sdshdr8);
- case SDS_TYPE_16:
- return sizeof(struct sdshdr16);
- case SDS_TYPE_32:
- return sizeof(struct sdshdr32);
- case SDS_TYPE_64:
- return sizeof(struct sdshdr64);
- }
+ { … 10 line(s) … ⟦tj:8ed5c5befbd5069040e1506230bb6e6f⟧ }
return 0;
-}
static inline char sdsReqType(size_t string_size) {
if (string_size < 1<<5)
return SDS_TYPE_5;
- if (string_size < 1<<8)
- return SDS_TYPE_8;
- if (string_size < 1<<16)
- return SDS_TYPE_16;
-#if (LONG_MAX == LLONG_MAX)
- if (string_size < 1ll<<32)
- return SDS_TYPE_32;
- return SDS_TYPE_64;
-#else
- return SDS_TYPE_32;
+ { … 10 line(s) … ⟦tj:7f8ee8f24e05af74975824fa34390d8b⟧ }
#endif
-}
static inline size_t sdsTypeMaxSize(char type) {
- if (type == SDS_TYPE_5)
- return (1<<5) - 1;
- if (type == SDS_TYPE_8)
- return (1<<8) - 1;
- if (type == SDS_TYPE_16)
- return (1<<16) - 1;
-#if (LONG_MAX == LLONG_MAX)
- if (type == SDS_TYPE_32)
- return (1ll<<32) - 1;
-#endif
- return -1; /* this is equivalent to the max SDS_TYPE_64 or SDS_TYPE_32 */
-}
+ { … 12 line(s) … ⟦tj:156a3722aa780ce5f34027fb7c944b46⟧ }
/* Create a new sds string with the content specified by the 'init' pointer
* and 'initlen'.
@@ -104,67 +73,8 @@
sds _sdsnewlen(const void *init, size_t initlen, int trymalloc) {
void *sh;
sds s;
- char type = sdsReqType(initlen);
- /* Empty strings are usually created in order to append. Use type 8
- * since type 5 is not good at this. */
- if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
- int hdrlen = sdsHdrSize(type);
- unsigned char *fp; /* flags pointer. */
- size_t usable;
-
- assert(initlen + hdrlen + 1 > initlen); /* Catch size_t overflow */
- sh = trymalloc?
- s_trymalloc_usable(hdrlen+initlen+1, &usable) :
- s_malloc_usable(hdrlen+initlen+1, &usable);
- if (sh == NULL) return NULL;
- if (init==SDS_NOINIT)
- init = NULL;
- else if (!init)
- memset(sh, 0, hdrlen+initlen+1);
- s = (char*)sh+hdrlen;
- fp = ((unsigned char*)s)-1;
- usable = usable-hdrlen-1;
- if (usable > sdsTypeMaxSize(type))
- usable = sdsTypeMaxSize(type);
- switch(type) {
- case SDS_TYPE_5: {
- *fp = type | (initlen << SDS_TYPE_BITS);
- break;
- }
- case SDS_TYPE_8: {
- SDS_HDR_VAR(8,s);
- sh->len = initlen;
- sh->alloc = usable;
- *fp = type;
- break;
- }
- case SDS_TYPE_16: {
- SDS_HDR_VAR(16,s);
- sh->len = initlen;
- sh->alloc = usable;
- *fp = type;
- break;
- }
- case SDS_TYPE_32: {
- SDS_HDR_VAR(32,s);
- sh->len = initlen;
- sh->alloc = usable;
- *fp = type;
- break;
- }
- case SDS_TYPE_64: {
- SDS_HDR_VAR(64,s);
- sh->len = initlen;
- sh->alloc = usable;
- *fp = type;
- break;
- }
- }
- if (initlen && init)
- memcpy(s, init, initlen);
- s[initlen] = '\0';
+ { … 59 line(s) … ⟦tj:d67faf289304390e811a6debc02dd14e⟧ }
return s;
-}
sds sdsnewlen(const void *init, size_t initlen) {
return _sdsnewlen(init, initlen, 0);
@@ -323,60 +233,8 @@
sds sdsResize(sds s, size_t size, int would_regrow) {
void *sh, *newsh;
char type, oldtype = s[-1] & SDS_TYPE_MASK;
- int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
- size_t len = sdslen(s);
- sh = (char*)s-oldhdrlen;
-
- /* Return ASAP if the size is already good. */
- if (sdsalloc(s) == size) return s;
-
- /* Truncate len if needed. */
- if (size < len) len = size;
-
- /* Check what would be the minimum SDS header that is just good enough to
- * fit this string. */
- type = sdsReqType(size);
- if (would_regrow) {
- /* Don't use type 5, it is not good for strings that are expected to grow back. */
- if (type == SDS_TYPE_5) type = SDS_TYPE_8;
- }
- hdrlen = sdsHdrSize(type);
-
- /* If the type is the same, or can hold the size in it with low overhead
- * (larger than SDS_TYPE_8), we just realloc(), letting the allocator
- * to do the copy only if really needed. Otherwise if the change is
- * huge, we manually reallocate the string to use the different header
- * type. */
- int use_realloc = (oldtype==type || (type < oldtype && type > SDS_TYPE_8));
- size_t newlen = use_realloc ? oldhdrlen+size+1 : hdrlen+size+1;
-
- if (use_realloc) {
- int alloc_already_optimal = 0;
- #if defined(USE_JEMALLOC)
- /* je_nallocx returns the expected allocation size for the newlen.
- * We aim to avoid calling realloc() when using Jemalloc if there is no
- * change in the allocation size, as it incurs a cost even if the
- * allocation size stays the same. */
- alloc_already_optimal = (je_nallocx(newlen, 0) == zmalloc_size(sh));
- #endif
- if (!alloc_already_optimal) {
- newsh = s_realloc(sh, newlen);
- if (newsh == NULL) return NULL;
- s = (char*)newsh+oldhdrlen;
- }
- } else {
- newsh = s_malloc(newlen);
- if (newsh == NULL) return NULL;
- memcpy((char*)newsh+hdrlen, s, len);
- s_free(sh);
- s = (char*)newsh+hdrlen;
- s[-1] = type;
- }
- s[len] = 0;
- sdssetlen(s, len);
- sdssetalloc(s, size);
+ { … 52 line(s) … ⟦tj:f66b0db5be241720059588834cce8ece⟧ }
return s;
-}
/* Return the total size of the allocation of the specified sds string,
* including:
@@ -422,43 +280,8 @@
void sdsIncrLen(sds s, ssize_t incr) {
unsigned char flags = s[-1];
size_t len;
- switch(flags&SDS_TYPE_MASK) {
- case SDS_TYPE_5: {
- unsigned char *fp = ((unsigned char*)s)-1;
- unsigned char oldlen = SDS_TYPE_5_LEN(flags);
- assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
- *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS);
- len = oldlen+incr;
- break;
- }
- case SDS_TYPE_8: {
- SDS_HDR_VAR(8,s);
- assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
- len = (sh->len += incr);
- break;
- }
- case SDS_TYPE_16: {
- SDS_HDR_VAR(16,s);
- assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
- len = (sh->len += incr);
- break;
- }
- case SDS_TYPE_32: {
- SDS_HDR_VAR(32,s);
- assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
- len = (sh->len += incr);
- break;
- }
- case SDS_TYPE_64: {
- SDS_HDR_VAR(64,s);
- assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));
- len = (sh->len += incr);
- break;
- }
- default: len = 0; /* Just to avoid compilation warnings. */
- }
+ { … 35 line(s) … ⟦tj:c5d872ed183dff9fd122f02e05c5f9cb⟧ }
s[len] = '\0';
-}
/* Grow the sds to have the specified length. Bytes that were not part of
* the original length of the sds will be set to zero.
@@ -466,34 +289,16 @@
* if the specified length is smaller than the current length, no operation
* is performed. */
sds sdsgrowzero(sds s, size_t len) {
- size_t curlen = sdslen(s);
+ { … 11 line(s) … ⟦tj:800981fc9c89589eef6131e9b589035f⟧ }
- if (len <= curlen) return s;
- s = sdsMakeRoomFor(s,len-curlen);
- if (s == NULL) return NULL;
-
- /* Make sure added region doesn't contain garbage */
- memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
- sdssetlen(s, len);
- return s;
-}
-
/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
* end of the specified sds string 's'.
*
* After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscatlen(sds s, const void *t, size_t len) {
- size_t curlen = sdslen(s);
+ { … 9 line(s) … ⟦tj:2cbb332218cfbd905615e12ebb6de194⟧ }
- s = sdsMakeRoomFor(s,len);
- if (s == NULL) return NULL;
- memcpy(s+curlen, t, len);
- sdssetlen(s, curlen+len);
- s[curlen+len] = '\0';
- return s;
-}
-
/* Append the specified null terminated C string to the sds string 's'.
*
* After the call, the passed sds string is no longer valid and all the
@@ -513,15 +318,7 @@
/* Destructively modify the sds string 's' to hold the specified binary
* safe string pointed by 't' of length 'len' bytes. */
sds sdscpylen(sds s, const char *t, size_t len) {
- if (sdsalloc(s) < len) {
- s = sdsMakeRoomFor(s,len-sdslen(s));
- if (s == NULL) return NULL;
- }
- memcpy(s, t, len);
- s[len] = '\0';
- sdssetlen(s, len);
- return s;
-}
+ { … 9 line(s) … ⟦tj:520aac9e97cc5b777baa1e5dd9861dd5⟧ }
/* Like sdscpylen() but 't' must be a null-terminated string so that the length
* of the string is obtained with strlen(). */
@@ -544,43 +341,8 @@
sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
va_list cpy;
char staticbuf[1024], *buf = staticbuf, *t;
- size_t buflen = strlen(fmt)*2;
- int bufstrlen;
-
- /* We try to start using a static buffer for speed.
- * If not possible we revert to heap allocation. */
- if (buflen > sizeof(staticbuf)) {
- buf = s_malloc(buflen);
- if (buf == NULL) return NULL;
- } else {
- buflen = sizeof(staticbuf);
- }
-
- /* Alloc enough space for buffer and \0 after failing to
- * fit the string in the current buffer size. */
- while(1) {
- va_copy(cpy,ap);
- bufstrlen = vsnprintf(buf, buflen, fmt, cpy);
- va_end(cpy);
- if (bufstrlen < 0) {
- if (buf != staticbuf) s_free(buf);
- return NULL;
- }
- if (((size_t)bufstrlen) >= buflen) {
- if (buf != staticbuf) s_free(buf);
- buflen = ((size_t)bufstrlen) + 1;
- buf = s_malloc(buflen);
- if (buf == NULL) return NULL;
- continue;
- }
- break;
- }
-
- /* Finally concat the obtained string to the SDS string and return it. */
- t = sdscatlen(s, buf, bufstrlen);
- if (buf != staticbuf) s_free(buf);
+ { … 35 line(s) … ⟦tj:7b1ebb90928eb747ba636e33170de52a⟧ }
return t;
-}
/* Append to the sds string 's' a string obtained using printf-alike format
* specifier.
@@ -626,97 +388,8 @@
sds sdscatfmt(sds s, char const *fmt, ...) {
size_t initlen = sdslen(s);
const char *f = fmt;
- long i;
- va_list ap;
-
- /* To avoid continuous reallocations, let's start with a buffer that
- * can hold at least two times the format string itself. It's not the
- * best heuristic but seems to work in practice. */
- s = sdsMakeRoomFor(s, strlen(fmt)*2);
- va_start(ap,fmt);
- f = fmt; /* Next format specifier byte to process. */
- i = initlen; /* Position of the next byte to write to dest str. */
- while(*f) {
- char next, *str;
- size_t l;
- long long num;
- unsigned long long unum;
-
- /* Make sure there is always space for at least 1 char. */
- if (sdsavail(s)==0) {
- s = sdsMakeRoomFor(s,1);
- }
-
- switch(*f) {
- case '%':
- next = *(f+1);
- if (next == '\0') break;
- f++;
- switch(next) {
- case 's':
- case 'S':
- str = va_arg(ap,char*);
- l = (next == 's') ? strlen(str) : sdslen(str);
- if (sdsavail(s) < l) {
- s = sdsMakeRoomFor(s,l);
- }
- memcpy(s+i,str,l);
- sdsinclen(s,l);
- i += l;
- break;
- case 'i':
- case 'I':
- if (next == 'i')
- num = va_arg(ap,int);
- else
- num = va_arg(ap,long long);
- {
- char buf[LONG_STR_SIZE];
- l = ll2string(buf,sizeof(buf),num);
- if (sdsavail(s) < l) {
- s = sdsMakeRoomFor(s,l);
- }
- memcpy(s+i,buf,l);
- sdsinclen(s,l);
- i += l;
- }
- break;
- case 'u':
- case 'U':
- if (next == 'u')
- unum = va_arg(ap,unsigned int);
- else
- unum = va_arg(ap,unsigned long long);
- {
- char buf[LONG_STR_SIZE];
- l = ull2string(buf,sizeof(buf),unum);
- if (sdsavail(s) < l) {
- s = sdsMakeRoomFor(s,l);
- }
- memcpy(s+i,buf,l);
- sdsinclen(s,l);
- i += l;
- }
- break;
- default: /* Handle %% and generally %<unknown>. */
- s[i++] = next;
- sdsinclen(s,1);
- break;
- }
- break;
- default:
- s[i++] = *f;
- sdsinclen(s,1);
- break;
- }
- f++;
- }
- va_end(ap);
-
- /* Add null-term */
- s[i] = '\0';
+ { … 89 line(s) … ⟦tj:6d3a1ea1b330740bd463adc347b5d434⟧ }
return s;
-}
/* Remove the part of the string from left and from right composed just of
* contiguous characters found in 'cset', that is a null terminated C string.
@@ -735,33 +408,15 @@
sds sdstrim(sds s, const char *cset) {
char *end, *sp, *ep;
size_t len;
-
- sp = s;
- ep = end = s+sdslen(s)-1;
- while(sp <= end && strchr(cset, *sp)) sp++;
- while(ep > sp && strchr(cset, *ep)) ep--;
- len = (ep-sp)+1;
- if (s != sp) memmove(s, sp, len);
- s[len] = '\0';
- sdssetlen(s,len);
+ { … 9 line(s) … ⟦tj:6bf491f6b1e6bf2c88737738cad83791⟧ }
return s;
-}
/* Changes the input string to be a subset of the original.
* It does not release the free space in the string, so a call to
* sdsRemoveFreeSpace may be wise after. */
void sdssubstr(sds s, size_t start, size_t len) {
- /* Clamp out of range input */
- size_t oldlen = sdslen(s);
- if (start >= oldlen) start = len = 0;
- if (len > oldlen-start) len = oldlen-start;
+ { … 10 line(s) … ⟦tj:8a9f790f91364cf8e81c6db870eeaf3a⟧ }
- /* Move the data */
- if (len) memmove(s, s+start, len);
- s[len] = 0;
- sdssetlen(s,len);
-}
-
/* Turn the string into a smaller (or equal) string containing only the
* substring specified by the 'start' and 'end' indexes.
*
@@ -784,15 +439,7 @@
* sdsrange(s,1,-1); => "ello World"
*/
void sdsrange(sds s, ssize_t start, ssize_t end) {
- size_t newlen, len = sdslen(s);
- if (len == 0) return;
- if (start < 0)
- start = len + start;
- if (end < 0)
- end = len + end;
- newlen = (start > end) ? 0 : (end-start)+1;
- sdssubstr(s, start, newlen);
-}
+ { … 9 line(s) … ⟦tj:4ecc11f1d827d27293466b6a577aae24⟧ }
/* Apply tolower() to every character of the sds string 's'. */
void sdstolower(sds s) {
@@ -820,17 +467,8 @@
* additional characters, the longer string is considered to be greater than
* the smaller one. */
int sdscmp(const sds s1, const sds s2) {
- size_t l1, l2, minlen;
- int cmp;
+ { … 10 line(s) … ⟦tj:3f943ee2fd15e204dd7bd5d0a7d4fd93⟧ }
- l1 = sdslen(s1);
- l2 = sdslen(s2);
- minlen = (l1 < l2) ? l1 : l2;
- cmp = memcmp(s1,s2,minlen);
- if (cmp == 0) return l1>l2? 1: (l1<l2? -1: 0);
- return cmp;
-}
-
/* Split 's' with separator in 'sep'. An array
* of sds strings is returned. *count will be set
* by reference to the number of tokens returned.
@@ -850,51 +488,9 @@
sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count) {
int elements = 0, slots = 5;
long start = 0, j;
- sds *tokens;
-
- if (seplen < 1 || len <= 0) {
- *count = 0;
- return NULL;
+ { … 42 line(s) … ⟦tj:1145a599fe3d0247c30996cfa316847e⟧ }
}
- tokens = s_malloc(sizeof(sds)*slots);
- if (tokens == NULL) return NULL;
- for (j = 0; j < (len-(seplen-1)); j++) {
- /* make sure there is room for the next element and the final one */
- if (slots < elements+2) {
- sds *newtokens;
-
- slots *= 2;
- newtokens = s_realloc(tokens,sizeof(sds)*slots);
- if (newtokens == NULL) goto cleanup;
- tokens = newtokens;
- }
- /* search the separator */
- if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
- tokens[elements] = sdsnewlen(s+start,j-start);
- if (tokens[elements] == NULL) goto cleanup;
- elements++;
- start = j+seplen;
- j = j+seplen-1; /* skip the separator */
- }
- }
- /* Add the final element. We are sure there is room in the tokens array. */
- tokens[elements] = sdsnewlen(s+start,len-start);
- if (tokens[elements] == NULL) goto cleanup;
- elements++;
- *count = elements;
- return tokens;
-
-cleanup:
- {
- int i;
- for (i = 0; i < elements; i++) sdsfree(tokens[i]);
- s_free(tokens);
- *count = 0;
- return NULL;
- }
-}
-
/* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
void sdsfreesplitres(sds *tokens, int count) {
if (!tokens) return;
@@ -912,28 +508,8 @@
sds sdscatrepr(sds s, const char *p, size_t len) {
s = sdsMakeRoomFor(s, len + 2);
s = sdscatlen(s,"\"",1);
- while(len--) {
- switch(*p) {
- case '\\':
- case '"':
- s = sdscatprintf(s,"\\%c",*p);
- break;
- case '\n': s = sdscatlen(s,"\\n",2); break;
- case '\r': s = sdscatlen(s,"\\r",2); break;
- case '\t': s = sdscatlen(s,"\\t",2); break;
- case '\a': s = sdscatlen(s,"\\a",2); break;
- case '\b': s = sdscatlen(s,"\\b",2); break;
- default:
- if (isprint(*p))
- s = sdscatlen(s, p, 1);
- else
- s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
- break;
- }
- p++;
- }
+ { … 20 line(s) … ⟦tj:46da1f63dc6aa1270a0222010267abae⟧ }
return sdscatlen(s,"\"",1);
-}
/* Returns one if the string contains characters to be escaped
* by sdscatrepr(), zero otherwise.
@@ -943,18 +519,8 @@
* treated as needing an escape.
*/
int sdsneedsrepr(const sds s) {
- size_t len = sdslen(s);
- const char *p = s;
-
- while (len--) {
- if (*p == '\\' || *p == '"' || *p == '\n' || *p == '\r' ||
- *p == '\t' || *p == '\a' || *p == '\b' || !isprint(*p) || isspace(*p)) return 1;
- p++;
- }
+ { … 11 line(s) … ⟦tj:2683fd6fe73d9cc8c3ea5997b473acf6⟧ }
- return 0;
-}
-
/* Helper function for sdssplitargs() that returns non zero if 'c'
* is a valid hex digit. */
int is_hex_digit(char c) {
@@ -967,24 +533,8 @@
int hex_digit_to_int(char c) {
switch(c) {
case '0': return 0;
- case '1': return 1;
- case '2': return 2;
- case '3': return 3;
- case '4': return 4;
- case '5': return 5;
- case '6': return 6;
- case '7': return 7;
- case '8': return 8;
- case '9': return 9;
- case 'a': case 'A': return 10;
- case 'b': case 'B': return 11;
- case 'c': case 'C': return 12;
- case 'd': case 'D': return 13;
- case 'e': case 'E': return 14;
- case 'f': case 'F': return 15;
- default: return 0;
+ { … 16 line(s) … ⟦tj:9219f89a0f07da977142786bdad6ab45⟧ }
}
-}
/* Split a line into arguments, where every argument can be in the
* following programming-language REPL-alike form:
@@ -1008,112 +558,8 @@
sds *sdssplitargs(const char *line, int *argc) {
const char *p = line;
char *current = NULL;
- char **vector = NULL;
-
- *argc = 0;
- while(1) {
- /* skip blanks */
- while(*p && isspace(*p)) p++;
- if (*p) {
- /* get a token */
- int inq=0; /* set to 1 if we are in "quotes" */
- int insq=0; /* set to 1 if we are in 'single quotes' */
- int done=0;
-
- if (current == NULL) current = sdsempty();
- while(!done) {
- if (inq) {
- if (*p == '\\' && *(p+1) == 'x' &&
- is_hex_digit(*(p+2)) &&
- is_hex_digit(*(p+3)))
- {
- unsigned char byte;
-
- byte = (hex_digit_to_int(*(p+2))*16)+
- hex_digit_to_int(*(p+3));
- current = sdscatlen(current,(char*)&byte,1);
- p += 3;
- } else if (*p == '\\' && *(p+1)) {
- char c;
-
- p++;
- switch(*p) {
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case 'b': c = '\b'; break;
- case 'a': c = '\a'; break;
- default: c = *p; break;
- }
- current = sdscatlen(current,&c,1);
- } else if (*p == '"') {
- /* closing quote must be followed by a space or
- * nothing at all. */
- if (*(p+1) && !isspace(*(p+1))) goto err;
- done=1;
- } else if (!*p) {
- /* unterminated quotes */
- goto err;
- } else {
- current = sdscatlen(current,p,1);
- }
- } else if (insq) {
- if (*p == '\\' && *(p+1) == '\'') {
- p++;
- current = sdscatlen(current,"'",1);
- } else if (*p == '\'') {
- /* closing quote must be followed by a space or
- * nothing at all. */
- if (*(p+1) && !isspace(*(p+1))) goto err;
- done=1;
- } else if (!*p) {
- /* unterminated quotes */
- goto err;
- } else {
- current = sdscatlen(current,p,1);
- }
- } else {
- switch(*p) {
- case ' ':
- case '\n':
- case '\r':
- case '\t':
- case '\0':
- done=1;
- break;
- case '"':
- inq=1;
- break;
- case '\'':
- insq=1;
- break;
- default:
- current = sdscatlen(current,p,1);
- break;
- }
- }
- if (*p) p++;
- }
- /* add the token to the vector */
- vector = s_realloc(vector,((*argc)+1)*sizeof(char*));
- vector[*argc] = current;
- (*argc)++;
- current = NULL;
- } else {
- /* Even on empty input string return something not NULL. */
- if (vector == NULL) vector = s_malloc(sizeof(void*));
- return vector;
- }
- }
-
-err:
- while((*argc)--)
- sdsfree(vector[*argc]);
- s_free(vector);
- if (current) sdsfree(current);
- *argc = 0;
+ { … 104 line(s) … ⟦tj:2dc78d37f6f0834677373759f9bb9758⟧ }
return NULL;
-}
/* Modify the string substituting all the occurrences of the set of
* characters specified in the 'from' string to the corresponding character
@@ -1125,44 +571,17 @@
* The function returns the sds string pointer, that is always the same
* as the input pointer since no resize is needed. */
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
- size_t j, i, l = sdslen(s);
+ { … 12 line(s) … ⟦tj:0cabb9f261793766d1d953a296afe4e8⟧ }
- for (j = 0; j < l; j++) {
- for (i = 0; i < setlen; i++) {
- if (s[j] == from[i]) {
- s[j] = to[i];
- break;
- }
- }
- }
- return s;
-}
-
/* Join an array of C strings using the specified separator (also a C string).
* Returns the result as an sds string. */
sds sdsjoin(char **argv, int argc, char *sep) {
- sds join = sdsempty();
- int j;
+ { … 9 line(s) … ⟦tj:fb99d124d1ad17f5cdaf8ad0fc285ce0⟧ }
- for (j = 0; j < argc; j++) {
- join = sdscat(join, argv[j]);
- if (j != argc-1) join = sdscat(join,sep);
- }
- return join;
-}
-
/* Like sdsjoin, but joins an array of SDS strings. */
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
- sds join = sdsempty();
- int j;
+ { … 9 line(s) … ⟦tj:3db2a37e3355bf0471c368cf6a6178a8⟧ }
- for (j = 0; j < argc; j++) {
- join = sdscatsds(join, argv[j]);
- if (j != argc-1) join = sdscatlen(join,sep,seplen);
- }
- return join;
-}
-
/* Wrappers to the allocators used by SDS. Note that SDS will actually
* just use the macros defined into sdsalloc.h in order to avoid to pay
* the overhead of function calls. Here we define these wrappers only for
@@ -1182,52 +601,8 @@
{
sds res = sdsempty();
const char *p = template;
-
- while (*p) {
- /* Find next variable, copy everything until there */
- const char *sv = strchr(p, '{');
- if (!sv) {
- /* Not found: copy till rest of template and stop */
- res = sdscat(res, p);
- break;
- } else if (sv > p) {
- /* Found: copy anything up to the beginning of the variable */
- res = sdscatlen(res, p, sv - p);
- }
-
- /* Skip into variable name, handle premature end or quoting */
- sv++;
- if (!*sv) goto error; /* Premature end of template */
- if (*sv == '{') {
- /* Quoted '{' */
- p = sv + 1;
- res = sdscat(res, "{");
- continue;
- }
-
- /* Find end of variable name, handle premature end of template */
- const char *ev = strchr(sv, '}');
- if (!ev) goto error;
-
- /* Pass variable name to callback and obtain value. If callback failed,
- * abort. */
- sds varname = sdsnewlen(sv, ev - sv);
- sds value = cb_func(varname, cb_arg);
- sdsfree(varname);
- if (!value) goto error;
-
- /* Append value to result and continue */
- res = sdscat(res, value);
- sdsfree(value);
- p = ev + 1;
- }
-
- return res;
-
-error:
- sdsfree(res);
+ { … 44 line(s) … ⟦tj:f1ad479849360fd201ae367b4047bc56⟧ }
return NULL;
-}
#ifdef REDIS_TEST
#include <stdio.h>
@@ -1249,248 +624,9 @@
int sdsTest(int argc, char **argv, int flags) {
UNUSED(argc);
UNUSED(argv);
- UNUSED(flags);
-
- {
- sds x = sdsnew("foo"), y;
-
- test_cond("Create a string and obtain the length",
- sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0);
-
- sdsfree(x);
- x = sdsnewlen("foo",2);
- test_cond("Create a string with specified length",
- sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0);
-
- x = sdscat(x,"bar");
- test_cond("Strings concatenation",
- sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
-
- x = sdscpy(x,"a");
- test_cond("sdscpy() against an originally longer string",
- sdslen(x) == 1 && memcmp(x,"a\0",2) == 0);
-
- x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
- test_cond("sdscpy() against an originally shorter string",
- sdslen(x) == 33 &&
- memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0);
-
- sdsfree(x);
- x = sdscatprintf(sdsempty(),"%d",123);
- test_cond("sdscatprintf() seems working in the base case",
- sdslen(x) == 3 && memcmp(x,"123\0",4) == 0);
-
- sdsfree(x);
- x = sdscatprintf(sdsempty(),"a%cb",0);
- test_cond("sdscatprintf() seems working with \\0 inside of result",
- sdslen(x) == 3 && memcmp(x,"a\0""b\0",4) == 0);
-
- {
- sdsfree(x);
- char etalon[1024*1024];
- for (size_t i = 0; i < sizeof(etalon); i++) {
- etalon[i] = '0';
- }
- x = sdscatprintf(sdsempty(),"%0*d",(int)sizeof(etalon),0);
- test_cond("sdscatprintf() can print 1MB",
- sdslen(x) == sizeof(etalon) && memcmp(x,etalon,sizeof(etalon)) == 0);
- }
-
- sdsfree(x);
- x = sdsnew("--");
- x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
- test_cond("sdscatfmt() seems working in the base case",
- sdslen(x) == 60 &&
- memcmp(x,"--Hello Hi! World -9223372036854775808,"
- "9223372036854775807--",60) == 0);
- printf("[%s]\n",x);
-
- sdsfree(x);
- x = sdsnew("--");
- x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
- test_cond("sdscatfmt() seems working with unsigned numbers",
- sdslen(x) == 35 &&
- memcmp(x,"--4294967295,18446744073709551615--",35) == 0);
-
- sdsfree(x);
- x = sdsnew(" x ");
- sdstrim(x," x");
- test_cond("sdstrim() works when all chars match",
- sdslen(x) == 0);
-
- sdsfree(x);
- x = sdsnew(" x ");
- sdstrim(x," ");
- test_cond("sdstrim() works when a single char remains",
- sdslen(x) == 1 && x[0] == 'x');
-
- sdsfree(x);
- x = sdsnew("xxciaoyyy");
- sdstrim(x,"xy");
- test_cond("sdstrim() correctly trims characters",
- sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0);
-
- y = sdsdup(x);
- sdsrange(y,1,1);
- test_cond("sdsrange(...,1,1)",
- sdslen(y) == 1 && memcmp(y,"i\0",2) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,1,-1);
- test_cond("sdsrange(...,1,-1)",
- sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,-2,-1);
- test_cond("sdsrange(...,-2,-1)",
- sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,2,1);
- test_cond("sdsrange(...,2,1)",
- sdslen(y) == 0 && memcmp(y,"\0",1) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,1,100);
- test_cond("sdsrange(...,1,100)",
- sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,100,100);
- test_cond("sdsrange(...,100,100)",
- sdslen(y) == 0 && memcmp(y,"\0",1) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,4,6);
- test_cond("sdsrange(...,4,6)",
- sdslen(y) == 0 && memcmp(y,"\0",1) == 0);
-
- sdsfree(y);
- y = sdsdup(x);
- sdsrange(y,3,6);
- test_cond("sdsrange(...,3,6)",
- sdslen(y) == 1 && memcmp(y,"o\0",2) == 0);
-
- sdsfree(y);
- sdsfree(x);
- x = sdsnew("foo");
- y = sdsnew("foa");
- test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0);
-
- sdsfree(y);
- sdsfree(x);
- x = sdsnew("bar");
- y = sdsnew("bar");
- test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0);
-
- sdsfree(y);
- sdsfree(x);
- x = sdsnew("aar");
- y = sdsnew("bar");
- test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0);
-
- sdsfree(y);
- sdsfree(x);
- x = sdsnewlen("\a\n\0foo\r",7);
- y = sdscatrepr(sdsempty(),x,sdslen(x));
- test_cond("sdscatrepr(...data...)",