Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: third_party/protobuf/php/src/Google/Protobuf/Internal/GPBUtil.php

Issue 2590803003: Revert "third_party/protobuf: Update to HEAD (83d681ee2c)" (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <?php
2
3 // Protocol Buffers - Google's data interchange format
4 // Copyright 2008 Google Inc. All rights reserved.
5 // https://developers.google.com/protocol-buffers/
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 namespace Google\Protobuf\Internal;
34
35 use Google\Protobuf\Internal\GPBType;
36 use Google\Protobuf\Internal\RepeatedField;
37
38 class GPBUtil
39 {
40 public function divideInt64ToInt32($value, &$high, &$low, $trim = false)
41 {
42 $isNeg = (bccomp($value, 0) < 0);
43 if ($isNeg) {
44 $value = bcsub(0, $value);
45 }
46 $high = (int) bcdiv(bcadd($value, 1), 4294967296);
47 $low = (int) bcmod($value, 4294967296);
48 if ($isNeg) {
49 $high = ~$high;
50 $low = ~$low;
51 $low++;
52 if (!$low) {
53 $high++;
54 }
55 }
56
57 if ($trim) {
58 $high = 0;
59 }
60 }
61
62
63 public static function checkString(&$var, $check_utf8)
64 {
65 if (is_array($var) || is_object($var)) {
66 trigger_error("Expect string.", E_USER_ERROR);
67 return;
68 }
69 if (!is_string($var)) {
70 $var = strval($var);
71 }
72 if ($check_utf8 && !preg_match('//u', $var)) {
73 trigger_error("Expect utf-8 encoding.", E_USER_ERROR);
74 return;
75 }
76 }
77
78 public static function checkEnum(&$var)
79 {
80 static::checkInt32($var);
81 }
82
83 public static function checkInt32(&$var)
84 {
85 if (is_numeric($var)) {
86 $var = intval($var);
87 } else {
88 trigger_error("Expect integer.", E_USER_ERROR);
89 }
90 }
91
92 public static function checkUint32(&$var)
93 {
94 if (is_numeric($var)) {
95 if (PHP_INT_SIZE === 8) {
96 $var = intval($var);
97 $var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
98 } else {
99 if (bccomp($var, 0x7FFFFFFF) > 0) {
100 $var = bcsub($var, "4294967296");
101 }
102 $var = (int) $var;
103 }
104 } else {
105 trigger_error("Expect integer.", E_USER_ERROR);
106 }
107 }
108
109 public static function checkInt64(&$var)
110 {
111 if (is_numeric($var)) {
112 if (PHP_INT_SIZE == 8) {
113 $var = intval($var);
114 } else {
115 $var = bcdiv($var, 1, 0);
116 }
117 } else {
118 trigger_error("Expect integer.", E_USER_ERROR);
119 }
120 }
121
122 public static function checkUint64(&$var)
123 {
124 if (is_numeric($var)) {
125 if (PHP_INT_SIZE == 8) {
126 $var = intval($var);
127 } else {
128 $var = bcdiv($var, 1, 0);
129 }
130 } else {
131 trigger_error("Expect integer.", E_USER_ERROR);
132 }
133 }
134
135 public static function checkFloat(&$var)
136 {
137 if (is_float($var) || is_numeric($var)) {
138 $var = floatval($var);
139 } else {
140 trigger_error("Expect float.", E_USER_ERROR);
141 }
142 }
143
144 public static function checkDouble(&$var)
145 {
146 if (is_float($var) || is_numeric($var)) {
147 $var = floatval($var);
148 } else {
149 trigger_error("Expect float.", E_USER_ERROR);
150 }
151 }
152
153 public static function checkBool(&$var)
154 {
155 if (is_array($var) || is_object($var)) {
156 trigger_error("Expect boolean.", E_USER_ERROR);
157 return;
158 }
159 $var = boolval($var);
160 }
161
162 public static function checkMessage(&$var, $klass)
163 {
164 if (!$var instanceof $klass && !is_null($var)) {
165 trigger_error("Expect message.", E_USER_ERROR);
166 }
167 }
168
169 public static function checkRepeatedField(&$var, $type, $klass = null)
170 {
171 if (!$var instanceof RepeatedField) {
172 trigger_error("Expect repeated field.", E_USER_ERROR);
173 }
174 if ($var->getType() != $type) {
175 trigger_error(
176 "Expect repeated field of different type.",
177 E_USER_ERROR);
178 }
179 if ($var->getType() === GPBType::MESSAGE &&
180 $var->getClass() !== $klass) {
181 trigger_error(
182 "Expect repeated field of different message.",
183 E_USER_ERROR);
184 }
185 }
186
187 public static function Int64($value)
188 {
189 return new Int64($value);
190 }
191
192 public static function Uint64($value)
193 {
194 return new Uint64($value);
195 }
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698