Fun with Rails' blank? function and invisible characters
Posted May 2019

I recently came across a weird bug in a project I was working on, and the tl;dr of it is I had U+FEFF in a text string, more commonly known as zero width no-break space or a Byte Order Mark. It was messing with a string.blank? check.

It all started when I had some blank text and it was failing the string.blank? check. For those of you who don't know, in Ruby on Rails, the object.blank? is basically a .nil? and an .empty? wrapped all into one... or so I though. I went into the database where my string lived, and copied it out.

puts "   \n   \n".blank?

My output to my surprise was false. Scratching my head, I made some additional test cases to see how blank? behaved:

puts "       ".blank?
puts "\n\n\n\n\n\n".blank?

And to my expectation all of these returned true. Now I was really puzzled. I took a look at the string in hex form, and thats when I found the culprit. The U+FEFF character got in there somehow and my otherwise blank string was now returning false. The string was actually \uFEFF \n \n"! A-ha!

Since nbsp or a Non-breaking space maps to U+00A0, it is considered whitespace and captured by the empty regex. I checked a bunch of other UTF non-breaking spaces, such as U+202F (Narrow no-break space) and U+2007 (Figure space), and decided I was just going to extend/wrap blank? to also cover my invisible BOM case.

I originally took the naive approach of just deleting the BOM character from the string and performing the check again, which resulted in this monstrosity.

def blank_or_invisible?(string)
  return string.blank? || string.delete(/\uFEFF/).blank?
end

Running some perf tests on the above yielded some gross results:


puts Benchmark.measure {
  50_000.times do
    blank_or_invisible?("\uFEFF\uFEFF\uFEFF\uFEFF \n\n")
  end
}

puts "blank?"
puts Benchmark.measure {
  50_000.tim
Click here to read the full post
Setting up a Raspberry Pi Zero/W without a screen!
Posted August 2017

I recently purchased a Raspberry Pi Zero W (Pi Zero with WiFi/Bluetooth), and I wanted to set it up with just my laptop (I didn't have access to a HDMI cable, keyboard, mouse, etc... Here are the steps I took to set it up.

  1. Obtain the necessary hardware:
  2. Download Raspbian and flash it on the MicroSD Card
    • Download Raspbian Stretch with Desktop here
    • Flash the image according to your OS's instructions here
  3. Remount your SD card, and modify some files to enable USB-Ethernet and SSH
    • Navigate to <sdcard_base_dir>/boot
    • Modify the following files:
      • config.txt: Add dtoverlay=dwc2 to the bottom of the file, and save it.
      • cmdline.txt: Modify the existing line in this file and add modules-load=dwc2,g_ether after rootwait. Note the space in-between the two. It should look similar to this afterwards: dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet init=/usr/lib/raspi-config/init_resize.sh. Save it.
      • Add a new file called ssh in this directory. This will enable SSH when the raspberry pi boots, so we can log into it!
  4. Safely eject the SD card, and put it into your raspberry pi.
  5. Plug the USB port labeled PWR into a USB power source (any phone charger at 5v and 1.5A should be fine)
  6. Plug the USB port labeled USB into your PC. Wait up to 90 seconds to make sure the Pi has finished booting.
  7. Make sure Bonjour is installed, for pi auto-discovery.
    • Windows: Download Bonjour and install it.
    • OSX: Bonjour comes pre-installed
    • Linux: For any debian-based systems (Debian/Kali/Ubuntu/Mint/etc..
Click here to read the full post
Welcome to my Blog
Posted October 2016

Welcome! I have just created this blog, so please ignore the test code that may come and go. I hope to use this blog to post interesting tidbits about programming, life, and more!