Skip to content

Instantly share code, notes, and snippets.

@davidmz
davidmz / bcast.go
Created April 12, 2014 10:39
Простой броадкастер на Go
package bcast
type Broadcaster interface {
// Неблокирующая отправка сообщения
Send(interface{})
// Блокирующее получение одного сообщения
Fetch() interface{}
}
type bcaster struct {
@davidmz
davidmz / gist:f5adb52d186caf272062
Created June 1, 2015 19:40
PCRE для поиска URL в строке
\b
(?:
(?:(https?|ftp)://|www\.)(?:[a-zа-я0-9-]+\.)*[a-zа-я0-9]+
|
(?:[a-zа-я0-9][a-zа-я0-9-]*\.)+(?:ru|com|net|org|рф)(?![\w-])
)
[^<>\s]*
(?:
(?<![[:punct:]])
|
var findURLs = (function() {
var bracketBalance = function(text) {
var brackets = {
"(": 1,
")": -1,
"[": 100,
"]": -100,
"{": 10000,
"}": -10000
},
@davidmz
davidmz / bcrypt.php
Created June 9, 2012 20:01
PHP Bcrypt class
<?php
/**
* From http://stackoverflow.com/a/6337021
*/
class Bcrypt {
private $rounds;
public function __construct($rounds = 12) {
if (CRYPT_BLOWFISH != 1) {
throw new Exception("bcrypt not supported in this installation. See http://php.net/crypt");
@davidmz
davidmz / gist:2904842
Created June 10, 2012 10:24
Implementation of the PBKDF2 key derivation function as described in RFC 2898.
<?php
/**
* Implementation of the PBKDF2 key derivation function as described in
* RFC 2898.
*
* @param string $PRF Hash algorithm.
* @param string $P Password.
* @param string $S Salt.
* @param int $c Iteration count.
* @param mixed $dkLen Derived key length (in octets). If $dkLen is FALSE
@davidmz
davidmz / gist:2924310
Created June 13, 2012 14:10
Код символа → UTF-8
<?php
function codeToUtf8($code) {
$code = (int)$code;
if ($code < 0x80) return chr($code);
if ($code < 0x800) return chr(0xC0 + (($code >> 6) & 0x1F)) .
chr(0x80 + ($code & 0x3F));
if ($code < 0x10000) return chr(0xE0 + (($code >> 12) & 0x0F)) .
chr(0x80 + (($code >> 6) & 0x3F)) .
chr(0x80 + ($code & 0x3F));
if ($code < 0x200000) return chr(0xF0 + (($code >> 18) & 0x07)) .
@davidmz
davidmz / run-forever.sh
Created November 28, 2012 14:33
Скрипт авторестарта программы
#!/bin/sh
if [ $# -eq '0' ]
then
echo 'Empty command'
exit 1
fi
stop=0
trap 'stop=1' TERM QUIT INT
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@davidmz
davidmz / xcell.js
Created January 28, 2014 18:37
Простая FRP-библиотечка для JS, позволяющая писать код в Excell-стиле.
(function (undefined) {
"use strict";
var
isArray = Array.isArray || function (v) {
return Object.prototype.toString.call(v) === "[object Array]";
},
isFunction = function (v) {
return Object.prototype.toString.call(v) === "[object Function]"
},
@davidmz
davidmz / file_upload.php
Created March 13, 2016 19:52
Пример загрузки файла-картинки на PHP
<?php
$strTargetDir = "user_images/";
$fileUploadField = "file_name";
$max_image_width = 600;
$max_image_height = 600;
$max_image_size = 1 << 20; // 1 MiB
$error_msg = "";
// ....