Skip to content

Instantly share code, notes, and snippets.

View julp's full-sized avatar

julp julp

View GitHub Profile
@julp
julp / strlen.asm
Created March 29, 2016 20:14
x64 (N)ASM: some libc functions
; nasm -f elf64 strlen.asm -o strlen.o && gcc -c main.c -o main.o && gcc main.o strlen.o && ./a.out
bits 64
global asm_strlen
section .text
asm_strlen:
; save rdi
@julp
julp / test.phpt
Last active December 18, 2015 22:47
lazy_write and empty sessions
--TEST--
session.lazy_write triggers updateTimestamp callback when session remains empty
--INI--
session.lazy_write=1
--FILE--
<?php
class MySessionHandler implements SessionHandlerInterface#, SessionUpdateTimestampHandlerInterface
{
public function close() {
return TRUE;
@julp
julp / 0.js.erb
Last active August 29, 2015 14:26
Rails: export routes to Javascript
<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>
/* <generated by app/assets/javascripts/routes.coffee> */
var Route;
Route = (function() {
Route._routes = {};
function Route(name, path, pattern, segments, required, optionnal) {
this.name = name;
@julp
julp / number_spellout.php
Last active December 28, 2019 15:58
ICU dates relatives
<?php
function number_spellout($number, $ordinal = FALSE, $feminine = FALSE, $plural = FALSE, $locale = NULL) {
# see: http://saxonica.com/html/documentation/extensibility/config-extend/localizing/ICU-numbering-dates/ICU-numbering.html
static $map = array(
0b000 => '%spellout-cardinal-masculine',
0b001 => '%spellout-cardinal-masculine', // no plural
0b010 => '%spellout-cardinal-feminine',
0b011 => '%spellout-cardinal-feminine', // no plural
0b100 => '%spellout-ordinal-masculine',
0b101 => '%spellout-ordinal-masculine-plural',
@julp
julp / collator_replace_results.md
Last active December 24, 2015 04:19
Benchmarks between 2 implementations of Collator::replace and Regexp::replaceCallback

// #define UTF8_REPLACEMENT 1

Changes:

  1. convert replacement from UTF-8 to UTF-16 (before processing)
  2. copy (strdup) UTF-16 version of suject as result (before processing)
  3. convert back result from UTF-16 to UTF-8 (before returning)
  4. we directly work on UTF-16 code unit offset (internal for replacements)

$string = str_repeat('Hayır İyi', 1);

@julp
julp / cut_html.php
Last active December 22, 2015 03:59
[PHP] Truncate HTML text to a given length
<?php
var_dump(
html_cut('<b>12<a href="#">34<i>56</i></a></b><u>78</u>', 3),
// html_cut('<b>1&eacute;<a href="#">34<i>56</i></a></b><u>78</u>', 3),
html_cut('<b>1<![CDATA[é]]><a href="#">34<i>56</i></a></b><u>78</u>', 3),
NULL
);
function html_cut(/* UTF-8 */ $string, $length)
{
@julp
julp / routines.php
Last active December 16, 2015 09:29
Manage easily HTTP caching
<?php
function LastModified(Closure $when, Closure $noncached) {
$rmtime = $when();
if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
$lmtime = date_create($_SERVER['HTTP_IF_MODIFIED_SINCE']);
if (FALSE !== $lmtime && $lmtime >= $rmtime) {
header('HTTP/1.1 304 Not Modified', TRUE, 304);
exit;
}
}
@julp
julp / README.fr.md
Last active September 13, 2017 13:52
Fix bad encoded UTF-8 in MySQL

Warnings d'usage

Je décline toute responsabilité quant à l'usage fait de ces "codes".

Lisez attentivement cette description avant de faire quoi que ce soit.

Vous penserez bien évidemment, comme avant toute intervention sur une base de données à effectuer au préalable une sauvegarde (données, tables, procédures/triggers) de celle-ci (mysqldump ou éventuellement via phpMyAdmin - déconseillé avec des bases importantes, surtout sur un serveur distant).

S'il est question d'une base de données en production, mettez absolument les applications qui en dépendent hors ligne avant toute chose parce que s'il y a des insertions ou mises à jour dans le même laps de temps, ça risque de poser différents problèmes.

@julp
julp / array_rand_value.c
Created December 21, 2012 15:52
PHP function array_rand_value(array &input [, boolean unset, boolean reindex])
/* ext/standard/array.c */
/* {{{ proto mixed array_rand_value(array &input [, boolean unset, boolean reindex])
Return a random value in the array */
PHP_FUNCTION(array_rand_value)
{
zval *array, **entry;
zend_bool unset, reindex;
long i, randval;
int length;
@julp
julp / cut_on_nearest_word_at.php
Created October 1, 2012 15:54
Couper le texte sur le mot le plus proche pour un extrait (début) de texte limité à X graphèmes
<?php
function cut_on_nearest_word_at($string, $length = 150) {
$start = grapheme_substr($string, 0, $length);
$byte_offset = strlen($start);
$ubrk = IntlBreakIterator::createWordInstance(ini_get('intl.default_locale'));
$ubrk->setText($string);
$byte_offset = $ubrk->preceding($byte_offset);
return substr($string, 0, $byte_offset);
}