I have recently been learning and enjoying the Vala programming language. I am writing a lightweight markdown-to-PDF converter and wanted to be able to automatically number list items in Roman numerals. Here, in case anyone wants it, is Knuth’s algorithm for producing the Roman numeral for a number. I converted this to Vala from the original WEB source, part of TeX, as quoted by Hans Wennborg. Enjoy!
string roman(uint num) { // Knuth's algorithm for Roman numerals, from TeX. Quoted by // Hans Wennborg at https://www.hanshq.net/roman-numerals.html. // Converted to Vala by Chris White (github.com/cxw42). CC-BY 4.0 Intl. var sb = new StringBuilder(); string control = "m2d5c2l5x2v5i"; int j, k; // mysterious indices into `control` uint u, v; // mysterious numbers j = 0; v = 1000; while(true) { while(num >= v) { sb.append_c(control[j]); num -= v; } if(num <= 0) { // nonpositive input produces no output break; } k = j+2; u = v / control[k-1].digit_value(); if(control[k-1] == '2') { k += 2; u /= control[k-1].digit_value(); } if(num+u >= v) { sb.append_c(control[k]); num += u; } else { j += 2; v /= control[j-1].digit_value(); } } return sb.str; } // roman()
(not extensively tested — use at your own risk. No warranty. License details here.)