Skip to content

Instantly share code, notes, and snippets.

View nh7a's full-sized avatar

Naoki Hiroshima nh7a

View GitHub Profile
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# KEYWORDS=@n,@n1 TWITTER_USERNAME=n TWITTER_PASSWORD=password ruby blockem.rb
require 'rubygems'
require "uri"
require "yajl/http_stream"
def twitter_uri(path, stream = false)
u = ENV['TWITTER_USERNAME']
p = ENV['TWITTER_PASSWORD']
@nh7a
nh7a / population_count_test.c
Created December 13, 2011 09:44
population count test
/*
$ gcc -std=c99 a.c
$ ./a.out 10000000
10000000 bits...
precmp8: 11ms (1250001)
POPCNT: 6ms (1250001)
$ ./a.out 100000000
100000000 bits...
precmp8: 111ms (12500001)
POPCNT: 62ms (12500001)
def has_nothing_to_do_with_n(text):
if type(text) == unicode:
text = text.encode('utf-8')
reason = False
if len(text) < 5:
reason = 'too short'
elif text[2:].startswith(('\xc3','.','!','@','_','-','~','\xe9\x9f\xbf')):
reason = 'not for n'
elif len(text.split('@')) > 5:
reason = '#ff'
@nh7a
nh7a / chrome.patch
Created February 26, 2013 08:23
Using Backspace to Go Back is just ridiculous.
Index: chrome/browser/global_keyboard_shortcuts_mac.mm
===================================================================
--- chrome/browser/global_keyboard_shortcuts_mac.mm (revision 184327)
+++ chrome/browser/global_keyboard_shortcuts_mac.mm (working copy)
@@ -79,8 +79,10 @@
//--- ----- ----- ------
{true, false, false, false, kVK_LeftArrow, 0, IDC_BACK},
{true, false, false, false, kVK_RightArrow, 0, IDC_FORWARD},
+#if USE_IDIOTIC_SHORTCUT_KEYS
{false, false, false, false, kVK_Delete, 0, IDC_BACK},
@nh7a
nh7a / gist:7472056
Created November 14, 2013 18:38
GetSystemUptime()
int32_t GetSystemUptime()
{
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t mib_size = sizeof(mib) / sizeof(int);
struct timeval boottime;
size_t boottime_size = sizeof(boottime);
if (!sysctl(mib, mib_size, &boottime, &boottime_size, NULL, 0)) {
time_t now;
time(&now);
@nh7a
nh7a / NSObject+AssociatedDictionary.h
Created May 28, 2014 02:29
NSObject+AssociatedDictionary
#import <Foundation/Foundation.h>
@interface NSObject (AssociatedDictionary)
+ (NSMutableDictionary*)associatedDictionary;
- (NSMutableDictionary*)associatedDictionary;
@end
@nh7a
nh7a / NumberString.swift
Created June 11, 2014 02:44
property observer bug
class NumberString {
var number = 0
var string: String = "" {
didSet { number = string.toInt()! }
}
init(foo: String) {
string = foo // Will NOT cause didSet
}
@nh7a
nh7a / gist:a287167abc932d85708b
Created July 17, 2014 00:53
auto casting Float to Double
extension Float {
func __conversion() -> Double { return Double(self) }
}
let two = 2.0
let n1 = 1 / 2.0 // Ok
let n2 = Float(1) / 2.0 // Ok
let n3 = Double(1) / 2.0 // Ok
let n4 = CGFloat(1) / 2.0 // Ok
let n5 = 1 / two // Ok
// let qrcodeImage = UIImage(message: "Hello, QRCode", size: 200)
extension UIImage {
// correctionLevel can be L(7%), M(15%), Q(25%), or H(30%)
convenience init?(message: String, size: CGFloat, correctionLevel: String = "M") {
let filter = CIFilter(name: "CIQRCodeGenerator")
filter.setValue(correctionLevel, forKey: "inputCorrectionLevel")
filter.setValue(message.dataUsingEncoding(NSUTF8StringEncoding)!, forKey: "inputMessage")
var uiImage = UIImage(CIImage: filter.outputImage, scale: 1.0, orientation: UIImageOrientation.Up)!
func keyboardWillShow(notification: NSNotification) {
let info = notification.userInfo!
let rect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let opts = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: opts, animations: {
self.bottomConstraint.constant = rect.height
self.view.layoutIfNeeded()