Skip to content

Instantly share code, notes, and snippets.

View johnbartholomew's full-sized avatar

John Bartholomew johnbartholomew

View GitHub Profile
@johnbartholomew
johnbartholomew / no-init-list.cpp
Created January 21, 2014 23:14
Testing nested initializer-lists without initializer-list constructors.
/* Compiles and works with recent GCC and Clang in C++11 mode.
* $ g++ --version
* g++ (GCC) 4.8.2 20131219 (prerelease)
*
* $ clang++ --version
* clang version 3.4 (tags/RELEASE_34/final)
*/
#include <vector>
#include <iostream>
@johnbartholomew
johnbartholomew / match-time.sh
Last active January 2, 2016 23:49
bad regex! no cookie!
#!/bin/sh
a5=aaaaa
a27="$a5$a5$a5$a5$a5"aa
aq5='a?a?a?a?a?'
aq27="$aq5$aq5$aq5$aq5$aq5"'a?a?'
pattern="$aq27$a27"
input="$a27"
#include "nfa.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
Nfa *nfa;
NfaBuilder builder;
nfa_builder_init(&builder);
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static const char MESSAGE[] = "Hello, world!\n";
int main(void) {
char fname[64];
int fd;

I'm not trying to precisely match the behaviour of an existing glob syntax unless that behaviour really seems like the most obvious/easiest-to-use syntax. However, I do want globs to feel familiar to use.

I'll say that '**' matches anything, and '*' matches anything except directory separators. Without thinking too hard, how do you expect the following globs to behave?

  1. *.c
  2. **.c
  3. **/*.c
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void check_access(const char *path) {
int ret = access(path, F_OK);
if (ret == -1) {
int e = errno;
printf("access '%s' failed: %s\n", path, strerror(e));

Some definitions:

  • A "safe" transformation is a transformation of a path A into a path B such that any file system operation on path A will produce the same behaviour if given path B.

  • "normalize" means remove redundant directory separators (including trailing separators) and replace non-canonical separators with canonical separators (on Windows, backslash is the canonical separator, but forward slashes are also recognised as separators. On POSIX, forward slash is the only separator character and is canonical). Normalization is trivially "safe"; it doesn't add or remove any path components,

@johnbartholomew
johnbartholomew / gist:6811245
Last active December 24, 2015 14:19
Creating statically linkable C "packages"
#define PACKAGE_PRIVATE __attribute__((__visibility__("hidden")))
#define PACKAGE_PUBLIC __attribute__((__visibility__("default")))
/* -------- a.c -------- */
static int the_thing = 42;
PACKAGE_PRIVATE int compute_thing(void) {
return the_thing;
}
// ---- text.vert.glsl
#version 150
uniform vec2 text_offset;
uniform vec2 rcp_screen_size;
in vec2 v_screen_pos;
in vec2 v_uv;
out vec2 f_uv;
@johnbartholomew
johnbartholomew / ClangWarnGlobals.cpp
Created May 28, 2013 18:24
A Clang plugin to generate warnings for non-const global variables.
// -Wglobals Clang plugin
//
// Based on example plugins and searching Clang's API documentation.
// BEWARE! This is my first ever attempt at a Clang plugin.
// Makefile
#if 0
warnglobals.so: WarnGlobals.cpp
clang++ -std=c++98 -shared -fPIC $$(llvm-config --cflags --libs support mc) -o "$@" $^ -L/usr/lib/llvm/ -lclang