Skip to content

Instantly share code, notes, and snippets.

@lrhn
lrhn / benchRE.dart
Last active October 22, 2018 14:48
Benchmark handwritten string matching vs RegExp
import "dart:async" show Future;
main() async {
print("Sample size: ${samples.length}"); // Ensure samples generated.
for (var i = 0; i < 5; i++) {
// Measure the overhead of the benchmarking using the [nop] test.
bench("base", nop);
await new Future.delayed(Duration.zero); // prevent blocking when run in browser.
bench("re-case", fre1);
await new Future.delayed(Duration.zero);
@lrhn
lrhn / expandMultitest.dart
Created October 11, 2018 13:26
Expands a Dart SDK multitest file to separate tests.
import "dart:convert";
import "dart:io";
var multitestRE = RegExp(r"//#[ \t]*(\w+)[ \t]*:(.*)");
var filenameRE = RegExp(r"(?=(_test)?\.dart$)");
main(List<String> args) {
for (var fileName in args) {
if (!fileName.endsWith(".dart")) {
stderr.writeln("Not a Dart file: $fileName. Must end with \".dart\".");
@lrhn
lrhn / expando_locale.dart
Created September 28, 2018 08:58
Expando Locale Example
abstract class Locale {
String get languageCode;
String get scriptCode;
String get regionCode;
const factory Locale(String languageCode,
[String regionOrScriptCode, String regionCode]) = _Locale._select;
}
class _Locale implements Locale {
@lrhn
lrhn / locale.dart
Last active September 26, 2018 13:24
Example of Locale validation.
abstract class Locale {
String get languageCode;
String get scriptCode;
String get regionCode;
const factory Locale(String languageCode,
[String regionOrScriptCode, String regionCode]) = _Locale._select;
}
class _Locale implements Locale {
@lrhn
lrhn / stringbench.dart
Last active August 19, 2018 12:19
String Concatenation Benchmark
const int count = 100000;
void test(String s) {}
void main() {
Stopwatch t = new Stopwatch();
// Run more than once to account for JIT compilation and optimization.
for (int i = 0; i < 3; i++) {
// USING STRING
@lrhn
lrhn / CodeUnit.py
Created July 16, 2018 06:27
Sublime Plugin: .codeUnitAt(0)
import sublime
import sublime_plugin
import re
class DartCodeUnitCommand(sublime_plugin.TextCommand):
def run(self, edit):
rd = re.compile(r'r?([\'"])(.)\1\.codeUnitAt\(0\)');
for selection in self.view.sel():
selectionText = self.view.substr(selection);
matches = list(rd.finditer(selectionText))
@lrhn
lrhn / main.dart
Created May 24, 2018 17:13
Typed JSON Parsing
/// A parser of (some) JSON source.
///
/// Each type of parser must implement [parseInput].
/// For example, a pure boolean-parser can have a [parseInput] that
/// only accepts the words "true" and "false".
abstract class TypedJsonParser<T> {
const TypedJsonParser();
/// Parse a JSON value expression from [input] to a [T].
///
import "dart:io";
bool verbose = false;
bool dryRun = false;
main(List<String> args) {
int unchanged = 0;
for(var path in args) {
if (path == "-v") {
verbose = true;
continue;
@lrhn
lrhn / ascii
Created January 27, 2017 08:35
ASCII helper script - converts arguments to ASCII codes
#! /usr/bin/perl
my $format = "%2.2X";
if ($ARGV[0] eq "-l") {
$format = "%2.2x";
shift @ARGV;
}
if ($#ARGV >= 0) {
my @hex = ();
for $a (@ARGV) {
for $c ( split("", $a) ) {
@lrhn
lrhn / RotateSelection.py
Created November 30, 2016 13:58
Sublime commands to rotate the contents of selections.
import sublime
import sublime_plugin
# Rotate contents of selections forwards.
class RotateSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
prevText = view.substr(view.sel()[-1])
for selection in view.sel():
text = view.substr(selection);