##// END OF EJS Templates
charencode: allow clang-format oversight...
Augie Fackler -
r36243:6c87d411 default
parent child Browse files
Show More
@@ -1,58 +1,56 b''
1 # Files that just need to be migrated to the formatter.
1 # Files that just need to be migrated to the formatter.
2 # Do not add new files here!
2 # Do not add new files here!
3 mercurial/cext/base85.c
3 mercurial/cext/base85.c
4 mercurial/cext/charencode.c
5 mercurial/cext/charencode.h
6 mercurial/cext/dirs.c
4 mercurial/cext/dirs.c
7 mercurial/cext/manifest.c
5 mercurial/cext/manifest.c
8 mercurial/cext/mpatch.c
6 mercurial/cext/mpatch.c
9 mercurial/cext/osutil.c
7 mercurial/cext/osutil.c
10 mercurial/cext/revlog.c
8 mercurial/cext/revlog.c
11 # Vendored code that we should never format:
9 # Vendored code that we should never format:
12 contrib/python-zstandard/c-ext/bufferutil.c
10 contrib/python-zstandard/c-ext/bufferutil.c
13 contrib/python-zstandard/c-ext/compressiondict.c
11 contrib/python-zstandard/c-ext/compressiondict.c
14 contrib/python-zstandard/c-ext/compressionparams.c
12 contrib/python-zstandard/c-ext/compressionparams.c
15 contrib/python-zstandard/c-ext/compressionwriter.c
13 contrib/python-zstandard/c-ext/compressionwriter.c
16 contrib/python-zstandard/c-ext/compressobj.c
14 contrib/python-zstandard/c-ext/compressobj.c
17 contrib/python-zstandard/c-ext/compressor.c
15 contrib/python-zstandard/c-ext/compressor.c
18 contrib/python-zstandard/c-ext/compressoriterator.c
16 contrib/python-zstandard/c-ext/compressoriterator.c
19 contrib/python-zstandard/c-ext/constants.c
17 contrib/python-zstandard/c-ext/constants.c
20 contrib/python-zstandard/c-ext/decompressionwriter.c
18 contrib/python-zstandard/c-ext/decompressionwriter.c
21 contrib/python-zstandard/c-ext/decompressobj.c
19 contrib/python-zstandard/c-ext/decompressobj.c
22 contrib/python-zstandard/c-ext/decompressor.c
20 contrib/python-zstandard/c-ext/decompressor.c
23 contrib/python-zstandard/c-ext/decompressoriterator.c
21 contrib/python-zstandard/c-ext/decompressoriterator.c
24 contrib/python-zstandard/c-ext/frameparams.c
22 contrib/python-zstandard/c-ext/frameparams.c
25 contrib/python-zstandard/c-ext/python-zstandard.h
23 contrib/python-zstandard/c-ext/python-zstandard.h
26 contrib/python-zstandard/zstd.c
24 contrib/python-zstandard/zstd.c
27 contrib/python-zstandard/zstd/common/bitstream.h
25 contrib/python-zstandard/zstd/common/bitstream.h
28 contrib/python-zstandard/zstd/common/entropy_common.c
26 contrib/python-zstandard/zstd/common/entropy_common.c
29 contrib/python-zstandard/zstd/common/error_private.c
27 contrib/python-zstandard/zstd/common/error_private.c
30 contrib/python-zstandard/zstd/common/error_private.h
28 contrib/python-zstandard/zstd/common/error_private.h
31 contrib/python-zstandard/zstd/common/fse.h
29 contrib/python-zstandard/zstd/common/fse.h
32 contrib/python-zstandard/zstd/common/fse_decompress.c
30 contrib/python-zstandard/zstd/common/fse_decompress.c
33 contrib/python-zstandard/zstd/common/huf.h
31 contrib/python-zstandard/zstd/common/huf.h
34 contrib/python-zstandard/zstd/common/mem.h
32 contrib/python-zstandard/zstd/common/mem.h
35 contrib/python-zstandard/zstd/common/pool.c
33 contrib/python-zstandard/zstd/common/pool.c
36 contrib/python-zstandard/zstd/common/pool.h
34 contrib/python-zstandard/zstd/common/pool.h
37 contrib/python-zstandard/zstd/common/threading.c
35 contrib/python-zstandard/zstd/common/threading.c
38 contrib/python-zstandard/zstd/common/threading.h
36 contrib/python-zstandard/zstd/common/threading.h
39 contrib/python-zstandard/zstd/common/xxhash.c
37 contrib/python-zstandard/zstd/common/xxhash.c
40 contrib/python-zstandard/zstd/common/xxhash.h
38 contrib/python-zstandard/zstd/common/xxhash.h
41 contrib/python-zstandard/zstd/common/zstd_common.c
39 contrib/python-zstandard/zstd/common/zstd_common.c
42 contrib/python-zstandard/zstd/common/zstd_errors.h
40 contrib/python-zstandard/zstd/common/zstd_errors.h
43 contrib/python-zstandard/zstd/common/zstd_internal.h
41 contrib/python-zstandard/zstd/common/zstd_internal.h
44 contrib/python-zstandard/zstd/compress/fse_compress.c
42 contrib/python-zstandard/zstd/compress/fse_compress.c
45 contrib/python-zstandard/zstd/compress/huf_compress.c
43 contrib/python-zstandard/zstd/compress/huf_compress.c
46 contrib/python-zstandard/zstd/compress/zstd_compress.c
44 contrib/python-zstandard/zstd/compress/zstd_compress.c
47 contrib/python-zstandard/zstd/compress/zstd_opt.h
45 contrib/python-zstandard/zstd/compress/zstd_opt.h
48 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
46 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
49 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
47 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
50 contrib/python-zstandard/zstd/decompress/huf_decompress.c
48 contrib/python-zstandard/zstd/decompress/huf_decompress.c
51 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
49 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
52 contrib/python-zstandard/zstd/dictBuilder/cover.c
50 contrib/python-zstandard/zstd/dictBuilder/cover.c
53 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
51 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
54 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
52 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
55 contrib/python-zstandard/zstd/dictBuilder/zdict.c
53 contrib/python-zstandard/zstd/dictBuilder/zdict.c
56 contrib/python-zstandard/zstd/dictBuilder/zdict.h
54 contrib/python-zstandard/zstd/dictBuilder/zdict.h
57 contrib/python-zstandard/zstd/zstd.h
55 contrib/python-zstandard/zstd/zstd.h
58 hgext/fsmonitor/pywatchman/bser.c
56 hgext/fsmonitor/pywatchman/bser.c
@@ -1,401 +1,399 b''
1 /*
1 /*
2 charencode.c - miscellaneous character encoding
2 charencode.c - miscellaneous character encoding
3
3
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #define PY_SSIZE_T_CLEAN
10 #define PY_SSIZE_T_CLEAN
11 #include <Python.h>
11 #include <Python.h>
12 #include <assert.h>
12 #include <assert.h>
13
13
14 #include "charencode.h"
14 #include "charencode.h"
15 #include "compat.h"
15 #include "compat.h"
16 #include "util.h"
16 #include "util.h"
17
17
18 #ifdef IS_PY3K
18 #ifdef IS_PY3K
19 /* The mapping of Python types is meant to be temporary to get Python
19 /* The mapping of Python types is meant to be temporary to get Python
20 * 3 to compile. We should remove this once Python 3 support is fully
20 * 3 to compile. We should remove this once Python 3 support is fully
21 * supported and proper types are used in the extensions themselves. */
21 * supported and proper types are used in the extensions themselves. */
22 #define PyInt_Type PyLong_Type
22 #define PyInt_Type PyLong_Type
23 #define PyInt_AS_LONG PyLong_AS_LONG
23 #define PyInt_AS_LONG PyLong_AS_LONG
24 #endif
24 #endif
25
25
26 /* clang-format off */
26 /* clang-format off */
27 static const char lowertable[128] = {
27 static const char lowertable[128] = {
28 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
28 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
29 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
29 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
30 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
30 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
31 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
31 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
32 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
32 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
33 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
33 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
34 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
34 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
35 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
35 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
36 '\x40',
36 '\x40',
37 '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */
37 '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */
38 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */
38 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */
39 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */
39 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */
40 '\x78', '\x79', '\x7a', /* X-Z */
40 '\x78', '\x79', '\x7a', /* X-Z */
41 '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
41 '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
42 '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67',
42 '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67',
43 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f',
43 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f',
44 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77',
44 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77',
45 '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
45 '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
46 };
46 };
47
47
48 static const char uppertable[128] = {
48 static const char uppertable[128] = {
49 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
49 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
50 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
50 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
51 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
51 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
52 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
52 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
53 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
53 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
54 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
54 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
55 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
55 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
56 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
56 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
57 '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47',
57 '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47',
58 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f',
58 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f',
59 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57',
59 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57',
60 '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
60 '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
61 '\x60',
61 '\x60',
62 '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', /* a-g */
62 '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', /* a-g */
63 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', /* h-o */
63 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', /* h-o */
64 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', /* p-w */
64 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', /* p-w */
65 '\x58', '\x59', '\x5a', /* x-z */
65 '\x58', '\x59', '\x5a', /* x-z */
66 '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
66 '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
67 };
67 };
68
68
69 /* 1: no escape, 2: \<c>, 6: \u<x> */
69 /* 1: no escape, 2: \<c>, 6: \u<x> */
70 static const uint8_t jsonlentable[256] = {
70 static const uint8_t jsonlentable[256] = {
71 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 6, 2, 2, 6, 6, /* b, t, n, f, r */
71 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 6, 2, 2, 6, 6, /* b, t, n, f, r */
72 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
72 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
73 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* " */
73 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* " */
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
76 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, /* \\ */
76 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, /* \\ */
77 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
77 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, /* DEL */
78 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, /* DEL */
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
82 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
82 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
83 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
83 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
86 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
86 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87 };
87 };
88
88
89 static const uint8_t jsonparanoidlentable[128] = {
89 static const uint8_t jsonparanoidlentable[128] = {
90 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 6, 2, 2, 6, 6, /* b, t, n, f, r */
90 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 6, 2, 2, 6, 6, /* b, t, n, f, r */
91 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
91 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
92 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* " */
92 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* " */
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 6, 1, /* <, > */
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 6, 1, /* <, > */
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, /* \\ */
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, /* \\ */
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, /* DEL */
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, /* DEL */
98 };
98 };
99
99
100 static const char hexchartable[16] = {
100 static const char hexchartable[16] = {
101 '0', '1', '2', '3', '4', '5', '6', '7',
101 '0', '1', '2', '3', '4', '5', '6', '7',
102 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
102 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
103 };
103 };
104 /* clang-format on */
104 /* clang-format on */
105
105
106 /*
106 /*
107 * Turn a hex-encoded string into binary.
107 * Turn a hex-encoded string into binary.
108 */
108 */
109 PyObject *unhexlify(const char *str, Py_ssize_t len)
109 PyObject *unhexlify(const char *str, Py_ssize_t len)
110 {
110 {
111 PyObject *ret;
111 PyObject *ret;
112 char *d;
112 char *d;
113 Py_ssize_t i;
113 Py_ssize_t i;
114
114
115 ret = PyBytes_FromStringAndSize(NULL, len / 2);
115 ret = PyBytes_FromStringAndSize(NULL, len / 2);
116
116
117 if (!ret)
117 if (!ret)
118 return NULL;
118 return NULL;
119
119
120 d = PyBytes_AsString(ret);
120 d = PyBytes_AsString(ret);
121
121
122 for (i = 0; i < len;) {
122 for (i = 0; i < len;) {
123 int hi = hexdigit(str, i++);
123 int hi = hexdigit(str, i++);
124 int lo = hexdigit(str, i++);
124 int lo = hexdigit(str, i++);
125 *d++ = (hi << 4) | lo;
125 *d++ = (hi << 4) | lo;
126 }
126 }
127
127
128 return ret;
128 return ret;
129 }
129 }
130
130
131 PyObject *isasciistr(PyObject *self, PyObject *args)
131 PyObject *isasciistr(PyObject *self, PyObject *args)
132 {
132 {
133 const char *buf;
133 const char *buf;
134 Py_ssize_t i, len;
134 Py_ssize_t i, len;
135 if (!PyArg_ParseTuple(args, "s#:isasciistr", &buf, &len))
135 if (!PyArg_ParseTuple(args, "s#:isasciistr", &buf, &len))
136 return NULL;
136 return NULL;
137 i = 0;
137 i = 0;
138 /* char array in PyStringObject should be at least 4-byte aligned */
138 /* char array in PyStringObject should be at least 4-byte aligned */
139 if (((uintptr_t)buf & 3) == 0) {
139 if (((uintptr_t)buf & 3) == 0) {
140 const uint32_t *p = (const uint32_t *)buf;
140 const uint32_t *p = (const uint32_t *)buf;
141 for (; i < len / 4; i++) {
141 for (; i < len / 4; i++) {
142 if (p[i] & 0x80808080U)
142 if (p[i] & 0x80808080U)
143 Py_RETURN_FALSE;
143 Py_RETURN_FALSE;
144 }
144 }
145 i *= 4;
145 i *= 4;
146 }
146 }
147 for (; i < len; i++) {
147 for (; i < len; i++) {
148 if (buf[i] & 0x80)
148 if (buf[i] & 0x80)
149 Py_RETURN_FALSE;
149 Py_RETURN_FALSE;
150 }
150 }
151 Py_RETURN_TRUE;
151 Py_RETURN_TRUE;
152 }
152 }
153
153
154 static inline PyObject *_asciitransform(PyObject *str_obj,
154 static inline PyObject *
155 const char table[128],
155 _asciitransform(PyObject *str_obj, const char table[128], PyObject *fallback_fn)
156 PyObject *fallback_fn)
157 {
156 {
158 char *str, *newstr;
157 char *str, *newstr;
159 Py_ssize_t i, len;
158 Py_ssize_t i, len;
160 PyObject *newobj = NULL;
159 PyObject *newobj = NULL;
161 PyObject *ret = NULL;
160 PyObject *ret = NULL;
162
161
163 str = PyBytes_AS_STRING(str_obj);
162 str = PyBytes_AS_STRING(str_obj);
164 len = PyBytes_GET_SIZE(str_obj);
163 len = PyBytes_GET_SIZE(str_obj);
165
164
166 newobj = PyBytes_FromStringAndSize(NULL, len);
165 newobj = PyBytes_FromStringAndSize(NULL, len);
167 if (!newobj)
166 if (!newobj)
168 goto quit;
167 goto quit;
169
168
170 newstr = PyBytes_AS_STRING(newobj);
169 newstr = PyBytes_AS_STRING(newobj);
171
170
172 for (i = 0; i < len; i++) {
171 for (i = 0; i < len; i++) {
173 char c = str[i];
172 char c = str[i];
174 if (c & 0x80) {
173 if (c & 0x80) {
175 if (fallback_fn != NULL) {
174 if (fallback_fn != NULL) {
176 ret = PyObject_CallFunctionObjArgs(fallback_fn,
175 ret = PyObject_CallFunctionObjArgs(
177 str_obj, NULL);
176 fallback_fn, str_obj, NULL);
178 } else {
177 } else {
179 PyObject *err = PyUnicodeDecodeError_Create(
178 PyObject *err = PyUnicodeDecodeError_Create(
180 "ascii", str, len, i, (i + 1),
179 "ascii", str, len, i, (i + 1),
181 "unexpected code byte");
180 "unexpected code byte");
182 PyErr_SetObject(PyExc_UnicodeDecodeError, err);
181 PyErr_SetObject(PyExc_UnicodeDecodeError, err);
183 Py_XDECREF(err);
182 Py_XDECREF(err);
184 }
183 }
185 goto quit;
184 goto quit;
186 }
185 }
187 newstr[i] = table[(unsigned char)c];
186 newstr[i] = table[(unsigned char)c];
188 }
187 }
189
188
190 ret = newobj;
189 ret = newobj;
191 Py_INCREF(ret);
190 Py_INCREF(ret);
192 quit:
191 quit:
193 Py_XDECREF(newobj);
192 Py_XDECREF(newobj);
194 return ret;
193 return ret;
195 }
194 }
196
195
197 PyObject *asciilower(PyObject *self, PyObject *args)
196 PyObject *asciilower(PyObject *self, PyObject *args)
198 {
197 {
199 PyObject *str_obj;
198 PyObject *str_obj;
200 if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
199 if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
201 return NULL;
200 return NULL;
202 return _asciitransform(str_obj, lowertable, NULL);
201 return _asciitransform(str_obj, lowertable, NULL);
203 }
202 }
204
203
205 PyObject *asciiupper(PyObject *self, PyObject *args)
204 PyObject *asciiupper(PyObject *self, PyObject *args)
206 {
205 {
207 PyObject *str_obj;
206 PyObject *str_obj;
208 if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
207 if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
209 return NULL;
208 return NULL;
210 return _asciitransform(str_obj, uppertable, NULL);
209 return _asciitransform(str_obj, uppertable, NULL);
211 }
210 }
212
211
213 PyObject *make_file_foldmap(PyObject *self, PyObject *args)
212 PyObject *make_file_foldmap(PyObject *self, PyObject *args)
214 {
213 {
215 PyObject *dmap, *spec_obj, *normcase_fallback;
214 PyObject *dmap, *spec_obj, *normcase_fallback;
216 PyObject *file_foldmap = NULL;
215 PyObject *file_foldmap = NULL;
217 enum normcase_spec spec;
216 enum normcase_spec spec;
218 PyObject *k, *v;
217 PyObject *k, *v;
219 dirstateTupleObject *tuple;
218 dirstateTupleObject *tuple;
220 Py_ssize_t pos = 0;
219 Py_ssize_t pos = 0;
221 const char *table;
220 const char *table;
222
221
223 if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap",
222 if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap", &PyDict_Type,
224 &PyDict_Type, &dmap,
223 &dmap, &PyInt_Type, &spec_obj, &PyFunction_Type,
225 &PyInt_Type, &spec_obj,
224 &normcase_fallback))
226 &PyFunction_Type, &normcase_fallback))
227 goto quit;
225 goto quit;
228
226
229 spec = (int)PyInt_AS_LONG(spec_obj);
227 spec = (int)PyInt_AS_LONG(spec_obj);
230 switch (spec) {
228 switch (spec) {
231 case NORMCASE_LOWER:
229 case NORMCASE_LOWER:
232 table = lowertable;
230 table = lowertable;
233 break;
231 break;
234 case NORMCASE_UPPER:
232 case NORMCASE_UPPER:
235 table = uppertable;
233 table = uppertable;
236 break;
234 break;
237 case NORMCASE_OTHER:
235 case NORMCASE_OTHER:
238 table = NULL;
236 table = NULL;
239 break;
237 break;
240 default:
238 default:
241 PyErr_SetString(PyExc_TypeError, "invalid normcasespec");
239 PyErr_SetString(PyExc_TypeError, "invalid normcasespec");
242 goto quit;
240 goto quit;
243 }
241 }
244
242
245 /* Add some more entries to deal with additions outside this
243 /* Add some more entries to deal with additions outside this
246 function. */
244 function. */
247 file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
245 file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
248 if (file_foldmap == NULL)
246 if (file_foldmap == NULL)
249 goto quit;
247 goto quit;
250
248
251 while (PyDict_Next(dmap, &pos, &k, &v)) {
249 while (PyDict_Next(dmap, &pos, &k, &v)) {
252 if (!dirstate_tuple_check(v)) {
250 if (!dirstate_tuple_check(v)) {
253 PyErr_SetString(PyExc_TypeError,
251 PyErr_SetString(PyExc_TypeError,
254 "expected a dirstate tuple");
252 "expected a dirstate tuple");
255 goto quit;
253 goto quit;
256 }
254 }
257
255
258 tuple = (dirstateTupleObject *)v;
256 tuple = (dirstateTupleObject *)v;
259 if (tuple->state != 'r') {
257 if (tuple->state != 'r') {
260 PyObject *normed;
258 PyObject *normed;
261 if (table != NULL) {
259 if (table != NULL) {
262 normed = _asciitransform(k, table,
260 normed = _asciitransform(k, table,
263 normcase_fallback);
261 normcase_fallback);
264 } else {
262 } else {
265 normed = PyObject_CallFunctionObjArgs(
263 normed = PyObject_CallFunctionObjArgs(
266 normcase_fallback, k, NULL);
264 normcase_fallback, k, NULL);
267 }
265 }
268
266
269 if (normed == NULL)
267 if (normed == NULL)
270 goto quit;
268 goto quit;
271 if (PyDict_SetItem(file_foldmap, normed, k) == -1) {
269 if (PyDict_SetItem(file_foldmap, normed, k) == -1) {
272 Py_DECREF(normed);
270 Py_DECREF(normed);
273 goto quit;
271 goto quit;
274 }
272 }
275 Py_DECREF(normed);
273 Py_DECREF(normed);
276 }
274 }
277 }
275 }
278 return file_foldmap;
276 return file_foldmap;
279 quit:
277 quit:
280 Py_XDECREF(file_foldmap);
278 Py_XDECREF(file_foldmap);
281 return NULL;
279 return NULL;
282 }
280 }
283
281
284 /* calculate length of JSON-escaped string; returns -1 if unsupported */
282 /* calculate length of JSON-escaped string; returns -1 if unsupported */
285 static Py_ssize_t jsonescapelen(const char *buf, Py_ssize_t len, bool paranoid)
283 static Py_ssize_t jsonescapelen(const char *buf, Py_ssize_t len, bool paranoid)
286 {
284 {
287 Py_ssize_t i, esclen = 0;
285 Py_ssize_t i, esclen = 0;
288
286
289 if (paranoid) {
287 if (paranoid) {
290 /* don't want to process multi-byte escapes in C */
288 /* don't want to process multi-byte escapes in C */
291 for (i = 0; i < len; i++) {
289 for (i = 0; i < len; i++) {
292 char c = buf[i];
290 char c = buf[i];
293 if (c & 0x80) {
291 if (c & 0x80) {
294 PyErr_SetString(PyExc_ValueError,
292 PyErr_SetString(PyExc_ValueError,
295 "cannot process non-ascii str");
293 "cannot process non-ascii str");
296 return -1;
294 return -1;
297 }
295 }
298 esclen += jsonparanoidlentable[(unsigned char)c];
296 esclen += jsonparanoidlentable[(unsigned char)c];
299 if (esclen < 0) {
297 if (esclen < 0) {
300 PyErr_SetString(PyExc_MemoryError,
298 PyErr_SetString(PyExc_MemoryError,
301 "overflow in jsonescapelen");
299 "overflow in jsonescapelen");
302 return -1;
300 return -1;
303 }
301 }
304 }
302 }
305 } else {
303 } else {
306 for (i = 0; i < len; i++) {
304 for (i = 0; i < len; i++) {
307 char c = buf[i];
305 char c = buf[i];
308 esclen += jsonlentable[(unsigned char)c];
306 esclen += jsonlentable[(unsigned char)c];
309 if (esclen < 0) {
307 if (esclen < 0) {
310 PyErr_SetString(PyExc_MemoryError,
308 PyErr_SetString(PyExc_MemoryError,
311 "overflow in jsonescapelen");
309 "overflow in jsonescapelen");
312 return -1;
310 return -1;
313 }
311 }
314 }
312 }
315 }
313 }
316
314
317 return esclen;
315 return esclen;
318 }
316 }
319
317
320 /* map '\<c>' escape character */
318 /* map '\<c>' escape character */
321 static char jsonescapechar2(char c)
319 static char jsonescapechar2(char c)
322 {
320 {
323 switch (c) {
321 switch (c) {
324 case '\b':
322 case '\b':
325 return 'b';
323 return 'b';
326 case '\t':
324 case '\t':
327 return 't';
325 return 't';
328 case '\n':
326 case '\n':
329 return 'n';
327 return 'n';
330 case '\f':
328 case '\f':
331 return 'f';
329 return 'f';
332 case '\r':
330 case '\r':
333 return 'r';
331 return 'r';
334 case '"':
332 case '"':
335 return '"';
333 return '"';
336 case '\\':
334 case '\\':
337 return '\\';
335 return '\\';
338 }
336 }
339 return '\0'; /* should not happen */
337 return '\0'; /* should not happen */
340 }
338 }
341
339
342 /* convert 'origbuf' to JSON-escaped form 'escbuf'; 'origbuf' should only
340 /* convert 'origbuf' to JSON-escaped form 'escbuf'; 'origbuf' should only
343 include characters mappable by json(paranoid)lentable */
341 include characters mappable by json(paranoid)lentable */
344 static void encodejsonescape(char *escbuf, Py_ssize_t esclen,
342 static void encodejsonescape(char *escbuf, Py_ssize_t esclen,
345 const char *origbuf, Py_ssize_t origlen,
343 const char *origbuf, Py_ssize_t origlen,
346 bool paranoid)
344 bool paranoid)
347 {
345 {
348 const uint8_t *lentable =
346 const uint8_t *lentable =
349 (paranoid) ? jsonparanoidlentable : jsonlentable;
347 (paranoid) ? jsonparanoidlentable : jsonlentable;
350 Py_ssize_t i, j;
348 Py_ssize_t i, j;
351
349
352 for (i = 0, j = 0; i < origlen; i++) {
350 for (i = 0, j = 0; i < origlen; i++) {
353 char c = origbuf[i];
351 char c = origbuf[i];
354 uint8_t l = lentable[(unsigned char)c];
352 uint8_t l = lentable[(unsigned char)c];
355 assert(j + l <= esclen);
353 assert(j + l <= esclen);
356 switch (l) {
354 switch (l) {
357 case 1:
355 case 1:
358 escbuf[j] = c;
356 escbuf[j] = c;
359 break;
357 break;
360 case 2:
358 case 2:
361 escbuf[j] = '\\';
359 escbuf[j] = '\\';
362 escbuf[j + 1] = jsonescapechar2(c);
360 escbuf[j + 1] = jsonescapechar2(c);
363 break;
361 break;
364 case 6:
362 case 6:
365 memcpy(escbuf + j, "\\u00", 4);
363 memcpy(escbuf + j, "\\u00", 4);
366 escbuf[j + 4] = hexchartable[(unsigned char)c >> 4];
364 escbuf[j + 4] = hexchartable[(unsigned char)c >> 4];
367 escbuf[j + 5] = hexchartable[(unsigned char)c & 0xf];
365 escbuf[j + 5] = hexchartable[(unsigned char)c & 0xf];
368 break;
366 break;
369 }
367 }
370 j += l;
368 j += l;
371 }
369 }
372 }
370 }
373
371
374 PyObject *jsonescapeu8fast(PyObject *self, PyObject *args)
372 PyObject *jsonescapeu8fast(PyObject *self, PyObject *args)
375 {
373 {
376 PyObject *origstr, *escstr;
374 PyObject *origstr, *escstr;
377 const char *origbuf;
375 const char *origbuf;
378 Py_ssize_t origlen, esclen;
376 Py_ssize_t origlen, esclen;
379 int paranoid;
377 int paranoid;
380 if (!PyArg_ParseTuple(args, "O!i:jsonescapeu8fast",
378 if (!PyArg_ParseTuple(args, "O!i:jsonescapeu8fast", &PyBytes_Type,
381 &PyBytes_Type, &origstr, &paranoid))
379 &origstr, &paranoid))
382 return NULL;
380 return NULL;
383
381
384 origbuf = PyBytes_AS_STRING(origstr);
382 origbuf = PyBytes_AS_STRING(origstr);
385 origlen = PyBytes_GET_SIZE(origstr);
383 origlen = PyBytes_GET_SIZE(origstr);
386 esclen = jsonescapelen(origbuf, origlen, paranoid);
384 esclen = jsonescapelen(origbuf, origlen, paranoid);
387 if (esclen < 0)
385 if (esclen < 0)
388 return NULL; /* unsupported char found or overflow */
386 return NULL; /* unsupported char found or overflow */
389 if (origlen == esclen) {
387 if (origlen == esclen) {
390 Py_INCREF(origstr);
388 Py_INCREF(origstr);
391 return origstr;
389 return origstr;
392 }
390 }
393
391
394 escstr = PyBytes_FromStringAndSize(NULL, esclen);
392 escstr = PyBytes_FromStringAndSize(NULL, esclen);
395 if (!escstr)
393 if (!escstr)
396 return NULL;
394 return NULL;
397 encodejsonescape(PyBytes_AS_STRING(escstr), esclen, origbuf, origlen,
395 encodejsonescape(PyBytes_AS_STRING(escstr), esclen, origbuf, origlen,
398 paranoid);
396 paranoid);
399
397
400 return escstr;
398 return escstr;
401 }
399 }
General Comments 0
You need to be logged in to leave comments. Login now