If nobody can provide a response by the time I'm home tonight, I'll dig up some of my past code and see of it helps. I was the senior network engineer at an ISP for many years, and had to write code to carve up my /32 into efficient chunks that I could document and test with.