Spiral

My friend sent me this:

a bunch of hardcoded print statement which spiral a counter

It pretty much forced me to write spiral.php and spiral.sh to generate this:

print('1')

print('7 8 9')
print('6 1 2')
print('5 4 3')

print('21 22 23 24 25')
print('20  7  8  9 10')
print('19  6  1  2 11')
print('18  5  4  3 12')
print('17 16 15 14 13')

print('43 44 45 46 47 48 49')
print('42 21 22 23 24 25 26')
print('41 20  7  8  9 10 27')
print('40 19  6  1  2 11 28')
print('39 18  5  4  3 12 29')
print('38 17 16 15 14 13 30')
print('37 36 35 34 33 32 31')

print('73 74 75 76 77 78 79 80 81')
print('72 43 44 45 46 47 48 49 50')
print('71 42 21 22 23 24 25 26 51')
print('70 41 20  7  8  9 10 27 52')
print('69 40 19  6  1  2 11 28 53')
print('68 39 18  5  4  3 12 29 54')
print('67 38 17 16 15 14 13 30 55')
print('66 37 36 35 34 33 32 31 56')
print('65 64 63 62 61 60 59 58 57')

print('111 112 113 114 115 116 117 118 119 120 121')
print('110  73  74  75  76  77  78  79  80  81  82')
print('109  72  43  44  45  46  47  48  49  50  83')
print('108  71  42  21  22  23  24  25  26  51  84')
print('107  70  41  20   7   8   9  10  27  52  85')
print('106  69  40  19   6   1   2  11  28  53  86')
print('105  68  39  18   5   4   3  12  29  54  87')
print('104  67  38  17  16  15  14  13  30  55  88')
print('103  66  37  36  35  34  33  32  31  56  89')
print('102  65  64  63  62  61  60  59  58  57  90')
print('101 100  99  98  97  96  95  94  93  92  91')

print('157 158 159 160 161 162 163 164 165 166 167 168 169')
print('156 111 112 113 114 115 116 117 118 119 120 121 122')
print('155 110  73  74  75  76  77  78  79  80  81  82 123')
print('154 109  72  43  44  45  46  47  48  49  50  83 124')
print('153 108  71  42  21  22  23  24  25  26  51  84 125')
print('152 107  70  41  20   7   8   9  10  27  52  85 126')
print('151 106  69  40  19   6   1   2  11  28  53  86 127')
print('150 105  68  39  18   5   4   3  12  29  54  87 128')
print('149 104  67  38  17  16  15  14  13  30  55  88 129')
print('148 103  66  37  36  35  34  33  32  31  56  89 130')
print('147 102  65  64  63  62  61  60  59  58  57  90 131')
print('146 101 100  99  98  97  96  95  94  93  92  91 132')
print('145 144 143 142 141 140 139 138 137 136 135 134 133')

print('211 212 213 214 215 216 217 218 219 220 221 222 223 224 225')
print('210 157 158 159 160 161 162 163 164 165 166 167 168 169 170')
print('209 156 111 112 113 114 115 116 117 118 119 120 121 122 171')
print('208 155 110  73  74  75  76  77  78  79  80  81  82 123 172')
print('207 154 109  72  43  44  45  46  47  48  49  50  83 124 173')
print('206 153 108  71  42  21  22  23  24  25  26  51  84 125 174')
print('205 152 107  70  41  20   7   8   9  10  27  52  85 126 175')
print('204 151 106  69  40  19   6   1   2  11  28  53  86 127 176')
print('203 150 105  68  39  18   5   4   3  12  29  54  87 128 177')
print('202 149 104  67  38  17  16  15  14  13  30  55  88 129 178')
print('201 148 103  66  37  36  35  34  33  32  31  56  89 130 179')
print('200 147 102  65  64  63  62  61  60  59  58  57  90 131 180')
print('199 146 101 100  99  98  97  96  95  94  93  92  91 132 181')
print('198 145 144 143 142 141 140 139 138 137 136 135 134 133 182')
print('197 196 195 194 193 192 191 190 189 188 187 186 185 184 183')

PHP shutdown handlers and exit codes

I was in bed trying to get to sleep but my brain wanted to know the answer to this question. So I was forced out of bed to write this experiment:

function main( $argv ) {

  register_shutdown_function( 'shutdown_1' );
  register_shutdown_function( 'shutdown_2' );

  exit( 0 );

}

function shutdown_1() {

  exit( 1 );

}

function shutdown_2() {

  exit( 2 );

}

main( $argv );

With this PHP code, what do you expect is the resultant error level?

The answer is ‘1’. After main() calls exit( 0 ) the shutdown function shutdown_1() is invoked. When shutdown_1() calls exit( 1 ) the process exists and shutdown_2() is never called.

I’m glad we cleared that up. Back to bed.

PHP preg_match regex fail

So this happened. Basically the first regex causes preg_match to fail when it tries to process 128KiB ASCII zeros… and 128KiB isn’t really that many zeros. Fortunately the second regex performs much better, PHP runs out of memory before preg_match chokes on that one.

-------------------
Sat Apr 01 13:17:17 [bash:5.1.16 jobs:0 error:0 time:243]
jj5@charm:/home/jj5/desktop/experiment
$ cat base64-regex.php 
<?php

test( '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/' );
test( '/^[a-zA-Z0-9\/+]{2,}={0,2}$/' );

function test( $regex ) {

  echo "testing: $regex\n";

  $n = 0;

  for ( ;; ) {

    $n++;

    echo "n: $n\n";

    $string = str_repeat( '0', pow( 2, $n ) );

    $base64 = base64_encode( $string );

    if ( preg_match( $regex, $base64 ) ) { continue; }

    echo "error at N = $n.\n";

    return;

  }
}

-------------------
Sat Apr 01 13:17:21 [bash:5.1.16 jobs:0 error:0 time:247]
jj5@charm:/home/jj5/desktop/experiment
$ php base64-regex.php 
testing: /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/
n: 1
n: 2
n: 3
n: 4
n: 5
n: 6
n: 7
n: 8
n: 9
n: 10
n: 11
n: 12
n: 13
n: 14
n: 15
n: 16
n: 17
error at N = 17.
testing: /^[a-zA-Z0-9\/+]{2,}={0,2}$/
n: 1
n: 2
n: 3
n: 4
n: 5
n: 6
n: 7
n: 8
n: 9
n: 10
n: 11
n: 12
n: 13
n: 14
n: 15
n: 16
n: 17
n: 18
n: 19
n: 20
n: 21
n: 22
n: 23
n: 24
n: 25
n: 26
n: 27
n: 28
n: 29
n: 30
n: 31
n: 32
n: 33
n: 34
n: 35
Killed
-------------------
Sat Apr 01 13:18:21 [bash:5.1.16 jobs:0 error:137 time:307]