mirror of
https://github.com/python/cpython.git
synced 2025-10-07 15:42:02 +00:00
optimize all_name_chars (#3442)
Remove redundant PyUnicode_Check call. Use a static table for checking chars.
This commit is contained in:
parent
590665c399
commit
9020ac7cce
1 changed files with 14 additions and 14 deletions
|
@ -4,9 +4,6 @@
|
||||||
#include "code.h"
|
#include "code.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
|
|
||||||
#define NAME_CHARS \
|
|
||||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
|
|
||||||
|
|
||||||
/* Holder for co_extra information */
|
/* Holder for co_extra information */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Py_ssize_t ce_size;
|
Py_ssize_t ce_size;
|
||||||
|
@ -18,23 +15,26 @@ typedef struct {
|
||||||
static int
|
static int
|
||||||
all_name_chars(PyObject *o)
|
all_name_chars(PyObject *o)
|
||||||
{
|
{
|
||||||
static char ok_name_char[256];
|
/* [a-zA-Z0-9_] */
|
||||||
static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
|
static const bool ok_name_char[128] = {
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
|
||||||
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
|
||||||
|
};
|
||||||
const unsigned char *s, *e;
|
const unsigned char *s, *e;
|
||||||
|
|
||||||
if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
|
if (PyUnicode_READY(o) == -1 || !PyUnicode_IS_ASCII(o))
|
||||||
!PyUnicode_IS_ASCII(o))
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (ok_name_char[*name_chars] == 0) {
|
|
||||||
const unsigned char *p;
|
|
||||||
for (p = name_chars; *p; p++)
|
|
||||||
ok_name_char[*p] = 1;
|
|
||||||
}
|
|
||||||
s = PyUnicode_1BYTE_DATA(o);
|
s = PyUnicode_1BYTE_DATA(o);
|
||||||
e = s + PyUnicode_GET_LENGTH(o);
|
e = s + PyUnicode_GET_LENGTH(o);
|
||||||
while (s != e) {
|
for (; s != e; s++) {
|
||||||
if (ok_name_char[*s++] == 0)
|
if (!ok_name_char[*s])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue