forked from microsoft/dpu-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifiersplitting.py
60 lines (54 loc) · 1.96 KB
/
identifiersplitting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from functools import lru_cache
from typing import List
def split_camelcase(camel_case_identifier: str) -> List[str]:
"""
Split camelCase identifiers.
"""
if not len(camel_case_identifier):
return []
# split into words based on adjacent cases being the same
result = []
current = str(camel_case_identifier[0])
prev_upper = camel_case_identifier[0].isupper()
prev_digit = camel_case_identifier[0].isdigit()
prev_special = not camel_case_identifier[0].isalnum()
for c in camel_case_identifier[1:]:
upper = c.isupper()
digit = c.isdigit()
special = not c.isalnum()
new_upper_word = upper and not prev_upper
new_digit_word = digit and not prev_digit
new_special_word = special and not prev_special
if new_digit_word or new_upper_word or new_special_word:
result.append(current)
current = c
elif not upper and prev_upper and len(current) > 1:
result.append(current[:-1])
current = current[-1] + c
elif not digit and prev_digit:
result.append(current)
current = c
elif not special and prev_special:
result.append(current)
current = c
else:
current += c
prev_digit = digit
prev_upper = upper
prev_special = special
result.append(current)
return result
@lru_cache(maxsize=5000)
def split_identifier_into_parts(identifier: str) -> List[str]:
"""
Split a single identifier into parts on snake_case and camelCase
"""
snake_case = identifier.split("_")
identifier_parts = [] # type: List[str]
for i in range(len(snake_case)):
part = snake_case[i]
if len(part) > 0:
identifier_parts.extend(s.lower() for s in split_camelcase(part))
if len(identifier_parts) == 0:
return [identifier]
return identifier_parts