Skip to content

Instantly share code, notes, and snippets.

@stevebrun
stevebrun / iexp.c
Last active January 7, 2024 06:24
Get an integer mantissa for floating-point values in C
#include "iexp.h"
#include <float.h>
#include <math.h>
#include <stddef.h>
// https://stackoverflow.com/questions/7812044/finding-trailing-0s-in-a-binary-number/36791297#36791297
// Manually get two's compliment with (~integer + 1) instead of arithmatic
// negation because signed integer representation is implementation-defined
// behavior, so we can't always assume that negative integers are represented
// with two's complement instead of one's complement or signed magnitude.
@stevebrun
stevebrun / ConstantClass.m
Last active December 7, 2023 17:59
Compile-time constant classes in Objective-C
//
// ConstantClass.m
//
// An example for how to create compile-time constant Objective-C objects that
// can be assigned to any static const variable.
//
// Compile with -fno-objc-arc to allow overriding of retain/release methods.
//
#import <Foundation/Foundation.h>
@stevebrun
stevebrun / firefox.sh
Last active April 14, 2024 10:18
Launch Firefox from Steam Deck's "Gaming Mode".
#!/bin/bash
##
## Launch Firefox from Steam Deck's "Gaming Mode".
##
## Although it doesn't provide a perfect browsing experience, this script
## at least makes sure that menus and windows appear as they're supposed to.
##
## INSTRUCTIONS:
##
## - Save this file to the ~/.local/bin directory.
@stevebrun
stevebrun / desktopmode.sh
Last active November 27, 2022 20:08
Launch Steam Desk's "Desktop Mode" from within "Gaming Mode"
#!/bin/bash
#
# Launch Steam Desk's "Desktop Mode" from within "Gaming Mode".
# https://www.reddit.com/r/SteamDeck/comments/wycp1r/comment/ilw1cov/
# https://www.reddit.com/user/FineWolf/
# Friday, August 26, 2022
#
# Commented by Steven Brunwasser
# Sunday, October 23, 2022
#
@stevebrun
stevebrun / slideslive-dl.swift
Last active November 28, 2021 19:38
Download the HOPL IV sessions from SlidesLive
#!/usr/bin/env swift
//
// Copyright 2021 Steven Brunwasser
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

Keybase proof

I hereby claim:

  • I am stevebrun on github.
  • I am sbrun (https://keybase.io/sbrun) on keybase.
  • I have a public key ASDdAuz7l8NXr4dTieWLCCHVHVrxezxED0cYXn5kPvymyAo

To claim this, I am signing this object:

@stevebrun
stevebrun / CycleIterator.swift
Created April 17, 2018 04:58
An iterator to endlessly iterate over the same sequence.
struct CycleIterator<Element>: IteratorProtocol, ExpressibleByArrayLiteral {
private var elements: [Element]
private var offset: Int = 0
init(_ elements: [Element]) {
self.elements = elements
}
init(arrayLiteral elements: Element...) {
self.init(elements)
@stevebrun
stevebrun / Zip3Sequence.swift
Created April 15, 2018 06:19
An implementation of zip that combines the elements of three sequences.
import Foundation
struct Zip3Sequence<Sequence1, Sequence2, Sequence3>: Sequence
where Sequence1: Sequence, Sequence2: Sequence, Sequence3: Sequence {
typealias Element = Iterator.Element
typealias Iterator = Zip3Iterator<Sequence1.Iterator, Sequence2.Iterator, Sequence3.Iterator>
private var s1: Sequence1
private var s2: Sequence2
private var s3: Sequence3
@stevebrun
stevebrun / reverse-utf8-string.c
Created February 1, 2018 02:13
The proper way to reverse the code points in a UTF-8 encoded string.
#include <stdio.h>
#include <assert.h>
/// Count the amount of bytes contained within a null-terminated string.
/// - returns: The amount of bits before the null-terminator in the given string.
int string_length(char const *const string);
/// Reverse an array of bytes in-place.
/// - parameter bytes: A buffer of bytes to be reversed.
/// - parameter length: The amount of bytes contained within the buffer that should be reversed.
@stevebrun
stevebrun / NiceConstraints.swift
Last active July 18, 2017 23:57
AutoLayout using Operators
/// - note: The equivalence operators don't work because `NSObject` implements `Equatable`.
import UIKit
// MARK: Constraint From Anchor
public func == <AnchorType>(constrained: NSLayoutAnchor<AnchorType>, reference: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint {
return constrained.constraint(equalTo: reference)
}