Skip to content

Commit 982308e

Browse files
author
子旭 苏
committed
php8 compatible
1 parent 332214e commit 982308e

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

src/Sort.php

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
11
<?php
2-
function _qsort(\SplFixedArray $arr, int $l, int $r, callable $compare) {
2+
function _qsort(SplFixedArray $arr, int $l, int $r, callable $compare) {
33
$i = $l;
44
$j = $r;
55
$x = $arr[(int)(($i + $j) / 2)];
6-
$t = NULL;
6+
$t = null;
77
do {
8-
while ( $compare($arr[$i], $x) )
9-
++ $i;
10-
while ( $compare($x, $arr[$j]) )
11-
-- $j;
8+
while ($compare($arr[$i], $x))
9+
++$i;
10+
while ($compare($x, $arr[$j]))
11+
--$j;
1212
if ($i <= $j) {
1313
$t = $arr[$i];
1414
$arr[$i] = $arr[$j];
1515
$arr[$j] = $t;
16-
++ $i;
17-
-- $j;
16+
++$i;
17+
--$j;
1818
}
19-
} while ( $i <= $j );
19+
} while ($i <= $j);
2020
unset($t);
2121
unset($x);
22-
if ($i < $r)
22+
if ($i < $r) {
2323
_qsort($arr, $i, $r, $compare);
24-
if ($l < $j)
24+
}
25+
if ($l < $j) {
2526
_qsort($arr, $l, $j, $compare);
27+
}
2628
}
2729
;
2830
/**
2931
* 快排,默认从小到达排序。注意:会去掉数组的键值。能用sort等函数的话就不要调用本函数
30-
*
31-
* @param array|SplFixedArray $arr
32-
* @param function $compare_func
33-
* @param bool $need_spl_fixed_array
32+
*
33+
* @param array|SplFixedArray $arr
34+
* @param callable $compare_func
35+
* @param bool $need_spl_fixed_array
3436
* @return number length of $arr
3537
*/
3638
function qsort(&$arr, ?callable $compare_func = null, bool $need_spl_fixed_array = false): int {
37-
if (is_array($arr))
39+
if (is_array($arr)) {
3840
$array = \SplFixedArray::fromArray($arr, false);
39-
else {
41+
} else {
4042
$array = $arr;
4143
$need_spl_fixed_array = true;
4244
}
4345
$length = $array->getSize();
4446
if ($length == 0) {
45-
if ($need_spl_fixed_array)
47+
if ($need_spl_fixed_array) {
4648
$arr = $array;
47-
else
49+
} else {
4850
$arr = [];
51+
}
4952
return 0;
5053
}
51-
if (! isset($compare_func))
54+
if (! isset($compare_func)) {
5255
$compare_func = function ($a, $b) {
5356
return $a < $b;
5457
};
58+
}
5559
_qsort($array, 0, $length - 1, $compare_func);
56-
if ($need_spl_fixed_array)
60+
if ($need_spl_fixed_array) {
5761
$arr = $array;
58-
else
62+
} else {
5963
$arr = $array->toArray();
64+
}
6065
return $length;
6166
}

src/Time.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ function getToday(): int {
1414
function getTomorrow(): int {
1515
return getToday() + 86400;
1616
}
17-
function getDaysOfAMonth($year, $month): int {
18-
if ($month == 2)
19-
return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) ? 29 : 28;
20-
if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
17+
function getDaysOfAMonth(int $year, int $month): int {
18+
if ($month === 2) {
19+
return (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) ? 29 : 28;
20+
}
21+
if ($month === 4 || $month === 6 || $month === 9 || $month === 11) {
2122
return 30;
23+
}
2224
return 31;
2325
}
2426
/**
@@ -29,17 +31,19 @@ function getLastMonth(): int {
2931
$year,
3032
$month
3133
] = explode('-', date('Y-n', now()));
32-
if ($month > 1)
34+
if ($month > 1) {
3335
return mktime(0, 0, 0, $month - 1, 1, $year);
36+
}
3437
return mktime(0, 0, 0, 12, 1, $year - 1);
3538
}
3639
function getEndOfThisMonth(): int {
3740
[
3841
$year,
3942
$month
4043
] = explode('-', date('Y-n', now()));
41-
if ($month == 12)
44+
if ($month == 12) {
4245
return mktime(0, 0, 0, 1, 1, $year + 1) - 1;
43-
else
46+
} else {
4447
return mktime(0, 0, 0, $month + 1, 1, $year) - 1;
48+
}
4549
}

src/XString.php

+24-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function GenerateRandomString(int $length = 16, string $type = 'lower&num'): str
1616
$strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
1717
}
1818
$max = strlen($strPol) - 1;
19-
for($i = 0; $i < $length; $i ++)
19+
for ($i = 0; $i < $length; $i++)
2020
$s .= $strPol[mt_rand(0, $max)];
2121
return $s;
2222
}
@@ -27,11 +27,11 @@ function strRealLength(string $str): int {
2727
$json = json_encode($str);
2828
$json = substr($json, 1, strlen($json) - 2);
2929
$ret = 0;
30-
for($i = 0, $l = strlen($json); $i < $l; ++ $i) {
31-
if ($json{$i} != '\\')
32-
++ $ret;
33-
else {
34-
if ($json{$i + 1} == 'u' && ($json{$i + 2} == 'd' || $json{$i + 2} == 'e')) {
30+
for ($i = 0, $l = strlen($json); $i < $l; ++$i) {
31+
if ($json[$i] != '\\') {
32+
++$ret;
33+
} else {
34+
if ($json[$i + 1] == 'u' && ($json[$i + 2] == 'd' || $json[$i + 2] == 'e')) {
3535
$offset_to_add = 11;
3636
$ret += 2;
3737
} else {
@@ -43,18 +43,19 @@ function strRealLength(string $str): int {
4343
}
4444
return $ret;
4545
}
46-
function strRealSub(string $str, int $start, int $length = NULL, ?callable $if_sub_call_back = NULL): string {
46+
function strRealSub(string $str, int $start, int $length = null, ?callable $if_sub_call_back = null): string {
4747
$json = json_encode($str);
4848
$json = substr($json, 1, strlen($json) - 2);
4949
$l = strlen($json);
50-
if ($l == 0)
50+
if ($l == 0) {
5151
return '';
52+
}
5253
$ret = [];
53-
for($i = 0; $i < $l; ++ $i) {
54-
if ($json{$i} != '\\')
55-
$ret[] = $json{$i};
56-
else {
57-
if ($json{$i + 1} == 'u' && ($json{$i + 2} == 'd' || $json{$i + 2} == 'e')) {
54+
for ($i = 0; $i < $l; ++$i) {
55+
if ($json[$i] != '\\') {
56+
$ret[] = $json[$i];
57+
} else {
58+
if ($json[$i + 1] == 'u' && ($json[$i + 2] == 'd' || $json[$i + 2] == 'e')) {
5859
$offset_to_add = 11;
5960
$s = '"' . substr($json, $i, 12) . '"';
6061
} else {
@@ -69,22 +70,23 @@ function strRealSub(string $str, int $start, int $length = NULL, ?callable $if_s
6970
}
7071
$retstr = implode('', array_slice($ret, $start, $length));
7172
if (isset($if_sub_call_back)) {
72-
if ($start > 0)
73-
$if_sub_call_back($retstr, FALSE);
74-
elseif ($start + $length < count($ret))
75-
$if_sub_call_back($retstr, TRUE);
73+
if ($start > 0) {
74+
$if_sub_call_back($retstr, false);
75+
} elseif ($start + $length < count($ret)) {
76+
$if_sub_call_back($retstr, true);
77+
}
7678
}
7779
return $retstr;
7880
}
7981
function removeEmoji(string $str): string {
8082
$json = json_encode($str);
8183
$json = substr($json, 1, strlen($json) - 2);
8284
$newstr = '';
83-
for($i = 0, $l = strlen($json); $i < $l; ++ $i) {
84-
if ($json{$i} != '\\')
85-
$newstr .= $json{$i};
86-
else {
87-
if ($json{$i + 1} == 'u' && ($json{$i + 2} == 'd' || $json{$i + 2} == 'e')) {
85+
for ($i = 0, $l = strlen($json); $i < $l; ++$i) {
86+
if ($json[$i] != '\\') {
87+
$newstr .= $json[$i];
88+
} else {
89+
if ($json[$i + 1] == 'u' && ($json[$i + 2] == 'd' || $json[$i + 2] == 'e')) {
8890
$offset_to_add = 11;
8991
$newstr .= '?';
9092
} else {

0 commit comments

Comments
 (0)