Skip to content

Instantly share code, notes, and snippets.

Avatar

Pierre Jambet pjambet

View GitHub Profile
@pjambet
pjambet / kilo.rb
Last active Aug 12, 2022
A very basic text editor in ruby, copied from Kilo and adapted in Ruby
View kilo.rb
require 'termios'
s = [0, 0, 0, 0].pack("S_S_S_S_")
STDOUT.ioctl(Termios::TIOCGWINSZ, s)
HEIGHT, WIDTH, _, _ = s.unpack("S_S_S_S_")
# Raw mode
current = Termios.tcgetattr(STDIN)
t = current.dup
@pjambet
pjambet / repro.rb
Last active Jun 30, 2022
Active Record association scopes causing issues with `association.create` on has_many
View repro.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: "7-0-stable"
View stateful.go
package main
import (
"fmt"
"math/rand"
// "sync/atomic"
"bufio"
"net"
// "strconv"
View bench-large.rb
require 'benchmark/ips'
require 'set'
def bench(size)
Benchmark.ips do |x|
set = Set.new
array = []
hash = {}
size.times do |i|
View bench-large.js
var Benchmark = require('benchmark');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
var set = new Set();
var array = [];
View overflow.c
#include <stdio.h>
#include <stdint.h>
int main() {
long long l = -2;
printf("%lld\n", l);
printf("%llu\n", (unsigned long long) l);
printf("%llu\n", (uint64_t) l);
return 0;
}
@pjambet
pjambet / mermaid
Created Dec 8, 2020
A sequence diagram
View mermaid
sequenceDiagram
participant Frontend
participant Backend
Frontend->>+Frontend: Persist intent record
Frontend->>+Backend: Create resource request
Backend-->>-Frontend: Newly created resource
Note right of Backend: Resource must include a unique identifier
Frontend->>+Frontend: Update intent record with the unique identifier
@pjambet
pjambet / select.rs
Created Nov 22, 2020
select/pselect in Rust
View select.rs
extern crate libc;
use std::{io,mem,ptr,time};
use std::os::unix::io::RawFd;
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
// use std::io::Read;
pub struct FdSet(libc::fd_set);
@pjambet
pjambet / string_cmd.md
Last active Nov 17, 2020
String commands, without bit stuff
View string_cmd.md
  • APPEND
  • DECR
  • DECRBY
  • GET
  • GETRANGE
  • GETSET
  • INCR
  • INCRBY
  • INCRBYFLOAT
  • MGET
@pjambet
pjambet / popcount.c
Created Nov 12, 2020
Playing with the C code from Redis in bitops.c
View popcount.c
#include <stdio.h>
#include <inttypes.h>
size_t redisPopcount(void *s, long count) {
size_t bits = 0;
unsigned char *p = s;
uint32_t *p4;
static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
/* Count initial bytes not aligned to 32 bit. */