From b8b1a0e539b2b1a08e4c622b60224c2fc23fab05 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 3 Oct 2008 19:21:27 +0000 Subject: [PATCH] Translate to Python. --- utils/codelist | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/utils/codelist b/utils/codelist index 11c6414d04e..ce6be93f2fe 100755 --- a/utils/codelist +++ b/utils/codelist @@ -1,4 +1,4 @@ -#!/usr/bin/perl -n +#!/usr/bin/env python # codelist # given list of integers, one per line, outputs a minimal list of ranges # describing the list @@ -6,19 +6,37 @@ # this is useful for codepoints that are in a font, based on list of codes # output format is suitable for codepoints="" in Wesnoth fonts.cfg -#push @n, hex; -chomp; push @n, $_; -END { - foreach (sort { $a <=> $b } @n) { - if ($_ == $last + 1) { - $last = $_; - } else { - push @r, ($first == $last) ? "$first" : "$first-$last"; - $first = $last = $_; - } - } - push @r, ($first == $last) ? "$first" : "$first-$last"; - shift @r; # get rid of blank first element - print join(",", @r), "\n"; -} +import sys + +def rangeify(lst): + "Turn ranges of adjacent ints in a list into [start, end] list elements." + lst.sort() + lst.append(None) + while True: + for i in range(len(lst)-1): + if type(lst[i]) == type(0) and lst[i+1] == lst[i]+1: + lst = lst[:i] + [[lst[i], lst[i+1]]] + lst[i+2:] + break + elif type(lst[i]) == type([]) and lst[i+1] == lst[i][-1]+1: + lst[i][1] = lst[i+1] + lst = lst[:i+1] + lst[i+2:] + break + else: + break + lst.pop() + return lst + +def printbyrange(lst): + out = "" + for elt in lst: + if type(elt) == type(0): + out += `elt` + "," + else: + out += "%d-%d," % tuple(elt) + return out[:-1] + +codepoints = map(lambda x: int(x.strip()), sys.stdin.readlines()) +print printbyrange(rangeify(codepoints)) + +