##// END OF EJS Templates
encoding: add function to test if a str consists of ASCII characters...
Yuya Nishihara -
r33927:f4433f27 default
parent child Browse files
Show More
@@ -0,0 +1,33 b''
1 from __future__ import absolute_import
2
3 import unittest
4
5 from mercurial import (
6 encoding,
7 )
8
9 class IsasciistrTest(unittest.TestCase):
10 asciistrs = [
11 b'a',
12 b'ab',
13 b'abc',
14 b'abcd',
15 b'abcde',
16 b'abcdefghi',
17 b'abcd\0fghi',
18 ]
19
20 def testascii(self):
21 for s in self.asciistrs:
22 self.assertTrue(encoding.isasciistr(s))
23
24 def testnonasciichar(self):
25 for s in self.asciistrs:
26 for i in range(len(s)):
27 t = bytearray(s)
28 t[i] |= 0x80
29 self.assertFalse(encoding.isasciistr(bytes(t)))
30
31 if __name__ == '__main__':
32 import silenttestrunner
33 silenttestrunner.main(__name__)
@@ -18,6 +18,7 b' test-doctest.py'
18 test-duplicateoptions.py
18 test-duplicateoptions.py
19 test-empty-dir.t
19 test-empty-dir.t
20 test-empty.t
20 test-empty.t
21 test-encoding-func.py
21 test-excessive-merge.t
22 test-excessive-merge.t
22 test-hghave.t
23 test-hghave.t
23 test-imports-checker.t
24 test-imports-checker.t
@@ -12,6 +12,7 b''
12 #include <assert.h>
12 #include <assert.h>
13
13
14 #include "charencode.h"
14 #include "charencode.h"
15 #include "compat.h"
15 #include "util.h"
16 #include "util.h"
16
17
17 #ifdef IS_PY3K
18 #ifdef IS_PY3K
@@ -125,6 +126,29 b' PyObject *unhexlify(const char *str, Py_'
125 return ret;
126 return ret;
126 }
127 }
127
128
129 PyObject *isasciistr(PyObject *self, PyObject *args)
130 {
131 const char *buf;
132 Py_ssize_t i, len;
133 if (!PyArg_ParseTuple(args, "s#:isasciistr", &buf, &len))
134 return NULL;
135 i = 0;
136 /* char array in PyStringObject should be at least 4-byte aligned */
137 if (((uintptr_t)buf & 3) == 0) {
138 const uint32_t *p = (const uint32_t *)buf;
139 for (; i < len / 4; i++) {
140 if (p[i] & 0x80808080U)
141 Py_RETURN_FALSE;
142 }
143 i *= 4;
144 }
145 for (; i < len; i++) {
146 if (buf[i] & 0x80)
147 Py_RETURN_FALSE;
148 }
149 Py_RETURN_TRUE;
150 }
151
128 static inline PyObject *_asciitransform(PyObject *str_obj,
152 static inline PyObject *_asciitransform(PyObject *str_obj,
129 const char table[128],
153 const char table[128],
130 PyObject *fallback_fn)
154 PyObject *fallback_fn)
@@ -19,6 +19,7 b' enum normcase_spec {'
19 };
19 };
20
20
21 PyObject *unhexlify(const char *str, Py_ssize_t len);
21 PyObject *unhexlify(const char *str, Py_ssize_t len);
22 PyObject *isasciistr(PyObject *self, PyObject *args);
22 PyObject *asciilower(PyObject *self, PyObject *args);
23 PyObject *asciilower(PyObject *self, PyObject *args);
23 PyObject *asciiupper(PyObject *self, PyObject *args);
24 PyObject *asciiupper(PyObject *self, PyObject *args);
24 PyObject *make_file_foldmap(PyObject *self, PyObject *args);
25 PyObject *make_file_foldmap(PyObject *self, PyObject *args);
@@ -696,6 +696,7 b' static PyMethodDef methods[] = {'
696 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
696 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
697 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
697 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
698 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
698 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
699 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
699 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
700 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
700 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
701 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
701 {"dict_new_presized", dict_new_presized, METH_VARARGS,
702 {"dict_new_presized", dict_new_presized, METH_VARARGS,
@@ -716,7 +717,7 b' void dirs_module_init(PyObject *mod);'
716 void manifest_module_init(PyObject *mod);
717 void manifest_module_init(PyObject *mod);
717 void revlog_module_init(PyObject *mod);
718 void revlog_module_init(PyObject *mod);
718
719
719 static const int version = 2;
720 static const int version = 3;
720
721
721 static void module_init(PyObject *mod)
722 static void module_init(PyObject *mod)
722 {
723 {
@@ -7,8 +7,10 b''
7 #define inline __inline
7 #define inline __inline
8 #if defined(_WIN64)
8 #if defined(_WIN64)
9 typedef __int64 ssize_t;
9 typedef __int64 ssize_t;
10 typedef unsigned __int64 uintptr_t;
10 #else
11 #else
11 typedef int ssize_t;
12 typedef int ssize_t;
13 typedef unsigned int uintptr_t;
12 #endif
14 #endif
13 typedef signed char int8_t;
15 typedef signed char int8_t;
14 typedef short int16_t;
16 typedef short int16_t;
@@ -24,6 +24,7 b' from .pure import ('
24
24
25 charencode = policy.importmod(r'charencode')
25 charencode = policy.importmod(r'charencode')
26
26
27 isasciistr = charencode.isasciistr
27 asciilower = charencode.asciilower
28 asciilower = charencode.asciilower
28 asciiupper = charencode.asciiupper
29 asciiupper = charencode.asciiupper
29 _jsonescapeu8fast = charencode.jsonescapeu8fast
30 _jsonescapeu8fast = charencode.jsonescapeu8fast
@@ -75,7 +75,7 b' def _importfrom(pkgname, modname):'
75 (r'cext', r'diffhelpers'): 1,
75 (r'cext', r'diffhelpers'): 1,
76 (r'cext', r'mpatch'): 1,
76 (r'cext', r'mpatch'): 1,
77 (r'cext', r'osutil'): 1,
77 (r'cext', r'osutil'): 1,
78 (r'cext', r'parsers'): 2,
78 (r'cext', r'parsers'): 3,
79 }
79 }
80
80
81 # map import request to other package or module
81 # map import request to other package or module
@@ -13,6 +13,13 b' from .. import ('
13 pycompat,
13 pycompat,
14 )
14 )
15
15
16 def isasciistr(s):
17 try:
18 s.decode('ascii')
19 return True
20 except UnicodeDecodeError:
21 return False
22
16 def asciilower(s):
23 def asciilower(s):
17 '''convert a string to lowercase if ASCII
24 '''convert a string to lowercase if ASCII
18
25
General Comments 0
You need to be logged in to leave comments. Login now