Failed Conditions
Pull Request — master (#1127)
by Mischa
01:56
created

coalib.bearlib.spacing.SpacingHelper.get_indentation()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 23
rs 8.2508
1
from coalib.bearlib.abstractions.SectionCreatable import SectionCreatable
2
3
4
class SpacingHelper(SectionCreatable):
5
    DEFAULT_TAB_WIDTH = 4
6
7
    def __init__(self, tab_width: int=DEFAULT_TAB_WIDTH):
8
        """
9
        Creates a helper object for spacing operations.
10
11
        :param tab_width: The number of spaces which visually equals a tab.
12
        """
13
        SectionCreatable.__init__(self)
14
        if not isinstance(tab_width, int):
15
            raise TypeError("The 'tab_width' parameter should be an integer.")
16
17
        self.tab_width = tab_width
18
19
    def get_indentation(self, line):
20
        """
21
        Checks the lines indentation.
22
23
        :param line: A string to check for indentation.
24
        :return:     The indentation count in spaces.
25
        """
26
        if not isinstance(line, str):
27
            raise TypeError("The 'line' parameter should be a string.")
28
29
        count = 0
30
        for char in line:
31
            if char == ' ':
32
                count += 1
33
                continue
34
35
            if char == '\t':
36
                count += self.tab_width - (count % self.tab_width)
37
                continue
38
39
            break
40
41
        return count
42
43
    def replace_tabs_with_spaces(self, line):
44
        """
45
        Replaces tabs in this line with the appropriate number of spaces.
46
47
        Example: " \t" will be converted to "    ", assuming the tab_width is
48
        set to 4.
49
50
        :param line: The string with tabs to replace.
51
        :return:     A string with no tabs.
52
        """
53
        if not isinstance(line, str):
54
            raise TypeError("The 'line' parameter should be a string.")
55
56
        result = ""
57
        tabless_position = 0
58
        for char in line:
59
            if char == '\t':
60
                space_count = (self.tab_width - tabless_position
61
                               % self.tab_width)
62
                result += space_count * " "
63
                tabless_position += space_count
64
                continue
65
66
            result += char
67
            tabless_position += 1
68
69
        return result
70
71
    def replace_spaces_with_tabs(self, line):
72
        """
73
        Replaces spaces with tabs where possible. However in no case only one
74
        space will be replaced by a tab.
75
76
        Example: " \t   a_text   another" will be converted to
77
        "\t   a_text\tanother", assuming the tab_width is set to 4.
78
79
        :param line: The string with spaces to replace.
80
        :return:     The converted string.
81
        """
82
        if not isinstance(line, str):
83
            raise TypeError("The 'line' parameter should be a string.")
84
85
        currspaces = 0
86
        result = ""
87
        # Tracking the index of the string isnt enough because tabs are
88
        # spanning over multiple columns
89
        tabless_position = 0
90
        for char in line:
91
            if char == " ":
92
                currspaces += 1
93
                tabless_position += 1
94
            elif char == "\t":
95
                space_count = (self.tab_width - tabless_position
96
                               % self.tab_width)
97
                currspaces += space_count
98
                tabless_position += space_count
99
            else:
100
                result += currspaces*" " + char
101
                currspaces = 0
102
                tabless_position += 1
103
104
            # tabless_position is now incremented to point _after_ the current
105
            # char
106
            if tabless_position % self.tab_width == 0:
107
                if currspaces > 1:
108
                    result += "\t"
109
                else:
110
                    result += currspaces*" "
111
112
                currspaces = 0
113
114
        result += currspaces*" "
115
116
        return result
117