|
1 | 1 | __all__ = ["is_digit", "is_letter", "is_name_start", "is_name_continue"] |
2 | 2 |
|
| 3 | +try: |
| 4 | + "string".isascii() |
| 5 | +except AttributeError: # Python < 3.7 |
3 | 6 |
|
4 | | -def is_digit(char: str) -> bool: |
5 | | - """Check whether char is a digit |
| 7 | + def is_digit(char: str) -> bool: |
| 8 | + """Check whether char is a digit |
6 | 9 |
|
7 | | - For internal use by the lexer only. |
8 | | - """ |
9 | | - return "0" <= char <= "9" |
| 10 | + For internal use by the lexer only. |
| 11 | + """ |
| 12 | + return "0" <= char <= "9" |
10 | 13 |
|
| 14 | + def is_letter(char: str) -> bool: |
| 15 | + """Check whether char is a plain ASCII letter |
11 | 16 |
|
12 | | -def is_letter(char: str) -> bool: |
13 | | - """Check whether char is a plain ASCII letter |
| 17 | + For internal use by the lexer only. |
| 18 | + """ |
| 19 | + return "a" <= char <= "z" or "A" <= char <= "Z" |
14 | 20 |
|
15 | | - For internal use by the lexer only. |
16 | | - """ |
17 | | - return "A" <= char <= "Z" or "a" <= char <= "z" |
| 21 | + def is_name_start(char: str) -> bool: |
| 22 | + """Check whether char is allowed at the beginning of a GraphQL name |
18 | 23 |
|
| 24 | + For internal use by the lexer only. |
| 25 | + """ |
| 26 | + return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_" |
19 | 27 |
|
20 | | -def is_name_start(char: str) -> bool: |
21 | | - """Check whether char is allowed at the beginning of a GraphQL name |
| 28 | + def is_name_continue(char: str) -> bool: |
| 29 | + """Check whether char is allowed in the continuation of a GraphQL name |
22 | 30 |
|
23 | | - For internal use by the lexer only. |
24 | | - """ |
25 | | - return is_letter(char) or char == "_" |
| 31 | + For internal use by the lexer only. |
| 32 | + """ |
| 33 | + return ( |
| 34 | + "a" <= char <= "z" |
| 35 | + or "A" <= char <= "Z" |
| 36 | + or "0" <= char <= "9" |
| 37 | + or char == "_" |
| 38 | + ) |
26 | 39 |
|
| 40 | +else: |
27 | 41 |
|
28 | | -def is_name_continue(char: str) -> bool: |
29 | | - """Check whether char is allowed in the continuation of a GraphQL name |
| 42 | + def is_digit(char: str) -> bool: |
| 43 | + """Check whether char is a digit |
30 | 44 |
|
31 | | - For internal use by the lexer only. |
32 | | - """ |
33 | | - return is_letter(char) or is_digit(char) or char == "_" |
| 45 | + For internal use by the lexer only. |
| 46 | + """ |
| 47 | + return char.isascii() and char.isdigit() |
| 48 | + |
| 49 | + def is_letter(char: str) -> bool: |
| 50 | + """Check whether char is a plain ASCII letter |
| 51 | +
|
| 52 | + For internal use by the lexer only. |
| 53 | + """ |
| 54 | + return char.isascii() and char.isalpha() |
| 55 | + |
| 56 | + def is_name_start(char: str) -> bool: |
| 57 | + """Check whether char is allowed at the beginning of a GraphQL name |
| 58 | +
|
| 59 | + For internal use by the lexer only. |
| 60 | + """ |
| 61 | + return char.isascii() and (char.isalpha() or char == "_") |
| 62 | + |
| 63 | + def is_name_continue(char: str) -> bool: |
| 64 | + """Check whether char is allowed in the continuation of a GraphQL name |
| 65 | +
|
| 66 | + For internal use by the lexer only. |
| 67 | + """ |
| 68 | + return char.isascii() and (char.isalnum() or char == "_") |
0 commit comments