-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move hexdec & dechex builtins to runtime-common (#1181)
- Loading branch information
Showing
11 changed files
with
83 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2020 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#include "runtime-common/stdlib/math/math-functions.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "runtime-common/core/runtime-core.h" | ||
#include "runtime-common/stdlib/string/string-context.h" | ||
#include "runtime-common/stdlib/string/string-functions.h" | ||
|
||
string f$dechex(int64_t number) noexcept { | ||
auto v = static_cast<uint64_t>(number); | ||
|
||
char s[17]; | ||
int i = 16; | ||
|
||
const auto &lhex_digits{StringLibConstants::get().lhex_digits}; | ||
do { | ||
s[--i] = lhex_digits[v & 15]; | ||
v >>= 4; | ||
} while (v > 0); | ||
|
||
return {s + i, static_cast<string::size_type>(16 - i)}; | ||
} | ||
|
||
int64_t f$hexdec(const string &number) noexcept { | ||
uint64_t v = 0; | ||
bool bad_str_param = number.empty(); | ||
bool overflow = false; | ||
for (string::size_type i = 0; i < number.size(); i++) { | ||
const uint8_t d = hex_to_int(number[i]); | ||
if (unlikely(d == 16)) { | ||
bad_str_param = true; | ||
} else { | ||
v = math_functions_impl_::mult_and_add<16>(v, d, overflow); | ||
} | ||
} | ||
|
||
if (unlikely(bad_str_param)) { | ||
php_warning("Wrong parameter \"%s\" in function hexdec", number.c_str()); | ||
} | ||
if (unlikely(overflow)) { | ||
php_warning("Integer overflow on converting '%s' in function hexdec, " | ||
"the result will be different from PHP", | ||
number.c_str()); | ||
} | ||
return v; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
prepend(STDLIB_ARRAY stdlib/array/ array-functions.cpp) | ||
prepend(STDLIB_MATH stdlib/math/ math-functions.cpp) | ||
prepend(STDLIB_SERIALIZATION stdlib/serialization/ json-functions.cpp | ||
json-writer.cpp serialize-functions.cpp) | ||
prepend(STDLIB_STRING stdlib/string/ mbstring-functions.cpp | ||
string-functions.cpp) | ||
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp) | ||
|
||
set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_SERIALIZATION} ${STDLIB_STRING} | ||
${STDLIB_SERVER}) | ||
set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_MATH} ${STDLIB_SERIALIZATION} | ||
${STDLIB_STRING} ${STDLIB_SERVER}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@ok k2_skip | ||
@ok | ||
<?php | ||
|
||
function filesBinFromBase64($str) { // for storage-engine: secret packing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@ok k2_skip | ||
@ok | ||
<?php | ||
function inc_xor_str($a, $b) { | ||
$len = strlen($a); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@ok k2_skip | ||
@ok | ||
<?php | ||
|
||
function md5LastBits($x) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@ok k2_skip | ||
@ok | ||
<?php | ||
var_dump(hexdec("See")); | ||
var_dump(hexdec("ee")); | ||
|