forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-string.h
621 lines (515 loc) · 18.8 KB
/
type-string.h
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#pragma once
#include "hphp/runtime/base/req-ptr.h"
#include "hphp/runtime/base/static-string-table.h"
#include "hphp/runtime/base/string-data.h"
#include "hphp/runtime/base/typed-value.h"
#include "hphp/util/assertions.h"
#include <algorithm>
namespace HPHP {
//////////////////////////////////////////////////////////////////////
struct VarNR;
struct VariableSerializer;
struct VariableUnserializer;
// reserve space for buffer that will be filled in by client.
enum ReserveStringMode { ReserveString };
//////////////////////////////////////////////////////////////////////
namespace {
/*
* Don't actually perform a shrink unless the savings meets this
* threshold.
*/
constexpr int kMinShrinkThreshold = 1024;
}
//////////////////////////////////////////////////////////////////////
// Built strings will be uncounted, or will have their reference counts
// pre-initialized to 1.
StringData* buildStringData(int n);
StringData* buildStringData(int64_t n);
StringData* buildStringData(double n);
std::string convDblToStrWithPhpFormat(double n);
//////////////////////////////////////////////////////////////////////
/**
* String type wrapping around StringData to implement copy-on-write and
* literal string handling (to avoid string copying).
*/
struct String {
protected:
req::ptr<StringData> m_str;
using NoIncRef = req::ptr<StringData>::NoIncRef;
String(StringData* sd, NoIncRef) : m_str(sd, NoIncRef{}) {}
public:
static const int MinPrecomputedInteger = SCHAR_MIN;
static const int MaxPrecomputedInteger = 4095 + SCHAR_MIN;
static StringData const **converted_integers_raw;
static StringData const **converted_integers;
static bool HasConverted(int64_t n) {
return MinPrecomputedInteger <= n && n <= MaxPrecomputedInteger;
}
static bool HasConverted(int n) {
return HasConverted((int64_t)n);
}
// create a string from a character
static String FromChar(char ch) {
return String{makeStaticString(ch)};
}
static const StringData *ConvertInteger(int64_t n);
static const StringData *GetIntegerStringData(int64_t n) {
if (HasConverted(n)) {
return *(converted_integers + n);
}
return nullptr;
}
public:
String() {}
~String();
StringData* get() const { return m_str.get(); }
void reset(StringData* str = nullptr) { m_str.reset(str); }
// Transfer ownership of our reference to this StringData.
StringData* detach() { return m_str.detach(); }
/**
* Constructors
*/
explicit String(StringData *data) : m_str(data) { }
/* implicit */ String(int n);
/* implicit */ String(int64_t n);
/* implicit */ String(double n);
/* implicit */ String(const char* s)
: m_str(LIKELY((bool)s) ? StringData::Make(s, CopyString)
: nullptr, NoIncRef{}) { }
String(const String& str) : m_str(str.m_str) { }
/* implicit */ String(const StaticString& str);
/* Prevent unintentional promotion. */
/* implicit */ String(char) = delete;
/* implicit */ String(Variant&&) = delete;
// Disable this---otherwise this would generally implicitly create a
// Variant(bool) and then call String(Variant&&) ...
/* implicit */ String(const StringData*) = delete;
// Move ctor
/* implicit */ String(String&& str) noexcept : m_str(std::move(str.m_str)) {}
// Move assign
String& operator=(String&& src) {
m_str = std::move(src.m_str);
return *this;
}
String& operator=(req::ptr<StringData>&& src) {
m_str = std::move(src);
return *this;
}
String& operator=(const Variant&) = delete;
String& operator=(Variant&&) = delete;
/* implicit */ String(const std::string &s)
: m_str(StringData::Make(s.data(), s.size(), CopyString), NoIncRef{}) { }
/* implicit */ String(folly::StringPiece s)
: m_str(StringData::Make(s), NoIncRef{}) {}
// attach to null terminated malloc'ed string, maybe free it now.
String(char* s, AttachStringMode mode)
: m_str(LIKELY((bool)s) ? StringData::Make(s, mode) : nullptr, NoIncRef{}) {}
// copy a null terminated string
String(const char *s, CopyStringMode mode)
: m_str(LIKELY((bool)s) ? StringData::Make(s, mode) : nullptr, NoIncRef{}) {}
// attach to binary malloc'ed string
String(char* s, size_t length, AttachStringMode mode)
: m_str(LIKELY((bool)s) ? StringData::Make(s, length, mode)
: nullptr, NoIncRef{}) { }
// make copy of binary binary string
String(const char *s, size_t length, CopyStringMode mode)
: m_str(LIKELY((bool)s) ? StringData::Make(s, length, mode)
: nullptr, NoIncRef{}) { }
// force a copy of a String
String(const String& s, CopyStringMode mode)
: m_str(LIKELY((bool)s) ? StringData::Make(s.c_str(), s.size(), mode)
: nullptr, NoIncRef{}) {}
// make an empty string with cap reserve bytes, plus 1 for '\0'
String(size_t cap, ReserveStringMode /*mode*/)
: m_str(StringData::Make(cap), NoIncRef{}) {}
static String attach(StringData* sd) {
return String(sd, NoIncRef{});
}
void clear() { reset();}
/**
* Informational
*/
const char *data() const {
return m_str ? m_str->data() : "";
}
char *mutableData() const {
return m_str->mutableData();
}
public:
const String& setSize(int64_t len) {
assertx(m_str);
m_str->setSize(len);
return *this;
}
const String& shrink(size_t len) {
assertx(m_str && !m_str->isImmutable());
if (m_str->capacity() - len > kMinShrinkThreshold) {
m_str = req::ptr<StringData>::attach(m_str->shrinkImpl(len));
} else {
assertx(len < StringData::MaxSize);
m_str->setSize(len);
}
return *this;
}
folly::MutableStringPiece reserve(size_t size) {
if (!m_str) return folly::MutableStringPiece();
auto const tmp = m_str->reserve(size);
if (UNLIKELY(tmp != m_str)) {
m_str = req::ptr<StringData>::attach(tmp);
}
return m_str->bufferSlice();
}
const char *c_str() const {
return m_str ? m_str->data() : "";
}
bool empty() const {
return m_str ? m_str->empty() : true;
}
int64_t size() const {
return m_str ? m_str->size() : 0;
}
int64_t length() const {
return m_str ? m_str->size() : 0;
}
uint32_t capacity() const {
return m_str->capacity(); // intentionally skip nullptr check
}
folly::StringPiece slice() const {
return m_str ? m_str->slice() : folly::StringPiece();
}
folly::MutableStringPiece bufferSlice() {
return m_str ? m_str->bufferSlice() : folly::MutableStringPiece();
}
bool isNull() const { return !m_str; }
bool isNumeric() const {
return m_str ? m_str->isNumeric() : false;
}
bool isInteger() const {
return m_str ? m_str->isInteger() : false;
}
bool isZero() const {
return m_str ? m_str->isZero() : false;
}
String substr(int start, int length = StringData::MaxSize) const {
return String::attach(
m_str ? m_str->substr(start, length) : staticEmptyString());
}
/**
* Find a character or a substring and return its position. "pos" has to be
* within current string and it's the start point for searching.
*/
static const int npos = -1;
int find(char ch, int pos = 0, bool caseSensitive = true) const;
int find(const char *s, int pos = 0, bool caseSensitive = true) const;
int find(const String& s, int pos = 0, bool caseSensitive = true) const;
int rfind(char ch, int pos = 0, bool caseSensitive = true) const;
int rfind(const char *s, int pos = 0, bool caseSensitive = true) const;
int rfind(const String& s, int pos = 0, bool caseSensitive = true) const;
/**
* Operators
*/
String& operator=(StringData *data) {
m_str = data;
return *this;
}
String& operator=(const String& v) {
m_str = v.m_str;
return *this;
}
String& operator=(const StaticString& v);
String& operator=(const char* v);
String& operator=(const std::string &s);
// These should be members, but g++ doesn't yet support the rvalue
// reference notation on lhs (http://goo.gl/LuCTo).
friend String&& operator+(String&& lhs, const char* rhs);
friend String&& operator+(String&& lhs, String&& rhs);
friend String operator+(String&& lhs, const String & rhs);
friend String operator+(const String& lhs, const char* rhs);
friend String operator+(const String & lhs, const String & rhs);
String& operator += (const char* v);
String& operator += (const String& v);
String& operator += (const std::string& v);
String& operator += (folly::StringPiece slice);
String& operator += (folly::MutableStringPiece slice);
String operator | (const String& v) const = delete;
String operator & (const String& v) const = delete;
String operator ^ (const String& v) const = delete;
String& operator |= (const String& v) = delete;
String& operator &= (const String& v) = delete;
String& operator ^= (const String& v) = delete;
String operator ~ () const = delete;
explicit operator std::string () const { return toCppString(); }
explicit operator bool() const { return (bool)m_str; }
/**
* These are convenient functions for writing extensions, since code
* generation always uses explicit functions like same(), less() etc. that
* are type specialized and unambiguous.
*/
bool operator == (const char* v) const = delete;
bool operator != (const char* v) const = delete;
bool operator >= (const char* v) const = delete;
bool operator <= (const char* v) const = delete;
bool operator > (const char* v) const = delete;
bool operator < (const char* v) const = delete;
bool operator == (const String& v) const;
bool operator != (const String& v) const;
bool operator >= (const String& v) const = delete;
bool operator <= (const String& v) const = delete;
bool operator > (const String& v) const;
bool operator < (const String& v) const;
bool operator == (const Variant& v) const = delete;
bool operator != (const Variant& v) const = delete;
bool operator >= (const Variant& v) const = delete;
bool operator <= (const Variant& v) const = delete;
bool operator > (const Variant& v) const = delete;
bool operator < (const Variant& v) const = delete;
/**
* Type conversions
*/
bool toBoolean() const { return m_str ? m_str->toBoolean() : false;}
char toByte () const { return m_str ? m_str->toByte () : 0;}
short toInt16 () const { return m_str ? m_str->toInt16 () : 0;}
int toInt32 () const { return m_str ? m_str->toInt32 () : 0;}
int64_t toInt64 () const { return m_str ? m_str->toInt64 () : 0;}
double toDouble () const { return m_str ? m_str->toDouble () : 0;}
std::string toCppString() const { return std::string(c_str(), size()); }
TypedValue asTypedValue() const {
assertx(m_str);
return make_tv<KindOfString>(m_str.get());
}
/**
* Comparisons
*/
bool same (const char* v2) const = delete;
bool same (const StringData *v2) const;
bool same (const String& v2) const;
bool same (const Array& v2) const = delete;
bool same (const Object& v2) const = delete;
bool same (const Resource& v2) const = delete;
bool equal(const char* v2) const = delete;
bool equal(const StringData *v2) const;
bool equal(const String& v2) const;
bool equal(const Array& v2) const = delete;
bool equal(const Object& v2) const = delete;
bool equal(const Resource& v2) const = delete;
bool less (const char* v2) const = delete;
bool less (const StringData *v2) const;
bool less (const String& v2) const;
bool less (const Array& v2) const = delete;
bool less (const Object& v2) const = delete;
bool less (const Resource& v2) const = delete;
bool more (const char* v2) const = delete;
bool more (const StringData *v2) const;
bool more (const String& v2) const;
bool more (const Array& v2) const = delete;
bool more (const Object& v2) const = delete;
bool more (const Resource& v2) const = delete;
int compare(const char* v2) const;
int compare(const String& v2) const;
/**
* Offset
*/
String rval(bool key) const { return rvalImpl(key ? 1 : 0);}
String rval(char key) const { return rvalImpl(key);}
String rval(short key) const { return rvalImpl(key);}
String rval(int key) const { return rvalImpl(key);}
String rval(int64_t key) const { return rvalImpl(key);}
String rval(double key) const { return rvalImpl((int64_t)key);}
String rval(const char* key) const {
return rvalImpl(String(key).toInt32());
}
String rval(const StringData *key) const {
not_reached();
return rvalImpl(key ? key->toInt32() : 0);
}
String rval(const String& key) const { return rvalImpl(key.toInt32());}
String rval(const Array& key) const = delete;
String rval(const Object& key) const = delete;
String rval(const Variant& key) const = delete;
/**
* Returns one character at specified position.
*/
char charAt(int pos) const;
char operator[](int pos) const { return charAt(pos);}
/**
* Debugging
*/
void dump() const;
template <class Op> ALWAYS_INLINE
String forEachByte(Op action) const {
String ret = String(size(), ReserveString);
auto srcSlice = slice();
const char* src = srcSlice.begin();
const char* end = srcSlice.end();
char* dst = ret.mutableData();
for (; src != end; ++src, ++dst) {
*dst = action(*src);
}
ret.setSize(size());
return ret;
}
template <class Op> ALWAYS_INLINE
String forEachByteFast(Op action) const {
if (this->empty()) {
return *this;
}
return forEachByte(action);
}
private:
String rvalImpl(int key) const {
if (m_str) {
return String{m_str->getChar(key)};
}
return String();
}
static void compileTimeAssertions() {
static_assert(sizeof(String) == sizeof(req::ptr<StringData>), "");
}
};
extern const String null_string;
///////////////////////////////////////////////////////////////////////////////
// StrNR
struct StrNR {
private:
StringData *m_px;
public:
StrNR() : m_px(nullptr) {}
explicit StrNR(StringData *sd) : m_px(sd) {}
explicit StrNR(const StringData *sd) : m_px(const_cast<StringData*>(sd)) {}
explicit StrNR(const String &s) : m_px(s.get()) {} // XXX
explicit StrNR(const char*) = delete;
~StrNR() {
if (debug) {
m_px = reinterpret_cast<StringData*>(0xdeadbeeffaceb004);
}
}
/* implicit */ operator const String&() const { return asString(); }
const char* data() const { return m_px ? m_px->data() : ""; }
const char* c_str() const { return data(); }
int size() const { return m_px ? m_px->size() : 0; }
bool empty() const { return size() == 0; }
uint32_t capacity() const {
return m_px->capacity(); // intentionally skip nullptr check
}
String& asString() {
return *reinterpret_cast<String*>(this);
}
const String& asString() const {
return const_cast<StrNR*>(this)->asString();
}
StringData* get() const { return m_px; }
private:
static void compileTimeAssertions() {
static_assert(offsetof(StrNR, m_px) == kExpectedMPxOffset, "");
}
};
///////////////////////////////////////////////////////////////////////////////
/**
* A StaticString can be co-accessed by multiple threads, therefore they are
* not thread local.
*/
struct StaticString : String {
TYPE_SCAN_IGNORE_BASES(String);
template<size_t N> explicit StaticString(const char(&s)[N]) {
construct(s, N - 1);
}
template<size_t N> explicit StaticString(char(&s)[N]) = delete;
~StaticString() {
// prevent ~req::ptr from destroying contents.
detach();
}
StaticString(const StaticString& other) {
m_str = other.get();
}
StaticString& operator=(const StaticString& other) = delete;
StringData* get() const {
assertx(valid());
return m_str.get();
}
bool valid() const {
if (s_globalInit) {
assertx(m_str);
return true;
}
return false;
}
// Return number of lazily created StaticStrings, must run before creating
// VM threads.
static uint32_t CreateAll();
// StaticStrings created before s_globalInit is set will be created lazily,
// upon create(). Those are expected to happen during process initialization
// before VM threads are created, so no need to worry about thread safety.
static bool s_globalInit;
private:
void construct(const char* s, size_t len);
void init(const char* s, size_t len);
static std::vector<std::tuple<const char*, size_t, StaticString*>>*
s_registered;
};
StaticString getDataTypeString(DataType t, bool isLegacy = false);
//////////////////////////////////////////////////////////////////////
inline String::String(const StaticString& str) :
m_str(str.m_str.get(), NoIncRef{}) {
assertx(str.m_str->isStatic());
}
inline String& String::operator=(const StaticString& v) {
m_str = req::ptr<StringData>::attach(v.m_str.get());
return *this;
}
//////////////////////////////////////////////////////////////////////
ALWAYS_INLINE String empty_string() {
return String::attach(staticEmptyString());
}
ALWAYS_INLINE TypedValue empty_string_tv() {
return make_tv<KindOfPersistentString>(staticEmptyString());
}
//////////////////////////////////////////////////////////////////////
ALWAYS_INLINE String& asStrRef(tv_lval tv) {
assertx(tvIsPlausible(*tv));
assertx(isStringType(type(tv)));
type(tv) = KindOfString;
return reinterpret_cast<String&>(val(tv).pstr);
}
ALWAYS_INLINE const String& asCStrRef(tv_rval tv) {
assertx(tvIsPlausible(*tv));
assertx(isStringType(type(tv)));
return reinterpret_cast<const String&>(val(tv).pstr);
}
}
namespace folly {
template<> class FormatValue<HPHP::String> {
public:
explicit FormatValue(const HPHP::String& str) : m_val(str) {}
template<typename Callback>
void format(FormatArg& arg, Callback& cb) const {
FormatValue<HPHP::StringData*>(m_val.get()).format(arg, cb);
}
private:
const HPHP::String& m_val;
};
template<> class FormatValue<HPHP::StaticString> {
public:
explicit FormatValue(const HPHP::StaticString& str) : m_val(str) {}
template<typename Callback>
void format(FormatArg& arg, Callback& cb) const {
FormatValue<HPHP::StringData*>(m_val.get()).format(arg, cb);
}
private:
const HPHP::StaticString& m_val;
};
}