3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gdr_encode($val) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $str = ''; while ($val > 0) { $str = $chrs[$val % $base] . $str; $val = (int)($val / $base); } return $str; } function gdr_decode($str) { // all characters that json encode without escaping $chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 !#$%&\'()*+,-.:;<=>?@[]^_`{|}~'; $base = strlen($chrs); $map = array(); for ($i=0; $i<$base; $i++) { $map[$chrs[$i]] = $i; } $val = 0; for ($i=0; $i<strlen($str); $i++) { $val *= $base; $val += (int)$map[$str[$i]]; } return $val; } function duration($val) { if ($val < 24) { return sprintf('%d', $val).'h'; } if ($val < 24*10) { return sprintf('%1.1f', $val/24).'d'; } if ($val < 168*10) { return sprintf('%1.1f', $val/168).'w'; } //if ($val < 730*10) { // return sprintf('%1.1f', $val/730).'m'; //} return sprintf('%1.1f', $val/8760).'y'; } function cmp($val) { return strlen(gdr_encode($val))+2-strlen($val); } function insert(&$vals, $n) { $min = intval('1'.str_repeat('0', $n-1)); $max = intval(str_repeat('9', $n)); $vals[] = $min; $vals[] = $max; $len = cmp($min); if ($len !== cmp($max)) { return; } while ($min < $max) { $mid = round(($min+$max)/2, 0)-1; $k = cmp($mid); echo "$min $mid $max $k\n"; if ($k == $len && $k < cmp($mid+1)) { $vals[] = $mid; $vals[] = $mid+1; return; } if ($k <= $len) { $min = $mid+1; } else { $max = $mid-1; } } } $vals = array(0); for ($i=1; $i<8; $i++) { insert($vals, $i); } $vals = array_unique($vals); sort($vals); //$vals = array( // 0, 1, 99, 100, 255, 256, 999, 1000, 9999, 10000, 65535, 65536, 99999, // 100000, 250000, 500000, 750000, 999999, 1000000, 9999999, //4294967295, 4294967296 //); echo sprintf("%7s %7s %7s %4s\n", 'num', 'str', 'time', 'cmp'); foreach ($vals as $val) { $enc = gdr_encode($val); $dec = gdr_decode($enc); if ($dec !== $val) { echo "ERROR: $val => $enc => $dec\n"; } else { echo sprintf("%7d %7s %7s %4d\n", $dec, "\"$enc\"", duration($val), strlen($enc)+2-strlen($val)); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 3
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 65
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 65
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 46
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 3
Branch analysis from position: 10
Branch analysis from position: 3
filename:       /in/OZM51
function name:  (null)
number of ops:  67
compiled vars:  !0 = $vals, !1 = $i, !2 = $val, !3 = $enc, !4 = $dec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   ASSIGN                                                   !0, <array>
   79     1        ASSIGN                                                   !1, 1
          2      > JMP                                                      ->8
   80     3    >   INIT_FCALL                                               'insert'
          4        SEND_REF                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0          
   79     7        PRE_INC                                                  !1
          8    >   IS_SMALLER                                               !1, 8
          9      > JMPNZ                                                    ~9, ->3
   82    10    >   INIT_FCALL                                               'array_unique'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $10     
         13        ASSIGN                                                   !0, $10
   83    14        INIT_FCALL                                               'sort'
         15        SEND_REF                                                 !0
         16        DO_ICALL                                                 
   89    17        INIT_FCALL                                               'sprintf'
         18        SEND_VAL                                                 '%257s+%257s+%257s+%254s%0A'
         19        SEND_VAL                                                 'num'
         20        SEND_VAL                                                 'str'
         21        SEND_VAL                                                 'time'
         22        SEND_VAL                                                 'cmp'
         23        DO_ICALL                                         $13     
         24        ECHO                                                     $13
   90    25      > FE_RESET_R                                       $14     !0, ->65
         26    > > FE_FETCH_R                                               $14, !2, ->65
   91    27    >   INIT_FCALL                                               'gdr_encode'
         28        SEND_VAR                                                 !2
         29        DO_FCALL                                      0  $15     
         30        ASSIGN                                                   !3, $15
   92    31        INIT_FCALL                                               'gdr_decode'
         32        SEND_VAR                                                 !3
         33        DO_FCALL                                      0  $17     
         34        ASSIGN                                                   !4, $17
   93    35        IS_NOT_IDENTICAL                                         !4, !2
         36      > JMPZ                                                     ~19, ->46
   94    37    >   ROPE_INIT                                     7  ~21     'ERROR%3A+'
         38        ROPE_ADD                                      1  ~21     ~21, !2
         39        ROPE_ADD                                      2  ~21     ~21, '+%3D%3E+'
         40        ROPE_ADD                                      3  ~21     ~21, !3
         41        ROPE_ADD                                      4  ~21     ~21, '+%3D%3E+'
         42        ROPE_ADD                                      5  ~21     ~21, !4
         43        ROPE_END                                      6  ~20     ~21, '%0A'
         44        ECHO                                                     ~20
         45      > JMP                                                      ->64
   96    46    >   INIT_FCALL                                               'sprintf'
         47        SEND_VAL                                                 '%257d+%257s+%257s+%254d%0A'
         48        SEND_VAR                                                 !4
         49        ROPE_INIT                                     3  ~26     '%22'
         50        ROPE_ADD                                      1  ~26     ~26, !3
         51        ROPE_END                                      2  ~25     ~26, '%22'
         52        SEND_VAL                                                 ~25
         53        INIT_FCALL                                               'duration'
         54        SEND_VAR                                                 !2
         55        DO_FCALL                                      0  $28     
         56        SEND_VAR                                                 $28
         57        STRLEN                                           ~29     !3
         58        ADD                                              ~30     ~29, 2
         59        STRLEN                                           ~31     !2
         60        SUB                                              ~32     ~30, ~31
         61        SEND_VAL                                                 ~32
         62        DO_ICALL                                         $33     
         63        ECHO                                                     $33
   90    64    > > JMP                                                      ->26
         65    >   FE_FREE                                                  $14
   98    66      > RETURN                                                   1

Function gdr_encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 6
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 6
Branch analysis from position: 15
Branch analysis from position: 6
filename:       /in/OZM51
function name:  gdr_encode
number of ops:  17
compiled vars:  !0 = $val, !1 = $chrs, !2 = $base, !3 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+%21%23%24%25%26%27%28%29%2A%2B%2C-.%3A%3B%3C%3D%3E%3F%40%5B%5D%5E_%60%7B%7C%7D%7E'
    6     2        STRLEN                                           ~5      !1
          3        ASSIGN                                                   !2, ~5
    7     4        ASSIGN                                                   !3, ''
    8     5      > JMP                                                      ->13
    9     6    >   MOD                                              ~8      !0, !2
          7        FETCH_DIM_R                                      ~9      !1, ~8
          8        CONCAT                                           ~10     ~9, !3
          9        ASSIGN                                                   !3, ~10
   10    10        DIV                                              ~12     !0, !2
         11        CAST                                          4  ~13     ~12
         12        ASSIGN                                                   !0, ~13
    8    13    >   IS_SMALLER                                               0, !0
         14      > JMPNZ                                                    ~15, ->6
   12    15    > > RETURN                                                   !3
   13    16*     > RETURN                                                   null

End of function gdr_encode

Function gdr_decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 16
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 16
Branch analysis from position: 25
Branch analysis from position: 16
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 7
Branch analysis from position: 13
Branch analysis from position: 7
filename:       /in/OZM51
function name:  gdr_decode
number of ops:  27
compiled vars:  !0 = $str, !1 = $chrs, !2 = $base, !3 = $map, !4 = $i, !5 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1        ASSIGN                                                   !1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+%21%23%24%25%26%27%28%29%2A%2B%2C-.%3A%3B%3C%3D%3E%3F%40%5B%5D%5E_%60%7B%7C%7D%7E'
   18     2        STRLEN                                           ~7      !1
          3        ASSIGN                                                   !2, ~7
   19     4        ASSIGN                                                   !3, <array>
   20     5        ASSIGN                                                   !4, 0
          6      > JMP                                                      ->11
   21     7    >   FETCH_DIM_R                                      ~11     !1, !4
          8        ASSIGN_DIM                                               !3, ~11
          9        OP_DATA                                                  !4
   20    10        PRE_INC                                                  !4
         11    >   IS_SMALLER                                               !4, !2
         12      > JMPNZ                                                    ~14, ->7
   23    13    >   ASSIGN                                                   !5, 0
   24    14        ASSIGN                                                   !4, 0
         15      > JMP                                                      ->22
   25    16    >   ASSIGN_OP                                     3          !5, !2
   26    17        FETCH_DIM_R                                      ~18     !0, !4
         18        FETCH_DIM_R                                      ~19     !3, ~18
         19        CAST                                          4  ~20     ~19
         20        ASSIGN_OP                                     1          !5, ~20
   24    21        PRE_INC                                                  !4
         22    >   STRLEN                                           ~23     !0
         23        IS_SMALLER                                               !4, ~23
         24      > JMPNZ                                                    ~24, ->16
   28    25    > > RETURN                                                   !5
   29    26*     > RETURN                                                   null

End of function gdr_decode

Function duration:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 27
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OZM51
function name:  duration
number of ops:  35
compiled vars:  !0 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        IS_SMALLER                                               !0, 24
          2      > JMPZ                                                     ~1, ->9
   33     3    >   INIT_FCALL                                               'sprintf'
          4        SEND_VAL                                                 '%25d'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        CONCAT                                           ~3      $2, 'h'
          8      > RETURN                                                   ~3
   35     9    >   IS_SMALLER                                               !0, 240
         10      > JMPZ                                                     ~4, ->18
   36    11    >   INIT_FCALL                                               'sprintf'
         12        SEND_VAL                                                 '%251.1f'
         13        DIV                                              ~5      !0, 24
         14        SEND_VAL                                                 ~5
         15        DO_ICALL                                         $6      
         16        CONCAT                                           ~7      $6, 'd'
         17      > RETURN                                                   ~7
   38    18    >   IS_SMALLER                                               !0, 1680
         19      > JMPZ                                                     ~8, ->27
   39    20    >   INIT_FCALL                                               'sprintf'
         21        SEND_VAL                                                 '%251.1f'
         22        DIV                                              ~9      !0, 168
         23        SEND_VAL                                                 ~9
         24        DO_ICALL                                         $10     
         25        CONCAT                                           ~11     $10, 'w'
         26      > RETURN                                                   ~11
   44    27    >   INIT_FCALL                                               'sprintf'
         28        SEND_VAL                                                 '%251.1f'
         29        DIV                                              ~12     !0, 8760
         30        SEND_VAL                                                 ~12
         31        DO_ICALL                                         $13     
         32        CONCAT                                           ~14     $13, 'y'
         33      > RETURN                                                   ~14
   45    34*     > RETURN                                                   null

End of function duration

Function cmp:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/OZM51
function name:  cmp
number of ops:  10
compiled vars:  !0 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   48     1        INIT_FCALL                                               'gdr_encode'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $1      
          4        STRLEN                                           ~2      $1
          5        ADD                                              ~3      ~2, 2
          6        STRLEN                                           ~4      !0
          7        SUB                                              ~5      ~3, ~4
          8      > RETURN                                                   ~5
   49     9*     > RETURN                                                   null

End of function cmp

Function insert:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 31
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 46) Position 1 = 54, Position 2 = 60
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 67
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 72
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
Branch analysis from position: 72
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 31
Branch analysis from position: 76
Branch analysis from position: 31
Branch analysis from position: 60
filename:       /in/OZM51
function name:  insert
number of ops:  77
compiled vars:  !0 = $vals, !1 = $n, !2 = $min, !3 = $max, !4 = $len, !5 = $mid, !6 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   52     2        INIT_FCALL                                               'str_repeat'
          3        SEND_VAL                                                 '0'
          4        SUB                                              ~7      !1, 1
          5        SEND_VAL                                                 ~7
          6        DO_ICALL                                         $8      
          7        CONCAT                                           ~9      '1', $8
          8        CAST                                          4  ~10     ~9
          9        ASSIGN                                                   !2, ~10
   53    10        INIT_FCALL                                               'str_repeat'
         11        SEND_VAL                                                 '9'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $12     
         14        CAST                                          4  ~13     $12
         15        ASSIGN                                                   !3, ~13
   54    16        ASSIGN_DIM                                               !0
         17        OP_DATA                                                  !2
   55    18        ASSIGN_DIM                                               !0
         19        OP_DATA                                                  !3
   56    20        INIT_FCALL                                               'cmp'
         21        SEND_VAR                                                 !2
         22        DO_FCALL                                      0  $17     
         23        ASSIGN                                                   !4, $17
   57    24        INIT_FCALL                                               'cmp'
         25        SEND_VAR                                                 !3
         26        DO_FCALL                                      0  $19     
         27        IS_NOT_IDENTICAL                                         !4, $19
         28      > JMPZ                                                     ~20, ->30
   58    29    > > RETURN                                                   null
   60    30    > > JMP                                                      ->74
   61    31    >   INIT_FCALL                                               'round'
         32        ADD                                              ~21     !2, !3
         33        DIV                                              ~22     ~21, 2
         34        SEND_VAL                                                 ~22
         35        SEND_VAL                                                 0
         36        DO_ICALL                                         $23     
         37        SUB                                              ~24     $23, 1
         38        ASSIGN                                                   !5, ~24
   62    39        INIT_FCALL                                               'cmp'
         40        SEND_VAR                                                 !5
         41        DO_FCALL                                      0  $26     
         42        ASSIGN                                                   !6, $26
   63    43        ROPE_INIT                                     8  ~29     !2
         44        ROPE_ADD                                      1  ~29     ~29, '+'
         45        ROPE_ADD                                      2  ~29     ~29, !5
         46        ROPE_ADD                                      3  ~29     ~29, '+'
         47        ROPE_ADD                                      4  ~29     ~29, !3
         48        ROPE_ADD                                      5  ~29     ~29, '+'
         49        ROPE_ADD                                      6  ~29     ~29, !6
         50        ROPE_END                                      7  ~28     ~29, '%0A'
         51        ECHO                                                     ~28
   64    52        IS_EQUAL                                         ~33     !6, !4
         53      > JMPZ_EX                                          ~33     ~33, ->60
         54    >   INIT_FCALL                                               'cmp'
         55        ADD                                              ~34     !5, 1
         56        SEND_VAL                                                 ~34
         57        DO_FCALL                                      0  $35     
         58        IS_SMALLER                                       ~36     !6, $35
         59        BOOL                                             ~33     ~36
         60    > > JMPZ                                                     ~33, ->67
   65    61    >   ASSIGN_DIM                                               !0
         62        OP_DATA                                                  !5
   66    63        ADD                                              ~39     !5, 1
         64        ASSIGN_DIM                                               !0
         65        OP_DATA                                                  ~39
   67    66      > RETURN                                                   null
   69    67    >   IS_SMALLER_OR_EQUAL                                      !6, !4
         68      > JMPZ                                                     ~40, ->72
   70    69    >   ADD                                              ~41     !5, 1
         70        ASSIGN                                                   !2, ~41
         71      > JMP                                                      ->74
   72    72    >   SUB                                              ~43     !5, 1
         73        ASSIGN                                                   !3, ~43
   60    74    >   IS_SMALLER                                               !2, !3
         75      > JMPNZ                                                    ~45, ->31
   75    76    > > RETURN                                                   null

End of function insert

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.21 ms | 1414 KiB | 32 Q