* subversion/libsvn_subr/validate.c
[svn.git] / subversion / libsvn_subr / genctype.py
blob363a6d363a283a59c74c9d7d51ccbfd4238d8406
1 #!/usr/bin/env python
2 """getctype.py - Generate the svn_ctype character classification table.
3 """
5 import string
7 # Table of ASCII character names
8 names = ('nul', 'soh', 'stx', 'etx', 'eot', 'enq', 'ack', 'bel',
9 'bs', 'ht', 'nl', 'vt', 'np', 'cr', 'so', 'si',
10 'dle', 'dc1', 'dc2', 'dc3', 'dc4', 'nak', 'syn', 'etb',
11 'can', 'em', 'sub', 'esc', 'fs', 'gs', 'rs', 'us',
12 'sp', '!', '"', '#', '$', '%', '&', '\'',
13 '(', ')', '*', '+', ',', '-', '.', '/',
14 '0', '1', '2', '3', '4', '5', '6', '7',
15 '8', '9', ':', ';', '<', '=', '>', '?',
16 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
17 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
18 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
19 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
20 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
21 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
22 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
23 'x', 'y', 'z', '{', '|', '}', '~', 'del')
25 # All whitespace characters:
26 # horizontal tab, vertical tab, new line, form feed, carriage return, space
27 whitespace = (9, 10, 11, 12, 13, 32)
29 # Bytes not valid in UTF-8 sequences
30 utf8_invalid = (0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF)
32 print ' /* **** DO NOT EDIT! ****'
33 print ' This table was generated by genctype.py, make changes there. */'
35 for c in xrange(256):
36 bits = []
38 # Ascii subrange
39 if c < 128:
40 bits.append('SVN_CTYPE_ASCII')
42 if len(names[c]) == 1:
43 name = string.center(names[c], 3)
44 else:
45 name = string.ljust(names[c], 3)
47 # Control characters
48 if c < 32 or c == 127:
49 bits.append('SVN_CTYPE_CNTRL')
51 # Whitespace characters
52 if c in whitespace:
53 bits.append('SVN_CTYPE_SPACE')
55 # Punctuation marks
56 if c >= 33 and c < 48 \
57 or c >= 58 and c < 65 \
58 or c >= 91 and c < 97 \
59 or c >= 123 and c < 127:
60 bits.append('SVN_CTYPE_PUNCT')
62 # Decimal digits
63 elif c >= 48 and c < 58:
64 bits.append('SVN_CTYPE_DIGIT')
66 # Uppercase letters
67 elif c >= 65 and c < 91:
68 bits.append('SVN_CTYPE_UPPER')
69 # Hexadecimal digits
70 if c <= 70:
71 bits.append('SVN_CTYPE_XALPHA')
73 # Lowercase letters
74 elif c >= 97 and c < 123:
75 bits.append('SVN_CTYPE_LOWER')
76 # Hexadecimal digits
77 if c <= 102:
78 bits.append('SVN_CTYPE_XALPHA')
80 # UTF-8 multibyte sequences
81 else:
82 name = hex(c)[1:]
84 # Lead bytes (start of sequence)
85 if c > 0xC0 and c < 0xFE and c not in utf8_invalid:
86 bits.append('SVN_CTYPE_UTF8LEAD')
88 # Continuation bytes
89 elif (c & 0xC0) == 0x80:
90 bits.append('SVN_CTYPE_UTF8CONT')
92 if len(bits) == 0:
93 flags = '0'
94 else:
95 flags = string.join(bits, ' | ')
96 print ' /* %s */ %s,' % (name, flags)