2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/runtime/base/type-conversions.h" // toInt64(double)
21 //////////////////////////////////////////////////////////////////////
23 inline bool cellToBool(Cell cell
) {
24 assert(cellIsPlausible(cell
));
26 switch (cell
.m_type
) {
28 case KindOfNull
: return false;
29 case KindOfInt64
: return cell
.m_data
.num
!= 0;
30 case KindOfBoolean
: return cell
.m_data
.num
;
31 case KindOfDouble
: return cell
.m_data
.dbl
!= 0;
32 case KindOfStaticString
:
33 case KindOfString
: return cell
.m_data
.pstr
->toBoolean();
34 case KindOfArray
: return !cell
.m_data
.parr
->empty();
35 case KindOfObject
: return cell
.m_data
.pobj
->o_toBoolean();
36 case KindOfResource
: return cell
.m_data
.pres
->o_toBoolean();
42 inline int64_t cellToInt(Cell cell
) {
43 assert(cellIsPlausible(cell
));
45 switch (cell
.m_type
) {
46 case KindOfInt64
: return cell
.m_data
.num
;
47 case KindOfDouble
: return toInt64(cell
.m_data
.dbl
);
49 case KindOfStaticString
: return cell
.m_data
.pstr
->toInt64(10);
50 case KindOfArray
: return cell
.m_data
.parr
->empty() ? 0 : 1;
51 case KindOfObject
: return cell
.m_data
.pobj
->o_toInt64();
52 case KindOfResource
: return cell
.m_data
.pres
->o_toInt64();
53 case KindOfBoolean
: return cell
.m_data
.num
;
55 case KindOfNull
: return 0;
61 inline double cellToDouble(Cell cell
) {
64 tvCastToDoubleInPlace(&tmp
);
65 return tmp
.m_data
.dbl
;
68 inline TypedNum
stringToNumeric(const StringData
* sd
) {
71 auto const dt
= sd
->isNumericWithVal(ival
, dval
, true /* allow_errors */);
72 return dt
== KindOfInt64
? make_tv
<KindOfInt64
>(ival
) :
73 dt
== KindOfDouble
? make_tv
<KindOfDouble
>(dval
) :
74 make_tv
<KindOfInt64
>(0);
77 //////////////////////////////////////////////////////////////////////