Skip to content

Instantly share code, notes, and snippets.

View xsleonard's full-sized avatar

Steve Leonard xsleonard

View GitHub Profile
@xsleonard
xsleonard / BarLineChartViewBase+Extensions.swift
Created October 17, 2020 10:04
Zoom into a specific x-axis range for iOS Charts
// For library: https://github.com/danielgindi/Charts
extension BarLineChartViewBase {
func focusXRange(start: Double, end: Double) {
guard end > start else {
return
}
// Reset the zoom back to the full chart view, so that the setVisibleXRange call will zoom in to the desired range,
// setVisibleXRange only zooms in, not out.
@xsleonard
xsleonard / array-binary-search.swift
Created September 10, 2020 02:59
Binary search for Swift arrays
extension Array where Element: Comparable {
// Returns the location of the value in the array,
// or where you should insert the value into the array.
// If the value does not exist in the array, the Bool return value is false.
func binarySearch(value: Element) -> (Index, Bool) {
guard !isEmpty else {
return (0, false)
}
if value > self.last! {
@xsleonard
xsleonard / UUID+Extensions.swift
Created August 26, 2020 06:51
Swift: Convert UUID to and from Data and Int64
extension UUID {
// UUID is 128-bit, we need two 64-bit values to represent it
var integers: (Int64, Int64) {
var a: UInt64 = 0
a |= UInt64(self.uuid.0)
a |= UInt64(self.uuid.1) << 8
a |= UInt64(self.uuid.2) << (8 * 2)
a |= UInt64(self.uuid.3) << (8 * 3)
a |= UInt64(self.uuid.4) << (8 * 4)
a |= UInt64(self.uuid.5) << (8 * 5)
@xsleonard
xsleonard / Int64+Extensions.swift
Created May 22, 2020 10:41
Distance between Int64 in Swift
extension Int64 {
/*
Returns the absolute distance between two Int64 values as a UInt64.
A UInt64 is required to represent the range of possible distance values.
*/
func distanceTo(_ other: Int64) -> UInt64 {
if self == other {
return 0
}
if self > other {
@xsleonard
xsleonard / deploy.sh
Created January 6, 2020 12:40
Hugo + Github Pages deploy.sh modified for --cleanDestinationDir behavior that works with git submodules
#!/bin/sh
# Original: https://gohugo.io/hosting-and-deployment/hosting-on-github/#put-it-into-a-script
# If a command fails then the deploy stops
set -e
printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"
# Clean the public folder
@xsleonard
xsleonard / 00-project-layout.txt
Last active March 14, 2024 02:01
Ansible role to create a new user and disable root
playbook
├── roles
│   ├── createuser
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │   └── sudoers.j2
│   └── maybecreateuser
│   └── tasks
│   └── main.yml

Keybase proof

I hereby claim:

  • I am xsleonard on github.
  • I am sleonard (https://keybase.io/sleonard) on keybase.
  • I have a public key ASAh6EnczT-cqdcVIQM4NoQ8dxy-9PQoLcVO-NIyMI37hgo

To claim this, I am signing this object:

-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26")
PROCESSOR: x86_64
Machine: x86_64-linux-gnu
-- Target host is 64 bit
-- Looking for include file stdint.h
-- Looking for include file stdint.h - found
-- Looking for include file stdlib.h
-- Looking for include file stdlib.h - found
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
@xsleonard
xsleonard / phoneperms.py
Created January 11, 2014 19:00
Phone number -> letter permutations
from sys import argv
from string import ascii_uppercase
def map_number(number):
""" Maps a number's digits to its possible phone letters """
sets = []
for n in str(number):
assert n != '0'
pos = (int(n) - 1) * 3
@xsleonard
xsleonard / gist:7341172
Created November 6, 2013 18:10
hex string to byte array, C
unsigned char* hexstr_to_char(const char* hexstr)
{
size_t len = strlen(hexstr);
IF_ASSERT(len % 2 != 0)
return NULL;
size_t final_len = len / 2;
unsigned char* chrs = (unsigned char*)malloc((final_len+1) * sizeof(*chrs));
for (size_t i=0, j=0; j<final_len; i+=2, j++)
chrs[j] = (hexstr[i] % 32 + 9) % 25 * 16 + (hexstr[i+1] % 32 + 9) % 25;
chrs[final_len] = '\0';