Skip to content

Instantly share code, notes, and snippets.

View GuilhermeRossato's full-sized avatar
💼
“What I cannot build, I do not understand” - Richard Feynman

Guilherme Rossato GuilhermeRossato

💼
“What I cannot build, I do not understand” - Richard Feynman
View GitHub Profile
@GuilhermeRossato
GuilhermeRossato / minifyStyle.js
Last active September 21, 2017 19:53
Minify inner CSS string
/*
* Applicable to strings which would go inside a style property of an element, like so:
* element.setAttribute("style",minifyStyle(`margin : 0px 5px 4px 10px;`)); // This would remove 4 unnecessary spaces from the string
*/
const minifyStyle = (s, mode=0) => s.replace(/(\r\n|\t|\n|\r)/gm,"").split('').filter((char, index) => (((((mode == 1)&&(mode = 2))||true)&&(((char == ':')&&(mode = 1))||true)&&(char == ';')&&(mode = 0)||true)&&mode == 0)?(char !== ' '):((mode == 2)?((mode = 3)&&(char != ' ')):true)).join('');
minifyStyle(`margin-top: 5px;
margin: 5px 5px 5px 6px;
background-image: url(...) center center no-repeat;
@GuilhermeRossato
GuilhermeRossato / recursive_list_files.c
Last active December 6, 2017 03:16
C - Get all files and their last modified date recursively
// A c program to search all files recursively from the executable's path and retrieve its relative filename and last modified date.
// Similar to 'ls' command on linux
/* Sample output:
This directory tree has 3 files:
2017-12-05 23:55:39 ./src/recursive_file_list.c
2017-12-05 23:52:17 ./recursive_file_list.exe
2017-12-05 23:51:17 ./tools/windows/compile.bat
*/
@GuilhermeRossato
GuilhermeRossato / rec_search_file.php
Last active March 27, 2018 15:33
Search files recursively for a given extension in PHP
<?php
function search_recursively_for_extension($directory, &$array, $extension=".dat") {
//global $debug;
if (substr($directory, -1) === "/" || substr($directory, -1) === "\\") {
$directory = substr($directory, strlen($directory)-1);
}
$nodes = scandir($directory);
//$debug .= ("Reading Directory: ".$directory."\n").(($nodes === false || $nodes === NULL)?("false, ".(file_exists($directory)?"it does exist":"it doesn't even exist")."\n"):(var_export($nodes, true)."\n"));
if (is_array($nodes)) {
foreach ($nodes as $node) {
@GuilhermeRossato
GuilhermeRossato / parseDate.js
Last active January 21, 2018 23:54
JS - Guess what a "date" string format was and put that information in a object
/*
* parameter: a string with a possible date
* returns: false if it can't decode, otherwise an object like the following:
* {type, hour, minute, second, year, month, day}
* 'type' is the numeric index of the format that the string was guessed from.
*/
function parseDate(v) {
/*
* Every one of those options belo is a valid entry for the 'v' argument
* If some information is missing, it usually is replaced by current date info
@GuilhermeRossato
GuilhermeRossato / weekdayByDate.js
Last active August 2, 2019 19:59
Fast and compact way to get the weekday by date (year, month and day) in pure javascript
/**
* A pure function that retrieves the day of the week of a given date.
* Does not check if the date exists (i.e. 2019/02/31 yields a weekday, but doesn't exists).
*
* @param {number} y The year as a number with 4 digits
* @param {number} m The month as a number between 0 and 11 (january to december)
* @param {number} d The day as a number between 0 and 31
* @return {number} The number of the weekday, between 0 (monday) to 6 (sunday)
*/
function weekdayByDate(y, m, d) {
@GuilhermeRossato
GuilhermeRossato / debugbacktrace.php
Last active January 29, 2018 01:32
PHP retrieve better information from debug_backtrace without exausting memory
<?php
/*
* Output example at the bottom of this gist
*/
function get_formatted_debug_backtrace() {
$content = debug_backtrace();
$ret = [];
foreach($file_paths AS $file_path) {
$var = $file_path;
$ret[] = "File: ".$var['file'].": ".$var['line']."\n";
@GuilhermeRossato
GuilhermeRossato / CaptureScreenAndSave.h
Last active September 25, 2020 00:45
C++: Capture a sequence of screenshots and save with fixed interval (e.g. to make a GIF in sequence)
// Credits:
// Windows Capture and Save: Michael Ftsch
// Linux Capture and Save: None
//
#ifdef _WIN32
#include <windows.h>
inline int GetFilePointer(HANDLE FileHandle) {
return SetFilePointer(FileHandle, 0, 0, FILE_CURRENT);
@GuilhermeRossato
GuilhermeRossato / OrderedList.py
Created September 19, 2018 04:49
Self ordering ordered list (queue) implementation in python
import math
class OrderedList:
"""A self organizing list of (key, object) pairs ordering by the key"""
def __init__(self):
self.data = []
self.tests = 0
def __len__(self):
@GuilhermeRossato
GuilhermeRossato / ErrorHandler.php
Created October 18, 2018 01:23
Small PHP Error handler to format every error and show where the error happened
<?php
$error_was_already_triggered = false;
function process_error_handler($errno, $errstr, $errfile, $errline) {
global $error_was_already_triggered;
$error_is_marked_as_ignored = (error_reporting() == 0);
if ($error_is_marked_as_ignored || $error_was_already_triggered) {
return;
@GuilhermeRossato
GuilhermeRossato / test-disk-speed.c
Last active April 23, 2019 22:16
Simple C Disk Writing Benchmark for Windows
/*
* Description:
* C program for windowsto write to a file in 4096-byte blocks to a new file until it reaches 16777216 bytes (16MB)
*
* Supported OS:
* Windows and Linux
*
* Recomendations:
* Disable linux disk cache:
* sudo /sbin/sysctl -w vm.drop_caches=3