Skip to content
Snippets Groups Projects
Commit 265a979b5b49 authored by Nick Wellnhofer's avatar Nick Wellnhofer
Browse files

Port genChRanges.py to Python 3

parent 26176bd5bb02
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,6 @@
#
import sys
import string
import time
#
......@@ -57,7 +56,7 @@
try:
defines = open("chvalid.def", "r")
except:
print "Missing chvalid.def, aborting ..."
print("Missing chvalid.def, aborting ...")
sys.exit(1)
#
......@@ -72,8 +71,8 @@
# ignore blank lines, or lines beginning with '#'
if line[0] == '#':
continue
line = string.strip(line)
line = line.strip()
if line == '':
continue
# split line into space-separated fields, then split on type
try:
......@@ -76,8 +75,8 @@
if line == '':
continue
# split line into space-separated fields, then split on type
try:
fields = string.split(line, ' ')
fields = line.split(' ')
#
# name line:
# validate any previous function block already ended
......@@ -88,7 +87,7 @@
if fields[0] == 'name':
name = fields[1]
if state != 0:
print "'name' %s found before previous name" \
"completed" % (fields[1])
print("'name' %s found before previous name" \
"completed" % (fields[1]))
continue
state = 1
......@@ -93,8 +92,8 @@
continue
state = 1
if Functs.has_key(name):
print "name '%s' already present - may give" \
" wrong results" % (name)
if name in Functs:
print("name '%s' already present - may give" \
" wrong results" % (name))
else:
# dict entry with two list elements (chdata, rangedata)
Functs[name] = [ [], [] ]
......@@ -107,7 +106,7 @@
#
elif fields[0] == 'end':
if state == 0:
print "'end' found outside of function block"
print("'end' found outside of function block")
continue
state = 0
......@@ -119,5 +118,5 @@
#
elif fields[0] == 'ur':
if state != 1:
raise ValidationError, "'ur' found outside of 'name' block"
raise Exception("'ur' found outside of 'name' block")
for el in fields[1:]:
......@@ -123,5 +122,5 @@
for el in fields[1:]:
pos = string.find(el, '..')
pos = el.find('..')
# pos <=0 means not a range, so must be individual value
if pos <= 0:
# cheap handling of hex or decimal values
......@@ -132,8 +131,8 @@
else:
value = int(el)
if ((value < 0) | (value > 0x1fffff)):
raise ValidationError, 'Illegal value (%s) in ch for'\
' name %s' % (el,name)
raise Exception('Illegal value (%s) in ch for'\
' name %s' % (el,name))
# for ur we have only ranges (makes things simpler),
# so convert val to range
currange = (value, value)
......@@ -141,7 +140,7 @@
# the interval
else:
# split the range into it's first-val, last-val
(first, last) = string.split(el, "..")
(first, last) = el.split("..")
# convert values from text into binary
if first[0:2] == '0x':
start = int(first[2:],16)
......@@ -156,7 +155,7 @@
else:
end = int(last)
if (start < 0) | (end > 0x1fffff) | (start > end):
raise ValidationError, "Invalid range '%s'" % el
raise Exception("Invalid range '%s'" % el)
currange = (start, end)
# common path - 'currange' has the range, now take care of it
# We split on single-byte values vs. multibyte
......@@ -165,7 +164,7 @@
# validate that value not previously defined
if Functs[name][0][ch]:
msg = "Duplicate ch value '%s' for name '%s'" % (el, name)
raise ValidationError, msg
raise Exception(msg)
Functs[name][0][ch] = 1
else: # multi-byte
if currange in Functs[name][1]:
......@@ -169,9 +168,9 @@
Functs[name][0][ch] = 1
else: # multi-byte
if currange in Functs[name][1]:
raise ValidationError, "range already defined in" \
" function"
raise Exception("range already defined in" \
" function")
else:
Functs[name][1].append(currange)
except:
......@@ -174,8 +173,8 @@
else:
Functs[name][1].append(currange)
except:
print "Failed to process line: %s" % (line)
print("Failed to process line: %s" % (line))
raise
#
# At this point, the entire definition file has been processed. Now we
......@@ -194,9 +193,9 @@
try:
header = open("include/libxml/chvalid.h", "w")
except:
print "Failed to open include/libxml/chvalid.h"
print("Failed to open include/libxml/chvalid.h")
sys.exit(1)
try:
output = open("chvalid.c", "w")
except:
......@@ -198,9 +197,9 @@
sys.exit(1)
try:
output = open("chvalid.c", "w")
except:
print "Failed to open chvalid.c"
print("Failed to open chvalid.c")
sys.exit(1)
date = time.asctime(time.localtime(time.time()))
......@@ -236,10 +235,10 @@
typedef struct _xmlChSRange xmlChSRange;
typedef xmlChSRange *xmlChSRangePtr;
struct _xmlChSRange {
unsigned short low;
unsigned short high;
unsigned short\tlow;
unsigned short\thigh;
};
typedef struct _xmlChLRange xmlChLRange;
typedef xmlChLRange *xmlChLRangePtr;
struct _xmlChLRange {
......@@ -241,12 +240,12 @@
};
typedef struct _xmlChLRange xmlChLRange;
typedef xmlChLRange *xmlChLRangePtr;
struct _xmlChLRange {
unsigned int low;
unsigned int high;
unsigned int\tlow;
unsigned int\thigh;
};
typedef struct _xmlChRangeGroup xmlChRangeGroup;
typedef xmlChRangeGroup *xmlChRangeGroupPtr;
struct _xmlChRangeGroup {
......@@ -248,15 +247,15 @@
};
typedef struct _xmlChRangeGroup xmlChRangeGroup;
typedef xmlChRangeGroup *xmlChRangeGroupPtr;
struct _xmlChRangeGroup {
int nbShortRange;
int nbLongRange;
const xmlChSRange *shortRange; /* points to an array of ranges */
const xmlChLRange *longRange;
int\t\t\tnbShortRange;
int\t\t\tnbLongRange;
const xmlChSRange\t*shortRange;\t/* points to an array of ranges */
const xmlChLRange\t*longRange;
};
/**
* Range checking routine
*/
XMLPUBFUN int XMLCALL
......@@ -257,11 +256,11 @@
};
/**
* Range checking routine
*/
XMLPUBFUN int XMLCALL
xmlCharInRange(unsigned int val, const xmlChRangeGroup *group);
\t\txmlCharInRange(unsigned int val, const xmlChRangeGroup *group);
""" % (date, sources));
output.write(
"""/*
......@@ -264,9 +263,9 @@
""" % (date, sources));
output.write(
"""/*
* chvalid.c: this module implements the character range
* validation APIs
* chvalid.c:\tthis module implements the character range
*\t\tvalidation APIs
*
* This file is automatically generated from the cvs source
* definition files using the genChRanges.py Python script
......@@ -299,8 +298,7 @@
# compares, otherwise we output a 256-byte table and a macro to use it.
#
fkeys = Functs.keys() # Dictionary of all defined functions
fkeys.sort() # Put some order to our output
fkeys = sorted(Functs.keys())
for f in fkeys:
......@@ -354,7 +352,7 @@
# okay, I'm tired of the messy lineup - let's automate it!
pline = "#define %s_ch(c)" % f
# 'ntab' is number of tabs needed to position to col. 33 from name end
ntab = 4 - (len(pline)) / 8
ntab = 4 - (len(pline)) // 8
if ntab < 0:
ntab = 0
just = ""
......@@ -387,7 +385,7 @@
*/
""" % f)
pline = "#define %sQ(c)" % f
ntab = 4 - (len(pline)) / 8
ntab = 4 - (len(pline)) // 8
if ntab < 0:
ntab = 0
just = ""
......@@ -495,22 +493,22 @@
const xmlChLRange *lptr;
if (rptr == NULL) return(0);
if (val < 0x10000) { /* is val in 'short' or 'long' array? */
if (rptr->nbShortRange == 0)
return 0;
low = 0;
high = rptr->nbShortRange - 1;
sptr = rptr->shortRange;
while (low <= high) {
mid = (low + high) / 2;
if ((unsigned short) val < sptr[mid].low) {
high = mid - 1;
} else {
if ((unsigned short) val > sptr[mid].high) {
low = mid + 1;
} else {
return 1;
}
}
}
if (val < 0x10000) {\t/* is val in 'short' or 'long' array? */
\tif (rptr->nbShortRange == 0)
\t return 0;
\tlow = 0;
\thigh = rptr->nbShortRange - 1;
\tsptr = rptr->shortRange;
\twhile (low <= high) {
\t mid = (low + high) / 2;
\t if ((unsigned short) val < sptr[mid].low) {
\t\thigh = mid - 1;
\t } else {
\t\tif ((unsigned short) val > sptr[mid].high) {
\t\t low = mid + 1;
\t\t} else {
\t\t return 1;
\t\t}
\t }
\t}
} else {
......@@ -516,22 +514,22 @@
} else {
if (rptr->nbLongRange == 0) {
return 0;
}
low = 0;
high = rptr->nbLongRange - 1;
lptr = rptr->longRange;
while (low <= high) {
mid = (low + high) / 2;
if (val < lptr[mid].low) {
high = mid - 1;
} else {
if (val > lptr[mid].high) {
low = mid + 1;
} else {
return 1;
}
}
}
\tif (rptr->nbLongRange == 0) {
\t return 0;
\t}
\tlow = 0;
\thigh = rptr->nbLongRange - 1;
\tlptr = rptr->longRange;
\twhile (low <= high) {
\t mid = (low + high) / 2;
\t if (val < lptr[mid].low) {
\t\thigh = mid - 1;
\t } else {
\t\tif (val > lptr[mid].high) {
\t\t low = mid + 1;
\t\t} else {
\t\t return 1;
\t\t}
\t }
\t}
}
return 0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment