Skip to content

Instantly share code, notes, and snippets.

@gruber
gruber / Liberal Regex Pattern for All URLs
Last active May 29, 2024 00:03
Liberal, Accurate Regex Pattern for Matching All URLs
The regex patterns in this gist are intended to match any URLs,
including "mailto:foo@example.com", "x-whatever://foo", etc. For a
pattern that attempts only to match web URLs (http, https), see:
https://gist.github.com/gruber/8891611
# Single-line version of pattern:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
@aruslan
aruslan / revndup.c
Created April 20, 2012 00:34
revndup
#include <stdio.h>
void shodan_ndup(char* s) {
char* p = s;
for(; *s; ++s)
if (*s != s[1])
*p++ = *s;
*p = 0;
}
@preshing
preshing / sort1mb.cpp
Created October 25, 2012 11:28
Sort one million 8-digit numbers in 1MB RAM
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned int u32;
typedef unsigned long long u64;
//-------------------------------------------------------------------------
// WorkArea
//-------------------------------------------------------------------------
@dobrokot
dobrokot / cpp_iostream_fail_open_file.cpp
Created August 17, 2013 20:01
How to handle non-existing opened file with ifstream ?
#include <iostream>
#include <fstream>
int main() {
std::ifstream empty("empty.txt");
std::ifstream no_file("no_file.txt");
std::cout << "bool: " << (empty?"e 1":"e 0") << ' ' << (no_file?"n 1":"n 0") << '\n';
std::cout << "rdstate:" << (empty.rdstate()?"e 1":"e 0") << ' ' << (no_file.rdstate()?"n 1":"n 0") << '\n';
using System;
using N; // This using directive is unused, but cannot be simply removed
static class C
{
static void Ex(this string x) { }
static void Foo(Action<string> x, int y) { }
static void Foo(Action<string> x, string y) { }
static void Foo(Action<string> x, object y) { }
static void Foo(Action<int> x, int y) { }
@gruber
gruber / Liberal Regex Pattern for Web URLs
Last active April 22, 2024 19:02
Liberal, Accurate Regex Pattern for Matching Web URLs
The regex patterns in this gist are intended only to match web URLs -- http,
https, and naked domains like "example.com". For a pattern that attempts to
match all URLs, regardless of protocol, see: https://gist.github.com/gruber/249502
# Single-line version:
(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|s
@aruslan
aruslan / calc60.cc
Created February 10, 2015 04:56
calc60.cc
#include <cctype> // isspace
#include <cstdlib> // strtod, strtol
#include <functional> // function
#include <iomanip> // setw
#include <iostream> // cerr
template<typename T>
T strtot(const char* start, char** end) { return std::strtol(start, end, 0); }
template<>
double strtot(const char* start, char** end) { return std::strtod(start, end); }
@aruslan
aruslan / ziptest.cc
Created June 19, 2018 02:42
an example of zip for N.K.
#include <iostream>
#include <iterator>
#include <tuple>
template <typename C1, typename C2>
struct zip_container {
C1& c1; C2& c2;
typedef std::tuple<
decltype(std::begin(c1)), decltype(std::begin(c2))
> tuple;
@Nekotekina
Nekotekina / bound_class_templates.md
Last active January 17, 2019 00:03
Bound Class Templates Proposal

Bound Class Templates

Introduction

Bound class - a class that has direct access to non-static members of some outer class and knows its own location within this outer class. Whether it can access private or protected members of the outer class, is governed by existing friendship rules. The only proposed way a bound class can be defined and instantiated is through a bound class template. Non-template bound classes are not proposed.

Outer class - a class that has a member or a base class which is a bound class. An outer class itself can be a bound class, or a normal class. This term does not imply anything special.

Bound class template - a class template whose template parameter list has a special parameter, with special syntax. The presence of this template parameter turns a normal class template into a bound class template.