Skip to content

Instantly share code, notes, and snippets.

@detomon
detomon / strspn.js
Created March 28, 2014 10:49
JavaScript implementation of the PHP function `strspn`
String.prototype.strspn = function (chars, start, length) {
var end;
if (start == undefined)
start = 0;
else if (start < 0)
start += this.length;
if (length == undefined)
length = this.length;
@detomon
detomon / CGAffineTransform.swift
Last active January 10, 2024 15:26
Convenient operator overloadings for CGPoint, CGSize and CGRect
import Foundation
/**
* CGAffineTransform
*
* var a = CGAffineTransformMakeRotation(45.0 * M_PI / 180.0)
* var b = CGPointMake(30.0, 43.3)
*/
/**
@detomon
detomon / strnatcmp.c
Last active June 22, 2017 06:44
Compare strings using natural ordering by interpreting continuous digits as integers
#include <stdio.h>
#include <stdlib.h>
#include <Block.h>
/**
* Compare strings using natural ordering
*
* Interprets continuous digits as integers, e.g, the string "year 2015" will be
* greater than "year 308", although "3" has a higher character value than "2".
* [Trailing and pending whitespace is ignored.] Whitespace between tokens is
@detomon
detomon / bitfield.h
Last active August 29, 2015 14:19
A collection of macro functions to manipulate bitfields of arbitrary size
/**
* Bitfield function collection
*
* Can be used to define variables which require more bits than the biggest data
* type can hold. Bits are organized within multiple `slots` which is an array
* of the biggest integer type available.
*
* However, multiple bits cannot overlap two slots:
* bit_get_field (mybits, 62, 8) will only contain bits from the first slot
*
@detomon
detomon / intrinsic.md
Last active March 6, 2024 17:57
SSE Intrinsic Cheat Sheet (SSE3)

SSE Intrinsic Cheat Sheet (SSE3)

Load and Store

__m128 _mm_load_ps (float * a)
{
@detomon
detomon / endianess.cpp
Created May 18, 2016 09:48
Check the system endianness at compile time with C++
#include <cstdint>
#include <iostream>
struct Endian {
constexpr static bool is_big() {
union {
uint32_t i;
uint8_t c[4];
} check = {
.i = 0x01000000
@detomon
detomon / run-in-shell.c
Last active July 23, 2017 14:40
Compiling and running a C program in the shell by executing itself as shell script
#if 0
clang -Wall -O2 -o /tmp/`basename $0` $0 && /tmp/`basename $0` $@; exit
#endif
//
// sh run-in-shell.c `whoami`
//
#include <stdio.h>

Keybase proof

I hereby claim:

  • I am detomon on github.
  • I am detomon (https://keybase.io/detomon) on keybase.
  • I have a public key ASDSgRQCCkC9JCQ1gus0l45K4_e73TPja93u5Xypmsq6Uwo

To claim this, I am signing this object:

@detomon
detomon / swift-enum.c
Created September 9, 2017 17:13
Emulate Swift enum types in C
#include <stdio.h>
/**
* Emulate Swift enum type.
*/
typedef struct {
// define enum types
// (enums inside structs are not really private)
enum {
MyEnumInt,
#define VALUE_REF(S) (&((struct { typeof(S) s; }) { (S) }).s)
typedef struct {
int a;
int b;
} value;
int* i = VALUE_REF(12);
float* f = VALUE_REF(45.6f);
value* v = VALUE_REF(((value) { .a = 12, .b = 13 }));