Skip to content

Instantly share code, notes, and snippets.

View chmike's full-sized avatar

Christophe Meessen chmike

View GitHub Profile
@chmike
chmike / PrintHex.h
Created December 12, 2012 09:15
Print binary data in haxadecimal
#ifndef PRINTHEX_H
#define PRINTHEX_H
#include <stdio.h>
// Display data in hexadecimal
inline void printHex( const void* data, size_t len, const char* margin )
{
if( len == 0 )
{
@chmike
chmike / IncrementalStats.h
Created December 12, 2012 09:19
Compute incrementally mean and std dev
#include <cmath>
//! One kibibyte unit size (see http://en.wikipedia.org/wiki/Kibibyte)
const size_t KiB = 1024;
//! One mebibyte unit size (see http://en.wikipedia.org/wiki/Mebibyte)
const size_t MiB = 1024*1024;
//! Convert timeval structure to a double
double toDbl( struct timeval& t )
@chmike
chmike / GetTimeAsDouble.h
Created December 12, 2012 09:27
Get the current time as a double float in second units. Handy to compute processing time interval.
#include <sys/time.h>
// Return the current time as a double float in second units
double getTimeAsDouble()
{
struct timeval t;
::gettimeofday( &t, NULL );
return double(t.tv_sec) + double(t.tv_usec)/1000000.0;
}
@chmike
chmike / rdns2str.go
Last active October 2, 2019 08:18
Relative Distinguish Name to string
var (
cnNameOid = asn1.ObjectIdentifier{2, 5, 4, 3}
emailOid = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
userIDOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1}
dcNameOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}
)
// RDNSToString returns the Relative Distinguish Name as a string.
func RDNSToString(rdns *pkix.RDNSequence) string {
var buf strings.Builder
@chmike
chmike / httpGet.go
Last active November 8, 2019 10:00
Correct HTTP GET response handling
res, err := http.GET(...)
if res != nil {
defer func() {
_, err = io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
}
if err != nil {
// . . .
}
@chmike
chmike / getlinefd.c
Last active February 26, 2020 15:56
getline using fd
// getlinefd reads a full line from fd into *lineptr. Initialize *lineptr to NULL and *n to 0.
// getlinefd will store the line into *lineptr which is is a buffer that will grow as needed.
// The buffer size is stored in *n. The line ends '\0' and with a '\n'when present.
// getlinefd returns the number of characters read, or -1 in case of error or if the end of file is reached.
ssize_t getlinefd(char **lineptr, size_t *n, int fd) {
static char buf[10];
static size_t len = 0;
const size_t blen = sizeof(buf);
if (lineptr == NULL || n == NULL) {

Problems with Go's shorthand declaration :=

The shadow variable trap

The problem

It is best illustrated with the following Go code snippet.

var x int
package main
import (
"fmt"
"image"
"image/color"
"os"
"gioui.org/app"
"gioui.org/font/gofont"
package main
import (
"image/color"
"log"
"os"
"strings"
"gioui.org/app"
"gioui.org/f32"
@chmike
chmike / checkDomain.go
Last active December 1, 2023 19:21
Check domain name validity in Go
// Please use the package https://github.com/chmike/domain as is it maintained up to date with tests.
// checkDomain returns an error if the domain name is not valid.
// See https://tools.ietf.org/html/rfc1034#section-3.5 and
// https://tools.ietf.org/html/rfc1123#section-2.
func checkDomain(name string) error {
switch {
case len(name) == 0:
return nil // an empty domain name will result in a cookie without a domain restriction
case len(name) > 255: