Skip to content

Instantly share code, notes, and snippets.

@DavidJCobb
DavidJCobb / README.md
Last active March 24, 2025 04:01
A history of assertions

What are assertions?

Assertions are a means to verify that program invariants have not been violated. Put simply: you assert that impossible things are not happening, and if that assertion ever fails — if the impossible is happening — then your program should halt execution immediately, generally for two reasons:

  • If your program has done something that should (if your program is properly written) be logically impossible, then you will likely only detect the problem after the fact. You don't know what went wrong, you don't know how it went wrong, and you don't know the scope of the damage, and this means both that your program can't reliably correct the error and that your program can no longer trust itself at all. The least dangerous thing to do is stop executing immediately — crash, on purpose, before your program can do anything else that shouldn't be possible.

  • Depending on your choice of programming language and environment, halting execution may give you an opportunity t

@DavidJCobb
DavidJCobb / README.md
Created February 24, 2025 07:09
Ascii85 (base-85) codec in JavaScript

Ascii85, also known as base85, is an encoding similar in concept to base64. Where base64 uses four ASCII characters to represent three source bytes (thereby inflating the data size by 33%), ascii85 uses five ASCII characters to represent four source bytes (thereby inflating the data size by 25%).

This script can be used to encode and decode a DataView as ascii85. Spawn an Ascii85Codec with the desired configuration, and then call its member functions as appropriate. In particular, this class should be appropriate for storing a DataView inside of Local Storage with the smallest possible size (if you use the STORAGE_CHARSET offered).

Note that we do not perform any error-checking during the decode step. Ascii85Codec offers a validate member function that can be run on an encoded string to verify that it contains no illegal characters; this can be run prior to decoding in any situation where the input data is untrusted.

Implementation notes

  • The `S
@DavidJCobb
DavidJCobb / README.md
Created July 13, 2024 03:58
The Old New Thing - userscript for fixes

Userscript to fix issues on Raymond Chen's blog, and improve the browsing experience:

  • Redirect old-format links within the blog as close to their present-day destinations as possible
  • Add pagination to the top, and allow pagination between months (helps when reading in chronological order, month by month, on mobile)

Might expand this into a full WebExtension later? There are some articles that used to have inline JavaScript which now just renders as plain-text; recreating these scripts within a WebExtension (no eval!!) could be useful.

@DavidJCobb
DavidJCobb / README.md
Created March 13, 2023 11:38
JavaScript event bubbling: simple example

JavaScript event bubbling example

When JavaScript fires an event, like click, it doesn't just fire it on the event target (i.e. the thing that was clicked). It also fires the event on every ancestor element. This means that an event listener on an ancestor can handle clicks on any of its children.

The code below demonstrates this with a list, where we want to react to the user clicking any list item, and we want our reaction to take into account which list item they clicked. We use a listener on the entire list, and just check e.target to see what list item was clicked.

@DavidJCobb
DavidJCobb / README.md
Last active January 11, 2024 11:02
JavaScript: using a Proxy to create const references to an object (i.e. `const Object&`)

The as_const function returns a Proxy wrapping the passed-in argument. This proxy attempts to prevent all modifications to the underlying object. The difference between this and using Object.freeze is that the latter makes the object itself immutable, while as_const simply prevents direct changes to the object via a specific reference to the object. Additionally, as_const prevents changes to any object that is referenced through the proxy (i.e. as_const(foo).bar.data = 5 is equivalent to as_const(foo.bar).data = 5 and will throw an exception).

In C++ terms, Object.freeze is akin to instantiating a const Object, whereas the result of as_const is a const Object& and the original object can still be modified elsewhere.

This idea is leaky enough that I would recommend against using it. If you need functionality like this, prefer a language that offers it reliably as a built-in feature.

@DavidJCobb
DavidJCobb / regex.js
Created January 23, 2022 21:02
Helper for defining regexes
//
// This file contains a JavaScript tag function to let you
// define regexes without needing to backslash-escape every
// forward slash in the regex text; good for URLs.
//
function regex(tag) {
return new RegExp(tag.raw, "i");
}
@DavidJCobb
DavidJCobb / QPlainTextEdit maxlength implementation.cpp
Last active September 8, 2021 18:49
QPlainTextEdit maxlength implementation
// given:
QPlainTextEdit* widget;
int maxlength;
auto* doc = widget->document();
assert(doc);
QObject::connect(doc, &QTextDocument::contentsChange, this, [maxlength, widget](int pos, int removed, int added) {
if (added - removed <= 0)
return;
if (maxlength < 0)
@DavidJCobb
DavidJCobb / CobbMapHelper.lua
Created July 19, 2019 22:22
ESO Experiment/Rough Draft - Building and Rendering a Heightmap
--
-- This two-file add-on will track your position every tenth of a second
-- (or thirtieth when mounted, if I got the update re-registration right)
-- and maintain a grid of where you've been. Every fifth of a second, it
-- redraws the grid using Texture controls rendered in 3D as gridlines.
--
-- ISSUES:
--
-- - Performance (see below comments)
--
@DavidJCobb
DavidJCobb / TES5Edit-count-persistent-refs.psc
Created April 20, 2019 05:27
Skyrim - xEdit - Count the number of persistent references in all worldspaces in a file
Unit CobbCountPersistents;
Var
giCount: Integer;
Function Initialize: Integer;
Var
eFile: IInterface;
eWorlds: IInterface;
eWorld: IInterface;
@DavidJCobb
DavidJCobb / AHK - Make Windows notifications click-through-able.ahk
Last active August 29, 2020 07:11
Tired of Windows 10 notifications blocking access to toolbars, buttons, and Twitch chat textboxes in the lower-right corner of the screen? Sick of how the darn things block an invisible strip of space above them, too? This should fix that.
;
; Make Windows notifications semitransparent, and allow the user to click through
; all but their rightmost portion (so as to keep the close/dismiss button usable).
;
#Persistent
#SingleInstance
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%