Difference between revisions of "Cheat"

From HalfgeekKB
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 232: Line 232:
 
# Or without line endings
 
# Or without line endings
 
perl -MJSON -E 'print JSON->new->utf8->encode([map { chomp; $_ } <>])'
 
perl -MJSON -E 'print JSON->new->utf8->encode([map { chomp; $_ } <>])'
 +
# Convert gnarly JSON into normalized, canonicalized, indented JSON
 +
perl -MJSON -E 'undef $/; $j=JSON->new->utf8->canonical->loose->relaxed->pretty; print $j->encode($j->decode(<>));'
 +
 
# Optionally add ->pretty to any of the above
 
# Optionally add ->pretty to any of the above
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 300: Line 303:
  
 
===Python===
 
===Python===
 +
 +
* [https://docs.python.org/2/library/json.html Python 2.* Full JSON module documentation]
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
# Load stdin as JSON data
+
# Module
 +
import json
 +
# Or, if you have the simplejson module installed:
 +
#import simplejson as json
 +
 
 +
#
 +
# Reading JSON
 +
#
 +
 
 +
# Load an open file (readable file-like object) as JSON data
 +
some_python_data = json.load(some_readable_object)
 +
 
 +
# e.g. Load stdin as JSON data
 
import sys
 
import sys
import json
+
some_python_data = json.load(sys.stdin)
data = json.load(sys.stdin)
+
 
 +
# Load a string as JSON data
 +
some_python_data = json.loads(some_json_string)
 +
 
 +
#
 +
# Writing JSON
 +
#
 +
 
 +
# Defaults of note:
 +
# * Output is limited to ASCII-only by default (ensure_ascii=True).
 +
# * No indents/newlines are used for formatting (indent=None).
 +
# * Out-of-range float values are encoded as the JavaScript-valid but JSON-invalid expressions
 +
# Infinity, -Infinity, and NaN (allow_nan=True).
 +
# * List and key-value delimiters each have a trailing space (separators=(', ',': ')).
 +
 
 +
# Dump some data as JSON to an open file (writable file-like object)
 +
json.dump(some_python_data, some_writable_object, allow_nan=False)
 +
 
 +
# Dump some data as sorted, compact (i.e. canonical) JSON to an open file
 +
json.dump(some_python_data, some_writable_object, allow_nan=False, separators=(',',':'), sort_keys=True)
 +
 
 +
# Dump some data as sorted JSON formatted with 4-space indents to an open file
 +
json.dump(some_python_data, some_writable_object, allow_nan=False, indent=4, sort_keys=True)
 +
 
 +
# If using Python 3.2 or later, or if simplejson is used, you can say something like `indent="\t"` to get tab indents.
 +
 
 +
# Convert some data to a JSON string
 +
some_json_string = json.dumps(some_python_data, allow_nan=False)
  
# TODO: Add load from string, convert to string
+
# Convert some data to a sorted, compact (i.e. canonical) JSON string
# Refer to https://docs.python.org/2/library/json.html
+
# (Note that json.dumps() accepts roughly the same kwarg options as json.dump() above)
 +
some_json_string = json.dumps(some_python_data, allow_nan=False, separators=(',',':'), sort_keys=True)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==Decoding CSV==
 
==Decoding CSV==
 +
 +
===Perl: Oldish way===
  
 
  <nowiki>
 
  <nowiki>
Line 328: Line 375:
 
}
 
}
 
}
 
}
 +
</nowiki>
 +
 +
===Perl: Convert headed rows to JSON objects===
 +
 +
<nowiki>
 +
# Sets up STDIN for UTF-8
 +
use open qw/:std :utf8/;
 +
my $fh = \*STDIN;
 +
 +
use Text::CSV ();
 +
 +
# Full doc at https://metacpan.org/pod/Text::CSV
 +
my $csv = Text::CSV->new({
 +
binary => 1, # fields can contain non-text such as newlines
 +
auto_diag => 1, # complain about any errors
 +
# empty_is_undef => 1, # treat empty values as undef/null instead of strings
 +
});
 +
 +
# Get the first row as the column names
 +
$csv->header($fh, { munge => 'none' }); # Default is to force all-lowercase. (Why?) munge=>'none' leaves the case alone.
 +
 +
my @rows;
 +
 +
# $csv->getline_hr($fh) gets a hash for the next row. $csv->header(…)/$csv->column_names(…) must happen first.
 +
# @rows = @{ $csv->getline_hr_all($fh) } does the same for all rows at once.
 +
while(my $row = $csv->getline_hr($fh)) {
 +
push @rows, $row;
 +
}
 +
 +
# $csv->getline($fh) gets an array for the next row.
 +
# @{ $csv->getline_all($fh) } does the same for all rows at once.
 +
 +
use JSON ();
 +
print JSON->new->utf8->pretty->encode(\@rows);
 
</nowiki>
 
</nowiki>
  
Line 1,178: Line 1,259:
 
}
 
}
 
</nowiki>
 
</nowiki>
 +
 +
==Java: Genericized Empty Singleton==
 +
 +
Creates a global singleton representing an empty object which, having no elements of type T, doesn't actually need a separate implementation for each type. Instead, a single instance is created and then repurposed as necessary using an unchecked cast.
 +
 +
Inspired by [http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collections.java#Collections.emptyList%28%29 the implementation of <code>Collections.emptyList()</code>] and the [https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom initialization-on-demand holder idiom].
 +
 +
 +
<syntaxhighlight lang=java>
 +
public class Dingus<T> {
 +
// A method to override
 +
public boolean isEmpty() {
 +
return false;
 +
}
 +
 +
private static final class EmptyDingus<T> extends Dingus<T> {
 +
private EmptyDingus() {}
 +
 +
private static final class InstanceHolder {
 +
private static final EmptyDingus<Object> INSTANCE = new EmptyDingus<Object>();
 +
}
 +
 +
@SuppressWarnings("unchecked")
 +
static <T> EmptyDingus<T> getInstance() {
 +
return (EmptyDingus<T>) InstanceHolder.INSTANCE;
 +
}
 +
 +
// Overridden methods
 +
 +
@Override
 +
public boolean isEmpty() {
 +
return true;
 +
}
 +
}
 +
 +
public static <T> Dingus<T> empty() {
 +
return EmptyDingus.getInstance();
 +
}
 +
}
 +
</syntaxhighlight>
  
 
==JavaScript: Binary search, uniq==
 
==JavaScript: Binary search, uniq==
Line 1,326: Line 1,447:
  
 
1;
 
1;
 +
</syntaxhighlight>
 +
 +
===Arduino/C++ class using opaque pointer===
 +
 +
The [http://en.wikipedia.org/wiki/Opaque_pointer "pimpl" idiom] is used to limit the details in the header to the outward interface while keeping most implementation details inside the source file. The outward class is implemented entirely in terms of calls to an object of a class only instantiable within the file.
 +
 +
====OutwardClass.h====
 +
 +
<syntaxhighlight lang=cpp>
 +
#ifndef OutwardClass_h
 +
#define OutwardClass_h
 +
 +
class OutwardClass {
 +
  public:
 +
    OutwardClass();
 +
    ~OutwardClass();
 +
    void foo();
 +
    int bar(int a, int b);
 +
  private:
 +
    void * _impl;
 +
};
 +
 +
#endif // OutwardClass_h
 +
</syntaxhighlight>
 +
 +
====OutwardClass.cpp====
 +
 +
<syntaxhighlight lang=cpp>
 +
#include "OutwardClass.h"
 +
 +
namespace {
 +
 +
class Impl {
 +
  private:
 +
    // ...
 +
 +
  public:
 +
    Impl() {
 +
      // ...
 +
    }
 +
 +
    ~Impl() {
 +
      // ...
 +
    }
 +
 +
    void foo() {
 +
      // ...
 +
    }
 +
 +
    int bar(int a, int b) {
 +
      // ...
 +
    }
 +
};
 +
 +
}
 +
 +
 +
// Define OutwardClass in terms of Impl
 +
 +
#define IMPL ((Impl*)_impl)
 +
 +
OutwardClass::OutwardClass() {
 +
  _impl = new Impl();
 +
}
 +
 +
OutwardClass::~OutwardClass() {
 +
  delete IMPL;
 +
}
 +
 +
void OutwardClass::foo() {
 +
  IMPL->foo();
 +
}
 +
 +
int OutwardClass::bar(int a, int b) {
 +
  return IMPL->bar(a, b);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Latest revision as of 06:30, 6 April 2022

Contents

Concatenate strings

Excel

The operator is &. If A is first name, B is last name, and C is organization, then "Smith, John (XYZ)" is achieved as

=B1&", "&A1&" ("&C1&")"

Change case of strings

# Perl
$u = uc $str;
$l = lc $str;
// JavaScript
u = str.toUpperCase();
l = str.toLowerCase();

Excel, operating on contents of cell A1:

=UPPER(A1)
=LOWER(A1)

Escape/unescape HTML

# Perl
use HTML::Entities;
$a = "V&amp;aring;re norske tegn b&amp;oslash;r &amp;#230res";
decode_entities($a);
encode_entities($a, "\200-\377"); # arg 2 optional
// JavaScript, escape only
// Contrived not to cause trouble with old HTML
// Uses #39 instead of apos
function htmlEscape(s) {
	return (String(s)
		.replace(/\x26/g, "\x26amp;")
		.replace(/\x3c/g, "\x26lt;")
		.replace(/\x3e/g, "\x26gt;")
		.replace(/\x22/g, "\x26quot;")
		.replace(/\x27/g, "\x26#39;")
	);
}
// JavaScript solution as Java
private static String htmlEscape(String s) {
	return s.replace("&", "&amp;")
			.replace("<", "&lt;")
			.replace(">", "&gt;")
			.replace("\"", "&quot;")
			.replace("'", "&#39;");
}

Escape/unescape URI (including URL) characters

# Perl
use URI::Escape;	# http://search.cpan.org/~gaas/URI/
$safe = uri_escape("10% is enough\n");
$verysafe = uri_escape("foo", "\0-\377");
$str  = uri_unescape($safe);

Cause an HTML div or span to appear as a single line extending beyond the screen width

/* CSS */
#id { white-space: nowrap; }

Internet Explorer: Remove scrollbar except when necessary

/* CSS */
html { overflow: auto; }

DOM: Generate a new node

// JavaScript
var node1 = document.createTextNode(str);
var node2 = document.createElement(tagName);
node2.appendChild(node1);

DOM: Adding and removing nodes

// JavaScript
parentElement.insertBefore(elementToInsert, insertBeforeThis); // [1]
parentElement.removeChild(elementToRemove);
// Remove all children
while(e.firstChild) e.removeChild(e.firstChild);

CSS equivalents to presentational HTML and word processor font styles

/* CSS */
#id {
	font-weight: bold;		/* b (bold) */
	font-style: italic;		/* i (italic) */
	font-variant: small-caps;	/* (small caps instead of lower case) */
	text-decoration: underline overline line-through blink;
		/* u (underline), (overline), s strike (strikethrough), blink
			"none" for nothing */
	white-space: pre;		/* pre (preformatted/space-formatted) */
	text-transform: uppercase;
		/*	uppercase: all caps
			lowercase: all lower-case
			capitalize: first character per word upper-case */
}

Join array elements

 // JavaScript
 a.join(sep);
 ["http:","","example.com",""].join('/');	// http://example.com/

Split string to array

// JavaScript
a = "1,2,3".split(',');	// ["1","2","3"]
a = "A,B.C".split(/[,.]/);	// ["A","B","C"]
// Leading/trailing blanks not trimmed
a = ",".split(',');		// ["",""]
// Intervening blanks not trimmed
a = "X,,,X".split(',');	// ["X","","","X"]
// Blank splits to single empty string
a = "".split(',');		// [""]

Replace patterns in a string

# Perl
# s/// replaces in-place
# For a string in $_
$_ = "string-to-search";
s/-/ /g;	# Replace all '-' with ' '
# For a string not in $_
$str = "string to search";
$str =~ s/-/ /g;
// JavaScript [2]
// Does NOT replace in-place
var s = "string-to-search";
s = s.replace('-',' ');	// With plain search string. NOT GLOBAL.
// OR
s = s.replace(/-/g,' ');	// With regex, global due to /g flag.
# PHP [3]
# Does NOT replace in-place
$s = "string-to-search";
$s = preg_replace('/-/', ' ', $s);
# Without regex [4]
$s = str_replace('-', ' ', $s);

Arrays

Converting among array and non-array lists

// JavaScript

// Use a list as arguments to a function
fn.apply(valOfThis, [a0, a1, a2]);

// Cast an enumerable list as some other kind of data
function evalableList(n,l) {
	if((!n) || (n < 1)) return "";
	l = l || 'a';
	var z = [];
	for(var i = 0; i < n; ++i) {
		z[z.length] = i;
	}   
	return l + '[' + z.join('],' + l + '[') + ']';
}
var a = eval('[' + evalableList(arguments.length,'arguments') + ']');
function construct(c, a) {
	if(arguments.length < 2)
		a = [];
	return eval('new c(' + evalableList(a.length) + ');');
}
var d = construct(Date,[100 * 86400 * 1000]);


JavaScript/ECMAScript: Passing an array instead of arguments to a constructor

Refer to JavaScript microlibs for the construct() function.

String to integer

// JavaScript
parseInt(str,10); // force decimal radix

Conditionals (if statements)

# sh bash ash
if [ -d "$@" ]; then
	echo "$@" is directory
elif [ -f "$@" ]; then
	echo "$@" is file
else
	echo "$@" is something I do not handle
fi



JavaScript/ECMAScript: Get the global scope object from anywhere

// If call has no arguments, the default context
// is the global scope object
var gs = (function(){return this}).call();

Encoding and decoding JSON

One-liners

# Perl one-liners
# Convert all text from stdin to single string in an array
perl -MJSON -E 'undef $/; print JSON->new->utf8->encode([<>])'
# Same, but each line separately (including line endings)
perl -MJSON -E 'print JSON->new->utf8->encode([<>])'
# Or without line endings
perl -MJSON -E 'print JSON->new->utf8->encode([map { chomp; $_ } <>])'
# Convert gnarly JSON into normalized, canonicalized, indented JSON
perl -MJSON -E 'undef $/; $j=JSON->new->utf8->canonical->loose->relaxed->pretty; print $j->encode($j->decode(<>));'

# Optionally add ->pretty to any of the above
# Python one-liners
# Validate and pretty print data.json
python -mjson.tool <data.json

Perl

# Perl
# See also http://search.cpan.org/~makamaka/JSON-2.12/

use JSON;

# Encode
my $out = JSON->new->utf8->pretty->encode($data);
# Use undef for null
# Use JSON::true, JSON::false for true, false
# Alternatively, use the constant scalar refs \1, \0 for true, false
#	(e.g. $data = { istrue => \1, isfalse => \0, isnull => undef })
# Replace utf8 with ascii for 7-bit safe
# Omit pretty or do pretty(0) to use compact form
# Add canonical to sort keys
# Add escape_slash to conform to JSON grammar for /
# Add allow_bignum to treat Math::BigInt and Math::BigFloat
#	as numbers instead of objects or strings
# Add allow_nonref to convert scalars to their corresponding
#	JSON atom were they the values in key:value pairs
#	(e.g. JSON->new->ascii->allow_nonref->encode("\x{263a}") eq '"\u263a"')

# Decode
my $in = JSON->new->decode($jsontext);
# Add allow_bignum (and possibly allow_blessed) to decode
#	long numbers as Math::BigInt and Math::BigFloat
# Add loose to allow even egregiously malformed input

# JSON::is_bool(value) tests a value to see if it originally
#	represented JSON true or false.
sub jsontest { return JSON::is_bool($_[0]) ? $_[0] : undef; }
sub jsoniif {
	my($n,$iftrue,$iffalse,$ifnotbool) = @_;
	return $ifnotbool unless JSON::is_bool($n);
	return $n ? $iftrue : $iffalse;
}

# Of course a new object isn't necessary each call:
my $json = JSON->new->pretty;
$out = $json->encode($data);
$in = $json->decode($jsontext);

JavaScript

// JavaScript / ECMAScript
// Using http://www.json.org/json2.js
var enc = JSON.stringify(obj);
var pretty = JSON.stringify(obj,null,'\t');
// 2nd arg is an optional serializer function
// 3rd arg specifies indent
var dec = JSON.parse(enc);
// 2nd arg is an optional output filter

Python

# Module
import json
# Or, if you have the simplejson module installed:
#import simplejson as json

#
# Reading JSON
#

# Load an open file (readable file-like object) as JSON data
some_python_data = json.load(some_readable_object)

# e.g. Load stdin as JSON data
import sys
some_python_data = json.load(sys.stdin)

# Load a string as JSON data
some_python_data = json.loads(some_json_string)

#
# Writing JSON
#

# Defaults of note:
# * Output is limited to ASCII-only by default (ensure_ascii=True).
# * No indents/newlines are used for formatting (indent=None).
# * Out-of-range float values are encoded as the JavaScript-valid but JSON-invalid expressions
#	Infinity, -Infinity, and NaN (allow_nan=True).
# * List and key-value delimiters each have a trailing space (separators=(', ',': ')).

# Dump some data as JSON to an open file (writable file-like object)
json.dump(some_python_data, some_writable_object, allow_nan=False)

# Dump some data as sorted, compact (i.e. canonical) JSON to an open file
json.dump(some_python_data, some_writable_object, allow_nan=False, separators=(',',':'), sort_keys=True)

# Dump some data as sorted JSON formatted with 4-space indents to an open file
json.dump(some_python_data, some_writable_object, allow_nan=False, indent=4, sort_keys=True)

# If using Python 3.2 or later, or if simplejson is used, you can say something like `indent="\t"` to get tab indents.

# Convert some data to a JSON string
some_json_string = json.dumps(some_python_data, allow_nan=False)

# Convert some data to a sorted, compact (i.e. canonical) JSON string
# (Note that json.dumps() accepts roughly the same kwarg options as json.dump() above)
some_json_string = json.dumps(some_python_data, allow_nan=False, separators=(',',':'), sort_keys=True)

Decoding CSV

Perl: Oldish way

# Perl
use Text::CSV;	# http://search.cpan.org/~alancitt/Text-CSV/

my $csv = Text::CSV->new();

while(<CSVFILE>) {
	if($csv->parse($_)) {
		my @row = $csv->fields();
		push(@set,\@row);
	}
	else {
		die "Parse error: " . $csv->error_input;
	}
}

Perl: Convert headed rows to JSON objects

# Sets up STDIN for UTF-8
use open qw/:std :utf8/;
my $fh = \*STDIN;
 
use Text::CSV ();

# Full doc at https://metacpan.org/pod/Text::CSV
my $csv = Text::CSV->new({
	binary => 1, # fields can contain non-text such as newlines
	auto_diag => 1, # complain about any errors
	# empty_is_undef => 1, # treat empty values as undef/null instead of strings
});

# Get the first row as the column names
$csv->header($fh, { munge => 'none' }); # Default is to force all-lowercase. (Why?) munge=>'none' leaves the case alone.

my @rows;

# $csv->getline_hr($fh) gets a hash for the next row. $csv->header(…)/$csv->column_names(…) must happen first.
# @rows = @{ $csv->getline_hr_all($fh) } does the same for all rows at once.
while(my $row = $csv->getline_hr($fh)) {
	push @rows, $row;
}

# $csv->getline($fh) gets an array for the next row.
# @{ $csv->getline_all($fh) } does the same for all rows at once.

use JSON (); 
print JSON->new->utf8->pretty->encode(\@rows);

JavaScript: Mostly reversible XML/JSON representation

A JSON representation I've come up with ad hoc has a structure that is as follows:

  • An array [] represents a sequence of nodes.
  • A string or number (convertible to string) represents a text node.
  • An object {} represents an element node.
    • The property "n" represents the name of the element.
    • The property "a" is a {} containing the key-value pairs for attributes. If "a" is omitted it is equivalent to zero attributes.
    • The property "c" is a [] representing the sequence of nodes contained within this element. If "c" is omitted it is equivalent to having no contained nodes, which is in turn equivalent to an empty tag.

Given an object of this form, the following function will convert it into equivalent XML, sans declaration.

function objectToXML(input) {
	function safe(str) {
		return (("" + str)
			.replace(/&/g,'&amp;')
			.replace(/</g,'&lt;')
			.replace(/>/g,'&gt;')
			.replace(/'/g,'&#39;')
			.replace(/"/g,'&quot;')
		);
	}

	function recurse(input) {
		if(typeof input == 'object') {
			if(input.length && input.splice) {
				var out = [''];
				for(var i = 0; i < input.length; ++i) {
					out.push(recurse(input[i]));
				}
				return out.join('');
			}
			else {
				var name = input.n,
					attr = input.a || {},
					cont = input.c || null,
					a = ['<'+name];

				for(var k in attr) {
					a.push(" " + k + '="' + safe(attr[k]) + '"');
				}

				if(cont === null) {
					a.push("/>");
				}
				else {
					a.push(">");
					a.push(recurse(cont));
					a.push("</" + name + ">");
				}
				return a.join('');
			}
		}
		else {
			return safe(input);
		}
	}

	return recurse(input);
}

Converting a UUID to and from Base64

This assumes that your UUID is in the text form and not the binary form.

To Base64

Replace uuidgen with echo if you already have a UUID.

uuidgen | \
perl -MMIME::Base64 -n -e 'chomp; $_=lc$_;
 s/[^0-9a-f]+//sg; die "Bad length" unless length($_)==32;
 $c=pack("H*","$_0000"); print substr(encode_base64($c),0,22)."\n";'

Since the UUID is a static length (22 chars in base64), the trailing '==' can be omitted, provided that the fact it's a UUID is clear from the context. This script omits the == and also ensures that the padding bits are nulls in the unlikely event of a pathological implementation.

From Base64

echo 'FFEEEEDDDDCCCCBBBBAAAA' | \
perl -MMIME::Base64 -n -e 'chomp;
 s![^A-Za-z0-9+/]+!!g; die "Bad length" unless length($_)==22;
 $d=decode_base64("$_=="); $_=unpack("H*",$d);
 s/^(.{8})(.{4})(.{4})(.{4})(.{12})$/$1-$2-$3-$4-$5\n/; print'

This script unpacks the 22-character base64 form (with or without ==), then repacks it in the common hyphenated hexadecimal form.

How to detect an illegal Base64 UUID

A 22-character b64 string encodes 132 bits, while a UUID only takes 128. This means only the first two bits of the last character are used. If the remaining four bits are anything but 0, the string is suspect. This happens to be easy to check; the only valid final characters are A (0), Q (16), g (32), and w (48).

Thus, a legal Base64 UUID matches the pattern

^[A-Za-z0-9+/]{21}[AQgw]$

More complete script

See uuidex.

Base 64/Base64

General information

Matching

Depending on your regexp engine, it may be practical to use a regexp to validate a base 64 string. The patterns suggested below will (theoretically; this is untested code) verify that the length is a multiple of 4 and that any padding bits are 0.

$ra = qr![A-Za-z0-9+/]!; # whole alphabet
$rb = qr![AQgw]!; # whole alphabet such that value and 001111 = 0
$rc = qr![AEIMQUYcgkosw048]!; # value and 000011 = 0
$anyword = qr/$ra{4}/;
$lastword = qr/(?:$anyword|$ra$rb==|$ra{2}$rc=)/;
$valid = qr/^(?:$anyword*$lastword)?$/;

$message =~ $valid or die;

Perl

# See http://search.cpan.org/~gaas/MIME-Base64/

use MIME::Base64;
$enc = encode_base64(data,eol);
# Omitting eol means eol = \n
# Provide "" for continuous
$dec = decode_base64(b64chars);

# Without namespace pollution
use MIME::Base64 ();
$enc = MIME::Base64::encode(data,eol);
$dec = MIME::Base64::decode(b64chars);

JavaScript

Some browsers support atob() and btoa(), which convert from and to Base64, respectively. IE doesn't seem to be among them. Fortunately, it's simple enough to implement a codec. See Base 64 (ECMAScript).

Transferring files via clipboard

GnuPG

Requires gpg on both machines.

This command prints an unencrypted (and possibly compressed) OpenPGP packet in radix-64 armor:

gpg -ao - --store src-filename

Copy the packet to the clipboard, then paste it to stdin on a destination console running:

gpg > dest-filename

JavaScript microlibs

See JavaScript microlibs.

Perl, Perl SAX: Watch the events produced by a Perl SAX parser

Add this simple module to your code:

package EMonitor;

sub new {
	return bless {};
}

sub AUTOLOAD {
	my $self = shift;
	use Data::Dumper;
	print Data::Dumper->Dump([\@_],[$AUTOLOAD]);
}

1;

Then provide a new object of that class to the parser and let it rip:

use XML::SAX::ParserFactory;

my $h = new EMonitor;
my $p = XML::SAX::ParserFactory->parser(Handler => $h);

$p->parse_uri("foo.xml");

jQuery

See Cheat/jQuery.

DOM: Modifying a frame's height (or width) from inside a frame

// JavaScript
top.document.getElementsByTagName('frameset')[0].rows = "50%,50%";
// jQuery
$('frameset',top.document).each(function(){ this.rows = "50%,50%"; });

// Substitute parent, parent.parent, parent.parent.parent, ...
//	for top if top is not the target.  The important part is that
//	you have the document object for the frameset.
// Substitute cols for rows if the columns are to be changed.
// The jQuery version degrades to a no-op if the frameset isn't found.
//	When using straight DOM, make sure to check for existence.
// getElementsByTagName(...)[0] can be replaced with getElementById(...);
//	similarly, $('frameset', ...) can be replaced with
//	$('#fooFramesetId', ...).

MS SQL: Using CASE as a ternary operator

-- This was used for some summarizing code
-- The number of rows where column `level` contains " IV "
sum(case when level like '% IV %' then 1 else 0 end)

MS SQL: Compare an ugly date string to the current instant

-- US dates are nasty
case when convert(datetime,'10/30/2008 16:29') < getdate()
	then 'past' else 'future' end

Clip N bytes off the beginning of a file

# shell
# copy a file except the first 3 bytes
dd bs=1 skip=3 if=infile of=outfile

This is good for clipping a superfluous BOM off of a Unicode text:

  • UTF-8: 3 bytes (ef bb bf)
  • UTF-16: 2 bytes (fe ff for BE, ff fe for LE)
  • UTF-32: 4 bytes (00 00 fe ff for BE, ff fe 00 00 for LE)

Java: Copy List without nulls

Here is the compact one-liner version, which only works without generics. Replace list with the list to remove items from and String with the type used by the List.

	list.removeAll(Arrays.asList(new String[]{null}));

To reduce the verbosity, one could add the method

	public static <T> List<T> nullList() {
		ArrayList<T> nul = new ArrayList<T>(1);
		nul.add(null);
		return Collections.unmodifiableList(nul);
	}

and then get the null list for the type in question to do

	list.removeAll(nullList());

This version exploits generics. If there is a null list to be used repeatedly, it would make sense to make it a static final:

	private static final List<String> nullListString = nullList();

Java: Loading plugins from .jar files

This is a high-level description of one method that works. It likely won't work as written; it's left as an exercise to look up the missing imports and exceptions in the API. Partially cribbed from Dr. Dobb's.

Suppose that your application wants to load a plugin that implements interface Plugin.

	package org.example;

	public interface Plugin {
		public String name();
		public int compute(int a, int b);
	}

First, you'll probably want some plugins.

	package com.example;

	public class AddPlugin implements org.example.Plugin {
		public String name() {
			return "example.com AddPlugin 1.0";
		}
		public int compute(int a, int b) {
			return a + b;
		}
	}

You'll put something in the manifest of the jar file to indicate which class implements the interface.

	Manifest-Version: 1.0
	Foo-Plugin: com.example.AddPlugin

When you get a list of candidate jar files, you'll be able to query the manifest to see if the applicable information is given.

	JarFile jar = new JarFile(file);
	String cn;
	Manifest m = jar.getManifest();
	Attributes a = m.getMainAttributes();
	cn = a.getValue("Foo-Plugin");
	if(c == null) {
		// skip
	} else {
		// got class name
	}

Given a filename and a class name, URLClassLoader can attempt to load and instantiate the class.

	URLClassLoader loader = new URLClassLoader(
		new URL[] { file.toURI().toURL() } );
	Class pclass = loader.loadClass(cn);

	// Modifier could also be used here
	// but may as well just rely on exceptions
	if(!Plugin.class.isAssignableFrom(pclass)) {
		// Error: loaded class does not implement interface
	}
	Object o = pclass.newInstance();
	Plugin p = (Plugin)o;

If that succeeds, you can do stuff with the resulting object.

	System.out.println("Name: " + p.name());
	System.out.println("3 ? 7: " + p.compute(3,7));

Java: Primitive array to List

See PrimitiveArrayLists.java.

Java: Boilerplate exception class


/**
Exception description goes here. Replace all "ZZZZ" with the name of the
exception sans the "Exception". Replace all "YYYY" with empty for a checked
exception or "Runtime" for an unchecked exception. If this is a nested class,
you may need to change "public class" to "public static class".
**/
public class ZZZZException
	extends YYYYException
{
	/**
		Constructs a new exception with {@code null} as its detail message.

		@see YYYYException#YYYYException()
	**/
	public ZZZZException() {
		super();
	}

	/**
		Constructs a new exception with the specified detail message.

		@see YYYYException#YYYYException(String)
	**/
	public ZZZZException(String message) {
		super(message);
	}

	/**
		Constructs a new exception with the specified detail message and cause.

		@see YYYYException#YYYYException(String,Throwable)
	**/
	public ZZZZException(String message, Throwable cause) {
		super(message, cause);
	}

	/**
		Constructs a new exception with the specified cause and a detail message of {@code (cause==null ? null : cause.toString())} (which typically contains the class and detail message of cause).

		@see YYYYException#YYYYException(Throwable)
	**/
	public ZZZZException(Throwable cause) {
		super(cause);
	}
}

JavaScript/C-like: Get month for day of year

Let d be the 1-based day of the year (i.e. 33 is February 2). Let leap be a boolean that is true if the year under test is a leap year. The result is only defined for an integer d in the range 1 to 365+(leap?1:0) inclusive; this code does not check the range.

The following expression is a binary search tree implemented in ternary conditionals. The traversal itself costs only 4 comparisons in the worst case (with the first third of the year taking only 3). The expression before the tree normalizes d to its equivalent non-leap-year day; it can be omitted if (somehow) only 365-day years are in use.

The returned month is 1-based (converting to 0-based is trivial).

if(leap && (d >= 60)) --d;
return ((d < 121) ?
	((d < 60) ?
		((d < 32) ? 1 : 2) :
		((d < 91) ? 3 : 4)
	) :
	((d < 244) ?
		((d < 182) ?
			((d < 152) ? 5 : 6) :
			((d < 213) ? 7 : 8)
		) :
		((d < 305) ?
			((d < 274) ? 9 : 10) :
			((d < 335) ? 11 : 12)
		)
	)
);

Java: Check array bounds for java.io-style write and read

		if((off < 0) || (len < 0) || (len > buf.length - off))  
			throw new IndexOutOfBoundsException("Invalid range");

Java: Boilerplate OutputStream implementation

import java.io.*;

/**
An {@link OutputStream} implementation based entirely on the implementation
of {@link #write(byte[], int, int)}.

@see	OutputStream
**/
public class ZZZZOutputStream
	extends OutputStream
{
	// Stream closed state
	private boolean streamIsClosed = false;
	
	
	/**
		Constructs a {@link ZZZZOutputStream}.
	**/
	public ZZZZOutputStream()
	{
	}
	
	/**
		Writes {@code len} bytes from the specified byte array starting at
		offset {@code off} to this output stream.
		
		@param	b	A data array
		@param	off	The offset into {@code b} at which to start copying
		@param	len	The number of elements to write
		@throws	NullPointerException	if {@code b} is {@code null}
		@throws	IndexOutOfBoundsException	if {@code off} is negative,
							or if {@code len} is negative,
							or if {@code len} exceeds
								{@code b.length - off}
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public synchronized void write(byte[] b, int off, int len)
		throws IOException
	{
		// Checks
		if(streamIsClosed)
			throw new IOException("Stream is closed");
		if(b == null)
			throw new NullPointerException();
		if((off < 0) || (len < 0) || (len > b.length - off))  
			throw new IndexOutOfBoundsException();
		
		// TODO: Implement
	}
	
	/**
		Writes {@code b.length} bytes from the specified byte array to this
		output stream.
		
		@param	b	A data array
		@throws	NullPointerException	if {@code b} is {@code null}
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public void write(byte[] b)
		throws IOException
	{
		// Checks
		//if(streamIsClosed)
		//	throw new IOException("Stream is closed");
		//if(b == null)
		//	throw new NullPointerException();
		
		// TODO: Reimplement if desired
		
		// This implementation is consistent with the general contract of
		// OutputStream and should probably be left alone.
		// If modified:
		// - Uncomment the checks above
		// - Add synchronized to the method signature if necessary
		write(b, 0, b.length);
	}
	
	/**
		Writes the specified byte to this output stream.
		The 8 low-order bits of {@code b} are written as a single byte.
		The 24 high-order bits of {@code b} are ignored.
		
		@param	b	A byte
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public void write(int b)
		throws IOException
	{
		// Checks
		//if(streamIsClosed)
		//	throw new IOException("Stream is closed");
		
		// TODO: Reimplement if desired
		
		// This implementation is not especially efficient but probably
		// suitable for cases where most of the writing will be done from
		// an array. It should be replaced if something more efficient is
		// reasonable.
		// If modified:
		// - Uncomment the checks above
		// - Add synchronized to the method signature if necessary
		write(new byte[]{(byte)b}, 0, 1);
	}
	
	/**
		Flushes this output stream and forces any buffered output bytes to
		be written out.
		
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public synchronized void flush()
		throws IOException
	{
		// Checks
		if(streamIsClosed)
			throw new IOException("Stream is closed");
		
		// TODO: Implement
	}
	
	/**
		Closes this output stream and releases any system resources
		associated with this stream.
		Calling {@link #close} on an already closed stream does nothing.
		
		@throws	IOException	if an I/O error occurs
	**/
	public synchronized void close()
		throws IOException
	{
		if(!streamIsClosed) {
			// TODO: Implement
			streamIsClosed = true;
		}
	}
}


Java: Boilerplate InputStream implementation

import java.io.*;

/**
An {@link InputStream} implementation based entirely on the implementation
of {@link #read(byte[], int, int)}.

@see	InputStream
**/
public class ZZZZInputStream
	extends InputStream
{
	// Stream closed state
	private boolean streamIsClosed = false;
	
	
	/**
		Constructs a {@link ZZZZInputStream}.
	**/
	public ZZZZInputStream()
	{
	}
	
	/**
		Reads up to {@code len} bytes from the input stream into the specified byte array.
		
		@param	b	A data array
		@param	off	The offset into {@code b} at which to start copying
		@param	len	The number of elements to read
		@throws	NullPointerException	if {@code b} is {@code null}
		@throws	IndexOutOfBoundsException	if {@code off} is negative,
							or if {@code len} is negative,
							or if {@code len} exceeds
								{@code b.length - off}
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public synchronized int read(byte[] b, int off, int len)
		throws IOException
	{
		// Checks
		if(streamIsClosed)
			throw new IOException("Stream is closed");
		if(b == null)
			throw new NullPointerException();
		if((off < 0) || (len < 0) || (len > b.length - off))  
			throw new IndexOutOfBoundsException();
		
		// TODO: Implement
	}
	
	/**
		Reads {@code b.length} bytes from the input stream into the specified byte array.
		
		@param	b	A data array
		@throws	NullPointerException	if {@code b} is {@code null}
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public int read(byte[] b)
		throws IOException
	{
		// Checks
		//if(streamIsClosed)
		//	throw new IOException("Stream is closed");
		//if(b == null)
		//	throw new NullPointerException();
		
		// TODO: Reimplement if desired
		
		// This implementation is consistent with the general contract of
		// InputStream and should probably be left alone.
		// If modified:
		// - Uncomment the checks above
		// - Add synchronized to the method signature if necessary
		return read(b, 0, b.length);
	}
	
	/**
		Reads the next byte from the input stream.
		The byte value is returned as an integer in the range {@code 0} to {@code 255}. If the end of the stream has been reached, {@code -1} is returned.
		
		@return	The next byte of data, or {@code -1} at the end of the stream
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public int read()
		throws IOException
	{
		// Checks
		//if(streamIsClosed)
		//	throw new IOException("Stream is closed");
		
		// TODO: Reimplement if desired
		
		// This implementation is not especially efficient but probably
		// suitable for cases where most of the reading will be done using
		// an array. It should be replaced if something more efficient is
		// reasonable.
		// If modified:
		// - Uncomment the checks above
		// - Add synchronized to the method signature if necessary
		byte[] b = new byte[1];
		return (read(b, 0, 1) < 0) ? -1 : (((int)b[0]) & 0xFF);
	}

	// Comment this out if the skip implementation
	// doesn't use it
	private byte[] skipBuffer = null;
	
	/**
		Skips over and discards up to {@code n} bytes from the input stream.
		This method may discard as few as {@code 0} or as many as {@code n} bytes from the stream. Skipping fewer than {@code n} bytes does not necessarily indicate the end of the stream.
		If {@code n} is negative or {@code 0}, no bytes are skipped.
		
		@throws	NullPointerException	if {@code b} is {@code null}
		@throws	IndexOutOfBoundsException	if {@code off} is negative,
							or if {@code len} is negative,
							or if {@code len} exceeds
								{@code b.length - off}
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public synchronized long skip(long n)
		throws IOException
	{
		// Checks
		if(streamIsClosed)
			throw new IOException("Stream is closed");
		
		// TODO: Reimplement if desired

		// This implementation is based on the read(byte[],int,int)
		// implementation and uses an array to pull the discarded bytes.
		// If your stream source directly supports seeking, this method
		// should be reimplemented to use it.
		// If modified:
		// - Remove the declaration for skipBuffer above
		
		if(n <= 0)
			return 0;
		
		// Change the array size if desired
		if(skipBuffer == null)
			skipBuffer = new byte[1024];
		
		long count = 0;
		while(count < n) {
			int c = read(skipBuffer, 0,
				Math.min(skipBuffer.length, n - count));
			if(c < 0)
				return count;
			count += c;
		}
		return count;
	}
	
	/**
		Returns an estimate of the number of bytes that can be read or
		skipped by the next call to this stream without blocking.
		The return value of this method should not be used to allocate a buffer to read all of its data.
		@return	The estimated number of bytes available to be consumed from
				this stream by the next method call to this object without
				blocking (including {@code 0} at the end of the stream)
		@throws	IOException	if this output stream is closed,
					or if an I/O error occurs
	**/
	public int available()
		throws IOException
	{
		// Checks
		if(streamIsClosed)
			throw new IOException("Stream is closed");
		
		// TODO: Reimplement if desired
		
		// This implementation takes advantage of the fact that the return
		// value of this method is a strictly voluntary offering. If your
		// stream source has a meaningful value to reflect here, it should
		// be used.
		// If modified:
		// - Add synchronized to the method signature if necessary
		
		return 0;
	}
		
	
	/**
		Closes this output stream and releases any system resources
		associated with this stream.
		Calling {@link #close} on an already closed stream does nothing.
		
		@throws	IOException	if an I/O error occurs
	**/
	public synchronized void close()
		throws IOException
	{
		if(!streamIsClosed) {
			// TODO: Implement
			streamIsClosed = true;
		}
	}
	
	/**
		Marks the current position in this input stream.
		Subsequently calling {@link #reset} while the mark is valid returns this stream to the marked position.
		Once the number of bytes read after the mark exceeds {@code readlimit}, the mark is no longer valid.
		Marking a closed stream has no effect.
		@param	readlimit	The number of bytes after the mark that can be
							read while keeping the mark valid
		@see	reset
	**/
	public void mark(int readlimit)
	{
		// TODO: Implement if desired
		
		// If modified:
		// - Do not throw an IOException (it is not allowed here)
		// - Do not take any action if the stream is closed
		// - Also reimplement markSupported() and reset()
	}
	
	/**
		Repositions this stream to the most recent valid mark.
		Some part of the following is applicable to an implementation of this method; edit as necessary.
		(Only if markSupported() returns true)
		- If {@link #mark} has not been called since this stream was created, an {@link IOException} is thrown.
		- If {@link #mark} has not been called since this stream was created, the position is returned to the beginning of the data.
		- If the number of bytes read since the last call to {@link #mark} is larger than the {@code readlimit} argument of that call, an {@link IOException} is thrown.
		If this call does not throw an {@link IOException}, then the position is reverted to the position at the time of the last call to {@link #mark} (or, if {@link #mark} has not been called, to the beginning of the data).
		(Only if markSupported() returns false)
		- Calling this method throws an {@link IOException}.
		- Calling this method results in the position being changed to ___.
		@throws	IOException	(reasons including)
							or if {@link #mark} has not been called since this stream was created,
							or if the number of bytes read since the last call to {@link #mark} is larger than the {@code readlimit} argument of that call,
							or always
		@see	mark
	**/
	public void reset()
		throws IOException
	{
		// TODO: Implement if desired
		
		// This implementation merely indicates that this operation
		// is not supported.
		
		// If modified:
		// - Also reimplement markSupported() and mark()
		
		throw new IOException("Reset is not supported");
	}
	
	/**
		Returns {@code true} if and only if this input stream object
		supports {@link #mark} and {@link #reset}.
		@return	{@code true} if this object supports {@link #mark} and
				{@link #reset}, or {@code false} otherwise
		@see	mark
		@see	reset
	**/
	public boolean markSupported()
	{
		// TODO: Implement if desired
		
		// If modified:
		// - Note that the return value must be invariant for the
		//	lifetime of the object
		
		return false;
	}
}

Java: Genericized Empty Singleton

Creates a global singleton representing an empty object which, having no elements of type T, doesn't actually need a separate implementation for each type. Instead, a single instance is created and then repurposed as necessary using an unchecked cast.

Inspired by the implementation of Collections.emptyList() and the initialization-on-demand holder idiom.


public class Dingus<T> {
	// A method to override
	public boolean isEmpty() {
		return false;
	}

	private static final class EmptyDingus<T> extends Dingus<T> {
		private EmptyDingus() {}

		private static final class InstanceHolder {
			private static final EmptyDingus<Object> INSTANCE = new EmptyDingus<Object>();
		}

		@SuppressWarnings("unchecked")
		static <T> EmptyDingus<T> getInstance() {
			return (EmptyDingus<T>) InstanceHolder.INSTANCE;
		}

		// Overridden methods

		@Override
		public boolean isEmpty() {
			return true;
		}
	}

	public static <T> Dingus<T> empty() {
		return EmptyDingus.getInstance();
	}
}

JavaScript: Binary search, uniq

See JavaScript microlibs.

Java/C-like: Data-byte conversions

See Java Data-Byte Conversions.

Java: Retrieve unsigned long as BigInteger

	static java.math.BigInteger unsignedLong(long n)
	{
		java.math.BigInteger b = java.math.BigInteger.valueOf(n & 0x7FFFFFFFFFFFFFFFL);
		return ((n & 0x8000000000000000L) != 0) ? b.setBit(63) : b;
	}

Java: Convert int array to byte array

	static byte[] intArrayAsByteArray(int[] n)
	{
		byte[] b = new byte[n.length];
		for(int i = 0; i < n.length; ++i)
			b[i] = (byte)(n[i]);
		return b;
	}

Perl/C-like: Convert between Unicode codepoints and UTF-16 characters

# Perl

# This function accepts one integer as a codepoint
# and produces 1 character for U+0..U+FFFF or
# 2 characters representing the surrogate pair for
# U+10000 to U+10FFFF.
sub codepoint_to_utf16($) {
	my($v) = int($_[0]);
	if($v < 0 or $v > 0x10FFFF) {
		croak "Codepoint $v is out of valid range";
	}
	
	# Single char
	if($v <= 0xFFFF) {
		return ($v);
	}
	
	# Reduce to 20 bits
	$v -= 0x10000;
	
	# Get high and low 10 bits (0x3ff is low 10 bits)
	# OR high with 0xD800
	# OR low with 0xDC00
	return (
		(($v >> 10) & 0x3ff) | 0xD800,
		(($v      ) & 0x3ff) | 0xDC00
	);
}

# This function accepts two integers as UTF-16
# surrogates. The first must be a high surrogate
# and the second must be a low surrogate. The
# function returns the codepoint represented by
# the pair.
sub utf16_to_codepoint {
	my($hi,$lo) = (int($_[0]), int($_[1]));
	if($hi < 0xD800 or $hi > 0xDBFF) {
		croak "Char $hi is not a high surrogate";
	}
	elsif($lo < 0xDC00 or $lo > 0xDFFF) {
		croak "Char $lo is not a low surrogate";
	}

	return 0x10000 + ((($hi & 0x3ff) << 10) | ($lo & 0x3ff));
}

Perl (one-liner): Generate a string of random characters

perl -e '
	$count = 24;
	@alphabet = ("A".."Z","a".."z","0".."9","!".."/",":".."\@","[".."`","{".."~");
	print $alphabet[int(rand(scalar @alphabet))] for 1..$count;
	print "\n";
'

Perl: Using File::Find with less outdated methodologies than find2perl

#! /usr/bin/perl

use 5.010;
use warnings;
use strict;
use Carp;

use File::Find ();

sub do_find(&;@) {
	my $sub = shift;
	my @locations = @_;
	push @locations, '.' unless @locations;
	
	return File::Find::find({wanted => sub {
		$sub->(
			parent_dir => $File::Find::dir,
			file_full_path => $File::Find::name,
			file_name => $_
		);
	}}, @locations);
}

do_find {
	my %info = @_;
	# Tests file named by $_
	return unless -f -r -w;
	say "File: $_ ($info{file_full_path})";		
};

Boilerplate classes

Perl

# Copied from perltoot

package PackageName;
	
use 5.010;
use warnings;
use strict;
use Carp;

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = {};
	$self->{NAME}   = undef;
	$self->{AGE}    = undef;
	$self->{PEERS}  = [];
	bless ($self, $class);
	return $self;
}

1;

Arduino/C++ class using opaque pointer

The "pimpl" idiom is used to limit the details in the header to the outward interface while keeping most implementation details inside the source file. The outward class is implemented entirely in terms of calls to an object of a class only instantiable within the file.

OutwardClass.h

#ifndef OutwardClass_h
#define OutwardClass_h

class OutwardClass {
  public:
    OutwardClass();
    ~OutwardClass();
    void foo();
    int bar(int a, int b);
  private:
    void * _impl;
};

#endif // OutwardClass_h

OutwardClass.cpp

#include "OutwardClass.h"

namespace {

class Impl {
  private:
    // ...

  public:
    Impl() {
      // ...
    }

    ~Impl() {
      // ...
    }

    void foo() {
      // ...
    }

    int bar(int a, int b) {
      // ...
    }
};

}


// Define OutwardClass in terms of Impl

#define IMPL ((Impl*)_impl)

OutwardClass::OutwardClass() {
  _impl = new Impl();
}

OutwardClass::~OutwardClass() {
  delete IMPL;
}

void OutwardClass::foo() {
  IMPL->foo();
}

int OutwardClass::bar(int a, int b) {
  return IMPL->bar(a, b);
}

Boilerplate programs

C

#include <stdio.h> // *printf
#include <stdlib.h> // strto*, rand, {m,re,ca}lloc, exit, ...
#include <string.h> // memcpy, strncpy, memset, strlen, ...
#include <math.h>
#include <stdint.h> // int*_t, uint*_t

int main(int argc, char** argv) {
	printf("Hello.\n");
	return 0;
}

(end)