Skip to content

Instantly share code, notes, and snippets.

View jesseschalken's full-sized avatar

Jesse Schalken jesseschalken

View GitHub Profile
@jesseschalken
jesseschalken / Serialize.php
Last active December 11, 2015 09:08
Simple serialization format
<?php
function serialize_uint($x) {
for ($r = ''; $x > 0; $x = $x >> 8)
$r = chr($x & 0xFF) . $r;
return chr(strlen($r)) . $r;
}
function serialize_str($s) {
return serialize_uint(strlen($s)) . $s;
@jesseschalken
jesseschalken / perms.php
Last active August 29, 2015 14:10
A PHP class to generate Unix symbolic permission notation (eg "-rw-r--r--") from lstat()/fileperms()
<?php
class Perms {
/** @var int */
private $mode;
/** @param string $path */
function __construct($path) {
$this->mode = fileperms($path);
}
@jesseschalken
jesseschalken / format serialize.php
Last active January 12, 2022 15:37
Pretty print a PHP serialized value. Useful for finding the differences between two serialized values.
<?php
/*
MIT License
Copyright (c) 2021 Jesse Schalken
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@jesseschalken
jesseschalken / ref.php
Last active August 29, 2015 14:27
Handy functions for working with references in PHP
<?php
final class ref {
static function &mk($x) { return $x; }
static function get(&$x) { return $x; }
static function set(&$x, $y) { $x = $y; }
static function inc(&$x) { $x++; }
static function dec(&$x) { $x--; }
static function swap(&$x, &$y) {
@jesseschalken
jesseschalken / scalar_serializer.php
Last active August 29, 2015 14:27
Simple fast bidirectional mapping between scalars and strings
<?php
class ScalarSerializer {
/**
* @param string $s
* @return string|int|float|bool|null
*/
static function unserialize($s) {
$t = (string)substr($s, 0, 1);
$v = (string)substr($s, 1);
@jesseschalken
jesseschalken / JoinSplit.php
Last active August 29, 2015 14:27
Join a list of arbitrary strings together so they can be split apart again, ie split(join($x)) === $x for all $x
<?php
namespace JoinSplit;
/**
* @param string[] $a
* @return string
*/
function join(array $a) {
$r = '';
@jesseschalken
jesseschalken / simple-autoload.php
Last active March 3, 2016 12:49
This autoloader will search for Foo\Bar\Baz (for example) in ./Foo/Bar/Baz.php, ./Foo/Bar.php and ./Foo.php, in that order. This enables developers to place multiple classes and even multiple namespaces in one file when appropriate. Just place this file in your src/ directory and use it as the entry point for your project.
<?php
spl_autoload_register(function ($cls) {
for ($cls = explode('\\', $cls); $cls; array_pop($cls)) {
$sep = DIRECTORY_SEPARATOR;
$php = __DIR__ . $sep . join($sep, $cls) . '.php';
if (file_exists($php)) {
/** @noinspection PhpIncludeInspection */
require_once $php;
break;
@jesseschalken
jesseschalken / memory_limit.php
Last active September 11, 2015 01:14
Class to increase/decrease PHP's memory_limit by a given amount
<?php
class MemoryLimit {
private $num = 0;
function inc($num) {
if (!$num)
return;
$this->num += $num;
@jesseschalken
jesseschalken / codeStyleSettings.xml
Created September 24, 2015 09:17
PhpStorm code style settings
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<PHPCodeStyleSettings>
<option name="ALIGN_KEY_VALUE_PAIRS" value="true" />
<option name="ALIGN_PHPDOC_PARAM_NAMES" value="true" />
<option name="ALIGN_PHPDOC_COMMENTS" value="true" />
<option name="ALIGN_ASSIGNMENTS" value="true" />
@jesseschalken
jesseschalken / code_style.md
Last active April 24, 2024 17:27
Code style

My preferred code style is 2-space K&R. This is intended to provide a justification for this style.

Why K&R?

K&R style has the following properties:

  1. Provides symmetric size (in terms of screen space consumed) between the opening and closing syntax of a clode block.
  2. Forces no empty or meaningless lines, thereby avoiding artificial distance between related things that should be together.
  3. Consumes the minimum vertical space while keeping the opening and closing syntax of a block on separate lines from the content.