Skip to content
Merged
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
1 change: 1 addition & 0 deletions mypyc/doc/str_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Methods
* ``s.isascii()``
* ``s.isdecimal()``
* ``s.isdigit()``
* ``s.isidentifier()``
* ``s.islower()``
* ``s.isnumeric()``
* ``s.isprintable()``
Expand Down
1 change: 1 addition & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,7 @@ bool CPyStr_IsPrintable(PyObject *str);
bool CPyStr_IsLower(PyObject *str);
bool CPyStr_IsTitle(PyObject *str);
bool CPyStr_IsUpper(PyObject *str);
bool CPyStr_IsIdentifier(PyObject *str);

// Bytes operations

Expand Down
18 changes: 18 additions & 0 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,21 @@ bool CPyStr_IsTitle(PyObject *str) {
}
return cased;
}

bool CPyStr_IsIdentifier(PyObject *str) {
if (PyUnicode_IS_ASCII(str)) {
// ASCII fast path: [A-Za-z_][A-Za-z0-9_]*
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (len == 0) return false;
const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str);
if (!Py_ISALPHA(data[0]) && data[0] != '_')
return false;
for (Py_ssize_t i = 1; i < len; i++) {
if (!Py_ISALNUM(data[i]) && data[i] != '_')
return false;
}
return true;
}
// Non-ASCII identifiers need XID_Start/XID_Continue checks (PEP 3131)
return PyUnicode_IsIdentifier(str) == 1;
}
9 changes: 9 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,15 @@
error_kind=ERR_NEVER,
)

# str.isidentifier()
method_op(
name="isidentifier",
arg_types=[str_rprimitive],
return_type=bool_rprimitive,
c_function_name="CPyStr_IsIdentifier",
error_kind=ERR_NEVER,
)


# obj.decode()
method_op(
Expand Down
1 change: 1 addition & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def isnumeric(self) -> bool: ...
def isprintable(self) -> bool: ...
def istitle(self) -> bool: ...
def isupper(self) -> bool: ...
def isidentifier(self) -> bool: ...

class float:
def __init__(self, x: object) -> None: pass
Expand Down
11 changes: 11 additions & 0 deletions mypyc/test-data/irbuild-str.test
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,17 @@ L0:
r0 = CPyStr_IsUpper(x)
return r0

[case testStrIsIdentifier]
def is_identifier(x: str) -> bool:
return x.isidentifier()
[out]
def is_identifier(x):
x :: str
r0 :: bool
L0:
r0 = CPyStr_IsIdentifier(x)
return r0

[case testStrIndexEqLiteral_64bit]
def is_comma(s: str, i: int) -> bool:
return s[i] == ","
Expand Down
55 changes: 55 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,61 @@ def test_str_subclass() -> None:
class S(str):
pass

[case testStrIsIdentifier]
from typing import Any

def check(s: str) -> None:
# Any forces generic dispatch, so we compare our primitive against CPython's
a: Any = s
assert s.isidentifier() == a.isidentifier(), f"isidentifier mismatch for {s!r}"

def test_all_code_points() -> None:
# Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds, and both the start
# and continuation positions. Check all BMP code points, and a sample of the
# others (checking all of them is slow).
for i in [*range(0x10000), *range(0x10000, 0x110000, 97), 0x10FFFF]:
c = chr(i)
check(c)
check("a" + c)

def test_strings() -> None:
samples = [
"", "_", "__", "a", "abc", "ABC", "_abc_123", "abc_", "class", "None", "1", "1abc",
"abc-def", "a b", "abc!", "abc\n", "\u00e9t\u00e9", "\u00e9", "\u4e2d\u6587", "x\u0660",
"\u0660x", "x\u00b2", "\u2167", "\u212e", "\U00010400abc", "abc\U00010400",
"\U0001f600", "abc\u2000", "abc\u00a0",
]
for s in samples:
check(s)
check(s + s)

def test_isidentifier() -> None:
assert "abc".isidentifier()
assert "_".isidentifier()
assert "_abc_123".isidentifier()
assert "ABC".isidentifier()
assert "class".isidentifier() # Keywords are identifiers
assert not "".isidentifier()
assert not "1abc".isidentifier()
assert not "abc-def".isidentifier()
assert not "a b".isidentifier()
assert "\u00e9t\u00e9".isidentifier()
assert "\u4e2d\u6587".isidentifier()
assert "x\u0660".isidentifier()
assert not "\u0660x".isidentifier()
assert "\U00010400abc".isidentifier()
assert not "\U0001f600".isidentifier()
assert not "abc\u2000".isidentifier()

def test_str_subclass() -> None:
from strsubclass import S
assert S("abc").isidentifier()
assert not S("1abc").isidentifier()

[file strsubclass.py]
class S(str):
pass

[case testStrIndexEqLiteralSpecialize]
from testutil import assertRaises

Expand Down
Loading