Skip to content

Instantly share code, notes, and snippets.

@kennethchiu
kennethchiu / hex_dfa.c
Created June 18, 2020 15:56
DFA to recognize hex numbers
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
// This function returns the numerical value of a hex digit. It returns -1 if
// it is not a valid hex digit.
int ishexdigit(int ch) {
// Should already be in lower case. This is defensive programming check,
@kennethchiu
kennethchiu / bounds-check.cpp
Created April 9, 2016 18:39
Bounds checking macros and benchmark of bounds checking cost
#include <iostream>
#include <chrono>
// This macro will always bounds check, even if debug build.
#define UNQ_ALWAYS_CHKBND(a, i) \
chk_bnd(a, i, __FILE__, __LINE__, __func__)
// This macro will be compiled out if NDEBUG is defined.
#ifndef NDEBUG
#define UNQ_DBG_CHKBND(a, i) \