Module: Spacer::Text

Defined in:
lib/spacer/text.rb

Class Method Summary collapse

Class Method Details

.count_bol_spaces_and_tabs(lines) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spacer/text.rb', line 3

def self.count_bol_spaces_and_tabs(lines)
  bol = OpenStruct.new
  bol.spaces = 0
  bol.tabs = 0

  for line in lines do
    for i in 0...line.length do
      c = line[i]

      if c == " "
        bol.spaces += 1
      elsif c == "\t"
        bol.tabs += 1
      else
        break
      end
    end
  end

  bol
end

.tabify(lines, tabsize, round_down_spaces) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spacer/text.rb', line 49

def self.tabify(lines, tabsize, round_down_spaces)
  i = 0
  while i < lines.length do
    line = lines[i]
    j = 0
    bol = true
    num_bol_spaces = 0
    new_line = ""

    while j < line.length do
      c = line[j]

      if bol and c == " "
        num_bol_spaces += 1
      elsif bol and c != " "
        bol = false
        new_line += "\t" * (num_bol_spaces / tabsize)

        if !round_down_spaces
          new_line += " " * (num_bol_spaces % tabsize)
        end

        new_line += c
      else
        new_line += c
      end

      j += 1
    end

    lines[i] = new_line
    i += 1
  end
end

.untabify(lines, tabsize) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spacer/text.rb', line 25

def self.untabify(lines, tabsize)
  i = 0
  while i < lines.length do
    line = lines[i]
    j = 0
    new_line = ""

    while j < line.length do
      c = line[j]

      if c == "\t"
        num_spaces = tabsize - (new_line.length % tabsize)
        new_line += " " * num_spaces
      else
        new_line += c
      end
      j += 1
    end

    lines[i] = new_line
    i += 1
  end
end