-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringVariant.cpp
More file actions
1504 lines (1333 loc) · 44.3 KB
/
Copy pathStringVariant.cpp
File metadata and controls
1504 lines (1333 loc) · 44.3 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
// @copyright 2017-2018 zzu_softboy <zzu_softboy@163.com>
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Created by zzu_softboy on 2017/08/08.
#include "zapi/ds/StringVariant.h"
#include "zapi/ds/internal/VariantPrivate.h"
#include "zapi/ds/ArrayItemProxy.h"
#include <cstring>
#include <stdexcept>
#include <list>
namespace zapi
{
namespace ds
{
using zapi::ds::internal::VariantPrivate;
const size_t STR_VARIANT_OVERHEAD = ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE;
#ifdef SMART_STR_PAGE
const size_t STR_VARIAMT_PAGE_SIZE = SMART_STR_PAGE; // just use zend default now
#else
const size_t STR_VARIAMT_PAGE_SIZE = 4096;
#endif
#ifdef SMART_STR_START_SIZE
const size_t STR_VARIANT_START_SIZE = SMART_STR_START_SIZE;
#else
const size_t STR_VARIANT_START_SIZE = (256 - STR_VARIANT_OVERHEAD - 1);
#endif
StringVariant::StringVariant()
{
zval *self = getUnDerefZvalPtr();
Z_STR_P(self) = nullptr;
Z_TYPE_INFO_P(self) = IS_STRING_EX;
}
StringVariant::StringVariant(const Variant &other)
{
// chech the type, if the type is not the string, we just try to convert
// if the type is string we deploy copy on write idiom
zval *from = const_cast<zval *>(other.getZvalPtr());
zval *self = getUnDerefZvalPtr();
if (other.getType() == Type::String) {
ZVAL_COPY(self, from);
} else {
zval temp;
// will increase 1 to gc refcount
ZVAL_DUP(&temp, from);
// will decrease 1 to gc refcount
convert_to_string(&temp);
ZVAL_COPY_VALUE(self, &temp);
}
// we just set the capacity equal to default allocator algorithm
// ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len))
getZendStringPtr()->h = ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(Z_STRLEN_P(self)));
}
StringVariant::StringVariant(const StringVariant &other)
: Variant(other)
{
if (other.getUnDerefType() == Type::Reference) {
SEPARATE_STRING(getUnDerefZvalPtr());
}
setCapacity(other.getCapacity());
}
StringVariant::StringVariant(StringVariant &other, bool isRef)
{
zval *self = getUnDerefZvalPtr();
if (!isRef) {
zval *otherPtr = other.getUnDerefZvalPtr();
stdCopyZval(self, const_cast<zval *>(otherPtr));
if (Z_TYPE_P(otherPtr) == IS_REFERENCE) {
SEPARATE_STRING(self);
}
} else {
zval *source = other.getUnDerefZvalPtr();
ZVAL_MAKE_REF(source);
ZVAL_COPY(self, source);
// if the reference string reference count > 1
// separate here
SEPARATE_STRING(getZvalPtr());
}
setCapacity(other.getCapacity());
}
StringVariant::StringVariant(Variant &&other)
: Variant(std::move(other))
{
if (getType() != Type::String) {
convert_to_string(getUnDerefZvalPtr());
}
setCapacity(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(Z_STRLEN_P(getZvalPtr()))));
}
StringVariant::StringVariant(StringVariant &&other) ZAPI_DECL_NOEXCEPT
: Variant(std::move(other))
{}
StringVariant::StringVariant(const std::string &value)
: StringVariant(value.c_str(), value.size())
{}
StringVariant::StringVariant(const char *value, size_t length)
{
// we alloc memory here
// we don't use default ZVAL_STRINGL to setup ourser zval
zend_string *strPtr = nullptr;
strAlloc(strPtr, length, false);
ZVAL_NEW_STR(getUnDerefZvalPtr(), strPtr);
// we need copy memory ourself
memcpy(ZSTR_VAL(strPtr), value, length);
ZSTR_VAL(strPtr)[length] = '\0';
ZSTR_LEN(strPtr) = length;
}
StringVariant::StringVariant(const char *value)
: StringVariant(value, std::strlen(value))
{}
StringVariant::StringVariant(zval &other, bool isRef)
: StringVariant(&other, isRef)
{}
StringVariant::StringVariant(zval &&other, bool isRef)
: StringVariant(&other, isRef)
{}
StringVariant::StringVariant(zval *other, bool isRef)
{
zval *self = getUnDerefZvalPtr();
if (nullptr != other && Z_TYPE_P(other) != IS_NULL) {
if ((isRef && (Z_TYPE_P(other) == IS_STRING ||
(Z_TYPE_P(other) == IS_REFERENCE && Z_TYPE_P(Z_REFVAL_P(other)) == IS_STRING))) ||
(!isRef && (Z_TYPE_P(other) == IS_REFERENCE && Z_TYPE_P(Z_REFVAL_P(other)) == IS_STRING))) {
SEPARATE_STRING(other);
ZVAL_MAKE_REF(other);
zend_reference *ref = Z_REF_P(other);
++GC_REFCOUNT(ref);
ZVAL_REF(self, ref);
} else if ((Z_TYPE_P(other) == IS_STRING ||
(Z_TYPE_P(other) == IS_REFERENCE && Z_TYPE_P(Z_REFVAL_P(other)) == IS_STRING))) {
ZVAL_DEREF(other);
ZVAL_COPY(self, other);
}else {
ZVAL_DUP(self, other);
convert_to_string(self);
}
setCapacity(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(Z_STRLEN_P(getZvalPtr()))));
} else {
Z_STR_P(self) = nullptr;
Z_TYPE_INFO_P(self) = IS_STRING_EX;
}
}
StringVariant &StringVariant::operator =(const StringVariant &other)
{
if (this != &other) {
zval *from = const_cast<zval *>(other.getZvalPtr());
if (getUnDerefType() != Type::Reference && nullptr != getZendStringPtr()) {
SEPARATE_ZVAL_NOREF(getUnDerefZvalPtr());
}
Variant::operator =(from);
setCapacity(getCapacity());
}
return *this;
}
StringVariant &StringVariant::operator =(const Variant &other)
{
zval *self = getZvalPtr();
if (getUnDerefType() != Type::Reference && nullptr != getZendStringPtr()) {
SEPARATE_ZVAL_NOREF(self);
}
zval *from = const_cast<zval *>(other.getZvalPtr());
// need set gc info
if (other.getType() == Type::String) {
// standard copy
Variant::operator =(from);
} else {
zval temp;
// will increase 1 to gc refcount
ZVAL_DUP(&temp, from);
// will decrease 1 to gc refcount
convert_to_string(&temp);
// we need free original zend_string memory
zend_string_free(Z_STR_P(self));
ZVAL_COPY_VALUE(self, &temp);
}
setCapacity(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(Z_STRLEN_P(self))));
return *this;
}
StringVariant &StringVariant::operator =(ArrayItemProxy &&other)
{
return operator =(other.toStringVariant());
}
StringVariant &StringVariant::operator =(char value)
{
ValueType buffer[2] = {value, '\0'};
return operator =(buffer);
}
StringVariant &StringVariant::operator =(const std::string &value)
{
return operator =(value.c_str());
}
StringVariant &StringVariant::operator =(const char *value)
{
zval *self = getZvalPtr();
zend_string *strPtr = getZendStringPtr();
if (getUnDerefType() != Type::Reference && nullptr != strPtr) {
SEPARATE_ZVAL_NOREF(self);
strPtr = getZendStringPtr();
}
size_t length = std::strlen(value);
strReAlloc(strPtr, length, 0);
ConstPointer sourcePtr = value;
Pointer destPtr = ZSTR_VAL(strPtr);
std::memcpy(destPtr, sourcePtr, length);
destPtr[length] = '\0';
ZSTR_LEN(strPtr) = length;
Z_STR_P(self) = strPtr;
return *this;
}
StringVariant &StringVariant::operator +=(const char *str)
{
return append(str);
}
StringVariant &StringVariant::operator +=(const char c)
{
return append(c);
}
StringVariant &StringVariant::operator +=(const std::string &str)
{
return append(str);
}
StringVariant &StringVariant::operator +=(const StringVariant &str)
{
return append(str);
}
StringVariant::Reference StringVariant::operator [](size_t pos)
{
return const_cast<Reference>(static_cast<const StringVariant &>(*this).operator [](pos));
}
StringVariant::ConstReference StringVariant::operator [](size_t pos) const
{
Pointer dataPtr = getRawStrPtr();
return dataPtr[pos];
}
bool StringVariant::operator !=(const char *other) const ZAPI_DECL_NOEXCEPT
{
return 0 != std::memcmp(getCStr(), other, std::max(getLength(), std::strlen(other)));
}
bool StringVariant::operator !=(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
return 0 != std::memcmp(getCStr(), other.c_str(), std::max(getLength(), other.length()));
}
bool StringVariant::operator !=(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
return 0 != std::memcmp(getCStr(), other.getCStr(), std::max(getLength(), other.getLength()));
}
bool StringVariant::operator ==(const char *other) const ZAPI_DECL_NOEXCEPT
{
return 0 == std::memcmp(getCStr(), other, std::max(getLength(), std::strlen(other)));
}
bool StringVariant::operator ==(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
return 0 == std::memcmp(getCStr(), other.c_str(), std::max(getLength(), other.length()));
}
bool StringVariant::operator ==(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
return 0 == std::memcmp(getCStr(), other.getCStr(), std::max(getLength(), other.getLength()));
}
bool StringVariant::operator <(const char *other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other, std::max(getLength(), std::strlen(other))) < 0;
}
bool StringVariant::operator <(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other.c_str(), std::max(getLength(), other.length())) < 0;
}
bool StringVariant::operator <(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other.getCStr(), std::max(getLength(), other.getLength())) < 0;
}
bool StringVariant::operator <=(const char *other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), std::strlen(other));
return std::memcmp(getCStr(), other, len) < 0||
0 == std::memcmp(getCStr(), other, len);
}
bool StringVariant::operator <=(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), other.length());
return std::memcmp(getCStr(), other.c_str(), len) < 0||
0 == std::memcmp(getCStr(), other.c_str(), len);
}
bool StringVariant::operator <=(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), other.getLength());
return std::memcmp(getCStr(), other.getCStr(), len) < 0 ||
0 == std::memcmp(getCStr(), other.getCStr(), len);
}
bool StringVariant::operator >(const char *other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other, std::max(getLength(), std::strlen(other))) > 0;
}
bool StringVariant::operator >(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other.c_str(), std::max(getLength(), other.length())) > 0;
}
bool StringVariant::operator >(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
return std::memcmp(getCStr(), other.getCStr(), std::max(getLength(), other.getLength())) > 0;
}
bool StringVariant::operator >=(const char *other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), std::strlen(other));
return std::memcmp(getCStr(), other, len) > 0||
0 == std::memcmp(getCStr(), other, len);
}
bool StringVariant::operator >=(const std::string &other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), other.length());
return std::memcmp(getCStr(), other.c_str(), len) > 0||
0 == std::memcmp(getCStr(), other.c_str(), len);
}
bool StringVariant::operator >=(const StringVariant &other) const ZAPI_DECL_NOEXCEPT
{
size_t len = std::max(getLength(), other.getLength());
return std::memcmp(getCStr(), other.getCStr(), len) > 0 ||
0 == std::memcmp(getCStr(), other.getCStr(), len);
}
bool StringVariant::toBool() const ZAPI_DECL_NOEXCEPT
{
return getType() != Type::Null && 0 != Z_STRLEN_P(const_cast<zval *>(getZvalPtr()));
}
std::string StringVariant::toString() const ZAPI_DECL_NOEXCEPT
{
zend_string *str = getZendStringPtr();
if (!str) {
return std::string();
}
return std::string(str->val, str->len);
}
std::string StringVariant::toLowerCase() const
{
if (isEmpty()) {
return std::string();
}
std::string ret(cbegin(), cend());
return zapi::utils::str_tolower(ret);
}
std::string StringVariant::toUpperCase() const
{
if (isEmpty()) {
return std::string();
}
std::string ret(cbegin(), cend());
return zapi::utils::str_toupper(ret);
}
StringVariant::Reference StringVariant::at(SizeType pos)
{
return const_cast<StringVariant::Reference>(const_cast<const StringVariant &>(*this).at(pos));
}
StringVariant::ConstReference StringVariant::at(SizeType pos) const
{
if (pos >= getSize()) {
throw std::out_of_range("string pos out of range");
}
char *str = getRawStrPtr();
return str[pos];
}
StringVariant::Iterator StringVariant::begin() ZAPI_DECL_NOEXCEPT
{
return getRawStrPtr();
}
StringVariant::ConstrIterator StringVariant::begin() const ZAPI_DECL_NOEXCEPT
{
return getCStr();
}
StringVariant::ConstrIterator StringVariant::cbegin() const ZAPI_DECL_NOEXCEPT
{
return getCStr();
}
StringVariant::ReverseIterator StringVariant::rbegin() ZAPI_DECL_NOEXCEPT
{
return ReverseIterator(getRawStrPtr() + getLength());
}
StringVariant::ConstReverseIterator StringVariant::rbegin() const ZAPI_DECL_NOEXCEPT
{
return ConstReverseIterator(getCStr() + getLength());
}
StringVariant::ConstReverseIterator StringVariant::crbegin() const ZAPI_DECL_NOEXCEPT
{
return ConstReverseIterator(getCStr() + getLength());
}
StringVariant::Iterator StringVariant::end() ZAPI_DECL_NOEXCEPT
{
return getRawStrPtr() + getLength();
}
StringVariant::ConstrIterator StringVariant::end() const ZAPI_DECL_NOEXCEPT
{
return getCStr() + getLength();
}
StringVariant::ConstrIterator StringVariant::cend() const ZAPI_DECL_NOEXCEPT
{
return getCStr() + getLength();
}
StringVariant::ReverseIterator StringVariant::rend() ZAPI_DECL_NOEXCEPT
{
return ReverseIterator(getRawStrPtr());
}
StringVariant::ConstReverseIterator StringVariant::rend() const ZAPI_DECL_NOEXCEPT
{
return ConstReverseIterator(getCStr());
}
StringVariant::ConstReverseIterator StringVariant::crend() const ZAPI_DECL_NOEXCEPT
{
return ConstReverseIterator(getCStr());
}
zapi_long StringVariant::indexOf(const char *needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
if (isEmpty()) {
return -1;
}
Pointer haystack = getRawStrPtr();
size_t haystackLength = getSize();
size_t needleLength = std::strlen(needle);
GuardValuePtrType haystackDup(nullptr, zapi::utils::std_php_memory_deleter);
GuardValuePtrType needleDup(nullptr, zapi::utils::std_php_memory_deleter);
ConstPointer found = nullptr;
if (offset < 0) {
offset += haystackLength;
}
if (offset < 0 || static_cast<size_t>(offset) > haystackLength) {
// TODO need throw exception here or return -1 ?
return -1;
}
if (0 == needleLength) {
return -1;
}
if (!caseSensitive) {
haystackDup.reset(static_cast<Pointer>(emalloc(haystackLength)));
needleDup.reset(static_cast<Pointer>(emalloc(needleLength)));
std::memcpy(haystackDup.get(), haystack, haystackLength);
std::memcpy(needleDup.get(), needle, needleLength);
haystack = haystackDup.get();
needle = needleDup.get();
zapi::utils::str_tolower(haystackDup.get(), haystackLength);
zapi::utils::str_tolower(needleDup.get(), needleLength);
}
found = zend_memnstr(haystack + offset, needle, needleLength,
haystack + haystackLength);
if (nullptr != found) {
return found - haystack;
}
return -1;
}
zapi_long StringVariant::indexOf(const StringVariant &needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return indexOf(needle.getCStr(), offset, caseSensitive);
}
zapi_long StringVariant::indexOf(const std::string &needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return indexOf(needle.c_str(), offset, caseSensitive);
}
zapi_long StringVariant::indexOf(const char needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
ValueType buffer[2] = {needle, '\0'};
return indexOf(reinterpret_cast<Pointer>(buffer), offset, caseSensitive);
}
zapi_long StringVariant::lastIndexOf(const char *needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
if (isEmpty()) {
return -1;
}
Pointer haystack = getRawStrPtr();
size_t haystackLength = getSize();
size_t needleLength = std::strlen(needle);
GuardValuePtrType haystackDup(nullptr, zapi::utils::std_php_memory_deleter);
GuardValuePtrType needleDup(nullptr, zapi::utils::std_php_memory_deleter);
Pointer p = nullptr;
Pointer e = nullptr;
ConstPointer found = nullptr;
if (!caseSensitive) {
haystackDup.reset(static_cast<Pointer>(emalloc(haystackLength)));
needleDup.reset(static_cast<Pointer>(emalloc(needleLength)));
std::memcpy(haystackDup.get(), haystack, haystackLength);
std::memcpy(needleDup.get(), needle, needleLength);
haystack = haystackDup.get();
needle = needleDup.get();
zapi::utils::str_tolower(haystackDup.get(), haystackLength);
zapi::utils::str_tolower(needleDup.get(), needleLength);
}
if (offset >= 0) {
if (static_cast<size_t>(offset) > haystackLength) {
// here we do like php
php_error_docref(NULL, E_WARNING, "Offset is greater than the length of haystack string");
return -1;
}
p = haystack + static_cast<size_t>(offset);
e = haystack + haystackLength;
} else {
if (offset < -INT_MAX || static_cast<size_t>(-offset) > haystackLength) {
php_error_docref(NULL, E_WARNING, "Offset is greater than the length of haystack string");
return -1;
}
p = haystack;
// find the best end pos
if (static_cast<size_t>(-offset) < needleLength) {
e = haystack + haystackLength;
} else {
e = haystack + haystackLength + offset + needleLength;
}
}
found = zend_memnrstr(p, needle, needleLength, e);
if (nullptr != found) {
return found - haystack;
}
return -1;
}
zapi_long StringVariant::lastIndexOf(const StringVariant &needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return lastIndexOf(needle.getCStr(), offset, caseSensitive);
}
zapi_long StringVariant::lastIndexOf(const std::string &needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return lastIndexOf(needle.c_str(), offset, caseSensitive);
}
zapi_long StringVariant::lastIndexOf(const char needle, zapi_long offset,
bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
ValueType buffer[2] = {needle, '\0'};
return lastIndexOf(reinterpret_cast<Pointer>(buffer), offset, caseSensitive);
}
bool StringVariant::contains(const StringVariant &needle, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return -1 != indexOf(needle.getCStr(), 0, caseSensitive);
}
bool StringVariant::contains(const char *needle, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return -1 != indexOf(needle, 0, caseSensitive);
}
bool StringVariant::contains(const std::string &needle, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return -1 != indexOf(needle, 0, caseSensitive);
}
bool StringVariant::contains(const char needle, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return -1 != indexOf(needle, 0, caseSensitive);
}
bool StringVariant::startsWith(const StringVariant &str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return startsWith(str.getCStr(), caseSensitive);
}
bool StringVariant::startsWith(const char *str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
if (isEmpty()) {
return false;
}
Pointer selfStr = getRawStrPtr();
Pointer otherStrPtr = const_cast<Pointer>(str);
GuardValuePtrType selfStrLowerCase(nullptr, zapi::utils::std_php_memory_deleter);
GuardValuePtrType otherStrLowerCase(nullptr, zapi::utils::std_php_memory_deleter);
size_t selfStrLength = getSize();
size_t otherStrLength = std::strlen(str);
if (selfStrLength < otherStrLength) {
return false;
}
if (!caseSensitive) {
selfStrLowerCase.reset(static_cast<Pointer>(emalloc(selfStrLength)));
otherStrLowerCase.reset(static_cast<Pointer>(emalloc(otherStrLength)));
std::memcpy(selfStrLowerCase.get(), selfStr, selfStrLength);
std::memcpy(otherStrLowerCase.get(), otherStrPtr, otherStrLength);
selfStr = selfStrLowerCase.get();
otherStrPtr = otherStrLowerCase.get();
zapi::utils::str_tolower(selfStr);
zapi::utils::str_tolower(otherStrPtr);
}
return 0 == std::memcmp(selfStr, otherStrPtr, otherStrLength);
}
bool StringVariant::startsWith(const std::string &str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return startsWith(str.c_str(), caseSensitive);
}
bool StringVariant::startsWith(char c, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
ValueType buffer[2] = {c, '\0'};
return startsWith(reinterpret_cast<Pointer>(buffer), caseSensitive);
}
bool StringVariant::endsWith(const StringVariant &str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return endsWith(str.getCStr(), caseSensitive);
}
bool StringVariant::endsWith(const char *str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
if (isEmpty()) {
return false;
}
Pointer selfStr = getRawStrPtr();
Pointer otherStrPtr = const_cast<Pointer>(str);
GuardValuePtrType selfStrLowerCase(nullptr, zapi::utils::std_php_memory_deleter);
GuardValuePtrType otherStrLowerCase(nullptr, zapi::utils::std_php_memory_deleter);
size_t selfStrLength = getSize();
size_t otherStrLength = std::strlen(str);
if (selfStrLength < otherStrLength) {
return false;
}
if (!caseSensitive) {
selfStrLowerCase.reset(static_cast<Pointer>(emalloc(selfStrLength)));
otherStrLowerCase.reset(static_cast<Pointer>(emalloc(otherStrLength)));
std::memcpy(selfStrLowerCase.get(), selfStr, selfStrLength);
std::memcpy(otherStrLowerCase.get(), otherStrPtr, otherStrLength);
selfStr = selfStrLowerCase.get();
otherStrPtr = otherStrLowerCase.get();
zapi::utils::str_tolower(selfStr);
zapi::utils::str_tolower(otherStrPtr);
}
return 0 == std::memcmp(selfStr + selfStrLength - otherStrLength, otherStrPtr, otherStrLength);
}
bool StringVariant::endsWith(const std::string &str, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
return endsWith(str.c_str(), caseSensitive);
}
bool StringVariant::endsWith(char c, bool caseSensitive) const ZAPI_DECL_NOEXCEPT
{
ValueType buffer[2] = {c, '\0'};
return endsWith(reinterpret_cast<Pointer>(buffer), caseSensitive);
}
StringVariant &StringVariant::prepend(const char *str)
{
zval *self = getZvalPtr();
zend_string *destStrPtr = getZendStringPtr();
if (getUnDerefType() != Type::Reference && nullptr != destStrPtr) {
SEPARATE_ZVAL_NOREF(self);
destStrPtr = getZendStringPtr();
}
size_t length = std::strlen(str);
size_t selfLength = getSize();
size_t newLength = strAlloc(destStrPtr, length, 0);
Pointer newRawStr = ZSTR_VAL(destStrPtr);
// copy backward
size_t iterator = selfLength;
while (iterator--) {
*(newRawStr + iterator + length) = *(newRawStr + iterator);
}
// copy prepend
std::memcpy(newRawStr, str, length);
// set self state
ZSTR_VAL(destStrPtr)[newLength] = '\0';
ZSTR_LEN(destStrPtr) = newLength;
Z_STR_P(self) = destStrPtr;
return *this;
}
StringVariant &StringVariant::prepend(const char c)
{
ValueType buffer[2] = {c, '\0'};
return prepend(reinterpret_cast<Pointer>(buffer));
}
StringVariant &StringVariant::prepend(const std::string &str)
{
return prepend(str.c_str());
}
StringVariant &StringVariant::prepend(const StringVariant &str)
{
return prepend(str.getCStr());
}
StringVariant &StringVariant::append(const char *str)
{
zval *self = getZvalPtr();
zend_string *destStrPtr = getZendStringPtr();
if (getUnDerefType() != Type::Reference && nullptr != destStrPtr) {
SEPARATE_ZVAL_NOREF(self);
destStrPtr = getZendStringPtr();
}
size_t length = std::strlen(str);
size_t newLength = strAlloc(destStrPtr, length, 0);
std::memcpy(ZSTR_VAL(destStrPtr) + getLength(), str, length);
// set self state
ZSTR_VAL(destStrPtr)[newLength] = '\0';
ZSTR_LEN(destStrPtr) = newLength;
Z_STR_P(self) = destStrPtr;
return *this;
}
StringVariant &StringVariant::append(const char c)
{
ValueType buffer[2] = {c, '\0'};
return append(reinterpret_cast<Pointer>(buffer));
}
StringVariant &StringVariant::append(const std::string &str)
{
return append(str.c_str());
}
StringVariant &StringVariant::append(const StringVariant &str)
{
return append(str.getCStr());
}
StringVariant &StringVariant::remove(size_t pos, size_t length)
{
size_t selfLength = getLength();
if (pos > selfLength) {
throw std::out_of_range("string pos out of range");
}
// implement php copy on write idiom
if (getUnDerefType() != Type::Reference) {
SEPARATE_ZVAL_NOREF(getUnDerefZvalPtr());
}
Pointer strPtr = getRawStrPtr();
if (pos + length < selfLength) {
size_t needCopy = selfLength - pos - length;
std::memmove(strPtr + pos, strPtr + pos + length, needCopy);
}
size_t newLength = selfLength - std::min(length, selfLength - pos);
*(strPtr + newLength) = '\0';
ZSTR_LEN(getZendStringPtr()) = newLength;
return *this;
}
StringVariant &StringVariant::remove(const char *str, bool caseSensitive)
{
size_t length = std::strlen(str);
zapi_long pos = -1;
while (-1 != (pos = indexOf(str, 0, caseSensitive))) {
remove(static_cast<size_t>(pos), length);
}
return *this;
}
StringVariant &StringVariant::remove(char c, bool caseSensitive)
{
ValueType buffer[2] = {c, '\0'};
return remove(reinterpret_cast<Pointer>(buffer), caseSensitive);
}
StringVariant &StringVariant::remove(const std::string &str, bool caseSensitive)
{
return remove(str.c_str(), caseSensitive);
}
StringVariant &StringVariant::remove(const StringVariant &str, bool caseSensitive)
{
return remove(str.getCStr(), caseSensitive);
}
StringVariant &StringVariant::insert(size_t pos, const char *str)
{
zval *self = getZvalPtr();
zend_string *destStrPtr = getZendStringPtr();
if (getUnDerefType() != Type::Reference && nullptr != destStrPtr) {
SEPARATE_ZVAL_NOREF(self);
destStrPtr = getZendStringPtr();
}
size_t selfLength = getLength();
if (pos > selfLength) {
throw std::out_of_range("string pos out of range");
}
size_t length = std::strlen(str);
size_t newLength = strAlloc(destStrPtr, length, 0);
Pointer dataPtr = ZSTR_VAL(destStrPtr);
size_t iterator = selfLength - pos;
Pointer newDataEnd = dataPtr + newLength;
while (iterator--) {
*(--newDataEnd) = *(dataPtr + pos + iterator);
}
std::memcpy(dataPtr + pos, str, length);
// set self state
*(dataPtr + newLength) = '\0';
ZSTR_LEN(destStrPtr) = newLength;
Z_STR_P(self) = destStrPtr;
return *this;
}
StringVariant &StringVariant::insert(size_t pos, const char c)
{
ValueType buffer[2] = {c, '\0'};
return insert(pos, reinterpret_cast<Pointer>(buffer));
}
StringVariant &StringVariant::insert(size_t pos, const std::string &str)
{
return insert(pos, str.c_str());
}
StringVariant &StringVariant::insert(size_t pos, const StringVariant &str)
{
return insert(pos, str.getCStr());
}
StringVariant &StringVariant::replace(size_t pos, size_t length, const char *replace)
{
remove(pos, length);
insert(pos, replace);
return *this;
}
StringVariant &StringVariant::replace(size_t pos, size_t length, const char replace)
{
remove(pos, length);
insert(pos, replace);
return *this;
}
StringVariant &StringVariant::replace(size_t pos, size_t length, const std::string &replace)
{
remove(pos, length);
insert(pos, replace);
return *this;
}
StringVariant &StringVariant::replace(size_t pos, size_t length, const StringVariant &replace)
{
remove(pos, length);
insert(pos, replace);
return *this;
}
StringVariant &StringVariant::replace(const char *search, const char *replaceStr, bool caseSensitive)
{
size_t length = std::strlen(search);
zapi_long pos = -1;
size_t startPos = 0;
while (-1 != (pos = indexOf(search, startPos, caseSensitive))) {
replace(static_cast<size_t>(pos), length, replaceStr);
startPos = pos + length;
}
return *this;
}
StringVariant &StringVariant::replace(char search, char replaceStr, bool caseSensitive)
{
zapi_long pos = -1;
size_t startPos = 0;
while (-1 != (pos = indexOf(search, startPos, caseSensitive))) {
replace(static_cast<size_t>(pos), 1, replaceStr);
startPos = pos + 1;
}
return *this;
}
StringVariant &StringVariant::replace(char search, const char *replaceStr, bool caseSensitive)
{
ValueType buffer[2] = {search, '\0'};
return replace(reinterpret_cast<Pointer>(buffer), replaceStr, caseSensitive);
}
StringVariant &StringVariant::replace(char search, const std::string &replaceStr, bool caseSensitive)
{
return replace(search, replaceStr.c_str(), caseSensitive);
}
StringVariant &StringVariant::replace(char search, const StringVariant &replaceStr, bool caseSensitive)
{
return replace(search, replaceStr.getCStr(), caseSensitive);
}
StringVariant &StringVariant::replace(const char *search, const std::string &replaceStr, bool caseSensitive)
{
return replace(search, replaceStr.c_str(), caseSensitive);
}
StringVariant &StringVariant::replace(const char *search, const StringVariant &replaceStr, bool caseSensitive)
{
return replace(search, replaceStr.getCStr(), caseSensitive);
}
StringVariant &StringVariant::replace(const std::string &search, const char *replaceStr, bool caseSensitive)
{
return replace(search.c_str(), replaceStr, caseSensitive);
}
StringVariant &StringVariant::replace(const std::string &search, const std::string &replaceStr, bool caseSensitive)
{
return replace(search.c_str(), replaceStr.c_str(), caseSensitive);
}
StringVariant &StringVariant::replace(const std::string &search, const StringVariant &replaceStr, bool caseSensitive)
{
return replace(search.c_str(), replaceStr.getCStr(), caseSensitive);
}
StringVariant &StringVariant::replace(const StringVariant &search, const char *replaceStr, bool caseSensitive)
{
return replace(search.getCStr(), replaceStr, caseSensitive);
}
StringVariant &StringVariant::replace(const StringVariant &search, const std::string &replaceStr, bool caseSensitive)
{
return replace(search.getCStr(), replaceStr.c_str(), caseSensitive);
}
StringVariant &StringVariant::replace(const StringVariant &search, const StringVariant &replaceStr, bool caseSensitive)
{
return replace(search.getCStr(), replaceStr.getCStr(), caseSensitive);
}
StringVariant &StringVariant::clear()
{
// here we release zend_string memory
// and set capacity to zero
zval *self = getZvalPtr();
if (nullptr == getZendStringPtr()) {
return *this;
}
if (getUnDerefType() != Type::Reference) {