Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,10 @@ static inline int Py_UNICODE_ISSPACE(Py_UCS4 ch) {

#define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)

PyAPI_FUNC(int) _PyUnicode_IsAlnum(Py_UCS4 ch);

static inline int Py_UNICODE_ISALNUM(Py_UCS4 ch) {
return (Py_UNICODE_ISALPHA(ch)
|| Py_UNICODE_ISDECIMAL(ch)
|| Py_UNICODE_ISDIGIT(ch)
|| Py_UNICODE_ISNUMERIC(ch));
return _PyUnicode_IsAlnum(ch);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :meth:`str.isalnum` and Unicode word matching in :mod:`re` (``\w``,
``\W``, ``\b`` and ``\B``) by reducing Unicode lookups and adding an ASCII fast
path. Patch by Maurycy Pawłowski-Wieroński.
3 changes: 2 additions & 1 deletion Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ static unsigned int sre_upper_locale(unsigned int ch)
#define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE(ch)
#define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK(ch)
#define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM(ch)
#define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM(ch) || (ch) == '_')
#define SRE_UNI_IS_WORD(ch)\
((ch) < 128 ? SRE_IS_WORD(ch) : (SRE_UNI_IS_ALNUM(ch) || (ch) == '_'))
#define SRE_UNI_IS_ALPHA(ch) Py_UNICODE_ISALPHA(ch)
#define SRE_UNI_IS_LOWER(ch) Py_UNICODE_ISLOWER(ch)
#define SRE_UNI_IS_UPPER(ch) Py_UNICODE_ISUPPER(ch)
Expand Down
9 changes: 9 additions & 0 deletions Objects/unicodectype.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch)
/* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt',
'Lo' or 'Lm', 0 otherwise. */

int
_PyUnicode_IsAlnum(Py_UCS4 ch)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

return (ctype->flags
& (ALPHA_MASK | DECIMAL_MASK | DIGIT_MASK | NUMERIC_MASK)) != 0;
}

int _PyUnicode_IsAlpha(Py_UCS4 ch)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
Expand Down
Loading