Skip to content

Commit 4698cee

Browse files
committed
Improved extract_indentation
1 parent cd20319 commit 4698cee

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/text_manipulation/indentation_kit.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,32 @@
44
from math import gcd
55
from typing import NamedTuple
66

7+
78
def get_line_indent_count(line: str):
89
return len(line) - len(line.lstrip())
910

1011

12+
def extract_indentation(line: str) -> str:
13+
"""
14+
Extract the leading whitespace from a given line.
15+
16+
Args:
17+
line (str): The input line to process.
18+
19+
Returns:
20+
str: The leading whitespace of the line.
21+
22+
Examples:
23+
>>> extract_indentation(" Hello")
24+
' '
25+
>>> extract_indentation("\t\tWorld")
26+
'\t\t'
27+
>>> extract_indentation("No indentation")
28+
''
29+
"""
30+
return line[:len(line) - len(line.lstrip())]
31+
32+
1133
class IndentationInfo(NamedTuple):
1234
"""
1335
A class to represent and manage indentation information.
@@ -69,9 +91,6 @@ def from_content[T: IndentationInfo, S: Sequence[str]](cls: T, content: str | S)
6991
# TODO Always send str?
7092
lines = [x.lstrip() for x in content.splitlines() if x.strip()] if isinstance(content, str) else content
7193

72-
def extract_indentation(line: str) -> str:
73-
return re.match(r'^\s*', line).group(0)
74-
7594
indentations = [extract_indentation(line) for line in lines if line.strip()]
7695

7796
if not indentations:

0 commit comments

Comments
 (0)