Skip to content

Instantly share code, notes, and snippets.

# Lil loading tool for JWCC: JSON With Commas and Comments.
# See docstrings for loads() and jwcc2json() for more info.
# Public domain.
import json
import re
_INTERESTING_B = re.compile(rb'''
\] # array end
| \} # object end
| (?<! \\)" # string start
@nolanw
nolanw / URLRequest+MultipartFormData.swift
Last active October 5, 2023 14:31
Swift multipart/form-data
// Public domain - https://gist.github.com/nolanw/dff7cc5d5570b030d6ba385698348b7c
import Foundation
extension URLRequest {
/**
Configures the URL request for `multipart/form-data`. The request's `httpBody` is set, and a value is set for the HTTP header field `Content-Type`.
- Parameter parameters: The form data to set.
@nolanw
nolanw / URLRequest+FormURLEncoded.swift
Last active May 11, 2023 20:38
Swift x-www-form-urlencoded
// Public domain - https://gist.github.com/nolanw/14b277903a2ba446f75202a6bfd55977
import Foundation
extension URLRequest {
/**
Configures the URL request for `application/x-www-form-urlencoded` data. The request's `httpBody` is set, and values are set for HTTP header fields `Content-Type` and `Content-Length`.
- Parameter queryItems: The (name, value) pairs to encode and set as the request's body.
@nolanw
nolanw / crashlytics-download.py
Created October 30, 2016 19:21
Script to download/update Crashlytics.framework and Fabric.framework without having to install Fabric.app or use CocoaPods
#!/usr/bin/env python
from __future__ import print_function
import json
from io import BytesIO
import logging
import os
import sys
try:
from urllib.request import Request, urlopen # py3
from urllib.parse import quote
@nolanw
nolanw / pbxproj-lint.swift
Created October 26, 2016 13:09
Xcode project file plist linter
#!/usr/bin/env swift
// Swift 3.0
import Foundation
func main() -> Int32 {
if CommandLine.arguments.count < 2 {
print("")
print("Usage: \(CommandLine.arguments[0]) (path_to_pbxproj | path_to_xcodeproj)")
@nolanw
nolanw / AttributedNumberFormatter.swift
Last active February 22, 2019 15:16
An NSNumberFormatter for iOS that usefully implements attributedStringForObjectValue…
import Foundation
/// A number formatter that usefully implements `attributedString(for:withDefaultAttributes:)`, which iOS's NumberFormatter does not.
final class AttributedNumberFormatter: NumberFormatter {
override func attributedString(for obj: Any, withDefaultAttributes defaultAttributes: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString? {
guard
let number = obj as? NSNumber,
let plain = string(from: number)
else { return nil }
@nolanw
nolanw / isequal.m
Created September 27, 2011 02:02
Timing isEqual: and isEqualToString:
// clang -framework Foundation isequal.m
#include <mach/mach_time.h>
#include <stdint.h>
#include <stdio.h>
#import <Foundation/Foundation.h>
// Convert from mach time to seconds (gets set in main() below).
double ticksToNanos = 0;
// Run some code many times, timing how long it took, and announce the average
@nolanw
nolanw / fscript.rb
Last active September 26, 2015 22:38
Inject F-Script into any app on OS X 10.7 or 10.8. (Yet another F-Script Anywhere replacement.)
#!/usr/bin/env ruby
FSCRIPT_PATH = "/Library/Frameworks/FScript.framework"
if ARGV.empty?
puts "Usage: #{$0} process_name"
exit
end
GDB = IO.popen("gdb", 'w')
@nolanw
nolanw / gist:954361
Created May 3, 2011 22:04
TextMate bundle command to nicely wrap contiguous single-line comments
#!/usr/bin/env ruby
def wrap text, sigil, indent
text = text.map do |line|
line.match(/^\s*\S (.*)$/)[1]
end.join ' '
lines = []
while text and not text.empty?
cut = [text.length, 78 - indent].min
if cut < text.length and text[cut, 1] != ' '
@nolanw
nolanw / c99_runtime_check.c
Created April 2, 2011 19:21
Check for at least C99 at runtime.
static int IsC99()
{
return (2//* */2
== 2);
}