Skip to content

Instantly share code, notes, and snippets.

View fstamour's full-sized avatar

Francis St-Amour fstamour

View GitHub Profile
<!-- The "content" is the amount of time (in seconds) between the refresh(es?) -->
<meta http-equiv="refresh" content="1" >
@fstamour
fstamour / bench_sets.cpp
Created February 12, 2013 23:09
C++11!! Mini benchmark to compare std::set and std::unordered_set. You may need a bigger list of word for better results.
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <set>
#include <chrono>
using namespace std;
int main()
{
@fstamour
fstamour / basicUserInput.hpp
Last active December 13, 2015 20:48
Basic utility functions to validate user input.
#ifndef MP_BASIC_USER_INPUT
#define MP_BASIC_USER_INPUT
namespace mp {
template< typename T >
bool validateInput( std::istream& is, T& value ) {
std::istream::sentry isOk(is);
if( isOk ) {
is >> value;
@fstamour
fstamour / min-jquery-show-hide.htm
Created February 20, 2013 01:26
Code Jquery minimal pour avoir une div (ou n'importe-quel balise) que l'on peut afficher/cacher.
<!DOCTYPE html>
<html>
<head>
<title>
Dev
</title>
<script type="text/javascript" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
@fstamour
fstamour / fifo.sh
Created February 22, 2013 18:57
Derping with fifo on linux.
#on terminal 1
mkfifo fifo
while true; do read line < fifo; $line; done
#on terminal 2
cat > fifo
@fstamour
fstamour / eatSpaces.cpp
Last active December 16, 2015 11:28
Simple function to skip white spaces in a input stream.
#include <istream>
#include <cctype>
void eatSpaces( std::istream& in ) {
char c;
while( in.good() ) {
c = in.peek();
if( !isspace(c) )
break;
in.get();
@fstamour
fstamour / escapeChar.cpp
Last active December 16, 2015 11:28
A simple helper function to handle escapes in parsing, you may choose the kind of error handling you want (by return value or by exception) and it is really easily converted to C.
//#include <stdexcept>
/// @warning The parameter is also the return value.
/// @return true if the escape sequence was valid, false otherwise
bool escapeChar( char& c ) {
switch( c ) {
case 't':
c = '\t';
break;
case 'n':
@fstamour
fstamour / readIntOrNotFloat.cpp
Last active December 16, 2015 11:39
Function to read an integer OR a float from a std::istream.
///@return true if int, false if float
bool readIntOrNotFloat( std::istream& in, int& i, float& f ) {
in >> i;
if( in.good() ) {
char c = in.peek();
if( c == '.' ) {
in >> f;
f += i;
return false;
}
@fstamour
fstamour / getExtension.cpp
Created April 21, 2013 12:09
Mini function to get the extension from a filename.
std::string getExtension( const std::string& filename ) {
size_t pos = filename.find_last_of('.');
return pos == std::string::npos? "" : filename.substr(pos);
}
@fstamour
fstamour / atoi.c
Created April 24, 2013 01:12
Minimal C code to read an int from a stream. This doesn't comply with the standard atoi at all, but it's just for simplicity.
int i = 0;
//FILE* in = stdin;
char c = getc(in);
while( c >= '0' && c <= '9' ) {
//i = i*10 + (c - '0'); //Equivalent to the line below
i = (i<<3)+(i<<1)+c-'0';
c = getchar();
}